programing

Mariadb Xampp-Database에 연결할 수 없습니다.

closeapi 2023. 9. 8. 21:31
반응형

Mariadb Xampp-Database에 연결할 수 없습니다.

eclipse와 tomcat을 통해 로컬 MariaDB Database에 연결하려고 합니다.프로그램을 실행하면 다음과 같은 메시지가 나타납니다.

java.sql.SQLNonTransientConnectionException: Could not connect to localhost:8080 : unexpected end of stream, read 0 bytes from 4 (socket was closed by server)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.get(ExceptionMapper.java:240)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionMapper.getException(ExceptionMapper.java:171)
    at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1132)
    at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:561)
    at org.mariadb.jdbc.MariaDbConnection.newConnection(MariaDbConnection.java:175)
    at org.mariadb.jdbc.Driver.connect(Driver.java:92)

이것이 제가 실행하고 있는 코드입니다.

public void verbinde() {
        Connection conn = null;
          Statement stmt = null;
          try {
              //STEP 2: Register JDBC driver
              Class.forName("org.mariadb.jdbc.Driver");

              //STEP 3: Open a connection
              System.out.println("Connecting to a selected database...");
              conn = DriverManager.getConnection(
                      "jdbc:mariadb://localhost:8080/test?user=root&password=root");
              System.out.println("Connected database successfully...");

              //STEP 4: Execute a query
              System.out.println("Creating table in given database...");
              stmt = conn.createStatement();

              String sql = "CREATE TABLE REGISTRATION "
                      + "(id INTEGER not NULL, "
                      + " first VARCHAR(255), "
                      + " last VARCHAR(255), "
                      + " age INTEGER, "
                      + " PRIMARY KEY ( id ))";

              stmt.executeUpdate(sql);
              System.out.println("Created table in given database...");
          } catch (SQLException se) {
              //Handle errors for JDBC
              se.printStackTrace();
          } catch (Exception e) {
              //Handle errors for Class.forName
              e.printStackTrace();
          } finally {
              //finally block used to close resources
              try {
                  if (stmt != null) {
                      conn.close();
                  }
              } catch (SQLException se) {
              }// do nothing
              try {
                  if (conn != null) {
                      conn.close();
                  }
              } catch (SQLException se) {
                  se.printStackTrace();
              }//end finally try
          }//end try
          System.out.println("Goodbye!");
      }//end main
    //end JDBCExample

MariaDB의 기본 포트는 8080이 아닌 3306입니다.

jdbc:mariadb://localhost:3306/test?user=root&password=root

8080은 Tomcat이 작동하는 포트입니다.

BTW: Try-Catch-Finally 블록 대신 Try-with-Resource를 사용합니다.

언급URL : https://stackoverflow.com/questions/59568272/cannot-connect-to-mariadb-xampp-database

반응형