我已将 MySQL JDBC 库添加到我的 eclipse 项目中,但我仍然无法连接到数据库,程序不会抛出错误,它只是冻结在我身上。这是我使用的库的链接http://dev.mysql.com/downloads/connector/j和我当前的代码(省略了用户名、主机和密码。
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
@Override
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://host:8080/feedback?"
+ "user=******Admin&password=********");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
System.out.println("Connected");
} catch (Exception e) {
System.out.println("Connection failed");
解决方案 我的端口错误,驱动程序安装不正确,数据库名称错误。这是修复驱动程序后正确的连接线。
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/DataBase?"
+ "user=****Admin&password=*******");