JDBC无法连接到XAMPP

时间:2018-12-15 11:18:39

标签: java jdbc xampp

我在使用JDBC将Java程序连接到本地主机上的XAMPP mySql数据库时遇到问题。这是我的Java代码:

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;


public class Main {
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  static final String DB_URL = "jdbc:mysql://127.0.0.1:8443/cocktailbar";


  static final String USER = "vincent";
  static final String PASS = "test123";

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Connection conn = null;
    Statement stmt = null;
    try{
      //STEP 2: Register JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);
      System.out.println("Connected!");

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT Bezeichnung"
            + "FROM cocktail"
            + "WHERE Cocktailnummer = 1501;";
      ResultSet rs = stmt.executeQuery(sql);

      //STEP 5: Extract data from result set
      while(rs.next()){
         //Retrieve by column name
         String id  = rs.getString("Bezeichnung");


         //Display values
         System.out.print("ID: " + id);

      }
      //STEP 6: Clean-up environment
      rs.close();
      stmt.close();
      conn.close();
   }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)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}

}

我已经将my.cnf文件中的bind-address设置为0.0.0.0,当我运行Java程序时,它会尝试连接大约一分钟,然后再给我错误。它告诉我EOFException导致通信链接失败。当我使用调试模式时,它在getConnection方法处停止。我已经关闭了防病毒软件,允许在eclipse中为我的防火墙进行所有连接,并为XAMPP设置端口,如下所示:

XAMPP Port Setup

我已经使用PHPmyAdmin设置了用户名和密码,并授予了该用户所有权限。谁能帮我告诉我我做错了什么地方?预先感谢!

0 个答案:

没有答案