我在 Vista 64 位平台上使用 java 获取 mysql 连接时遇到新手问题。我一直在阅读其他有类似问题的网络信息,到目前为止还没有运气。这可能是由于我缺乏理解。我可以使用 netstat -an 看到端口 3306 正在监听。我禁用了 MS 防火墙并安装了 webroot,因为我的防火墙和 mysql 是允许的。我已在 -cp 选项中添加了 JDBC 驱动程序 jar 文件的路径。调试似乎表明驱动程序未注册,但我可以从命令行对数据库执行所有mysql功能。
我看到了以下异常: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
这是我的输出:
C:\Users\Jerry>java -cp c:\Users\jerry;c:\ProgramFiles(86)\ConnectorJ\mysql-connector-java-5.1.11-bin dbaccess
FOO: jdbc:mysql://127.0.0.1:3306/access03tstroot
Cannot connect to database server
SQLEXCEPTION: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbaccess.mysqlConnection(dbaccess.java:77)
at dbaccess.main(dbaccess.java:59)
我打印出我要发送的消息,并设置断点以在 DriverManager.getConnection 中停止。我认为有一段代码可以检查已注册的驱动程序。
代码在 isDriverAllowed(aDriver.driver, callCL) 期间失败 据我了解,Class.forName 调用将在找到时注册一个新的驱动程序。 该循环进行两次传递,首先查找 ODBC 驱动程序,然后查找 com.mysql.jdbc.driver。
调用者CL = sun.misc.Launcher$AppClassLoader@1054f93 url 是 jdbc:mysql:/loacalhost:3306/access03tst
for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then
// skip it.
if(isDriverAllowed(aDriver.driver, callerCL)) {
try {
println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.driver.connect(url, info);
if (con != null) {
// Success!
println("getConnection returning " + aDriver.driver.getClass().getName());
return (con);
}
} catch (SQLException ex) {
if (reason == null) {
reason = ex;
}
}
} else {
println(" skipping: " + aDriver.getClass().getName());
}
}
我的编译命令是:
javac -d . -sourcepath . -cp c:\Users\Jerry;c:\ProgramFiles(86)\ConnectorJ\mysql-connector-java-5.1.11-bin c:\Users\Jerry\dbaccess.java
我的执行命令是:
java -cp c:\Users\jerry;c:\ProgramFiles(86)\ConnectorJ\mysql-connector-java-5.1.11-bin dbaccess
我的代码如下:
package dbaccess;
import java.sql.*;
import java.lang.*;
public class dbaccess
{
// Public static indicates that this is a class variable not an instance
// variable. It is part of the class as a whole.
public static Connection conn = null;
public boolean success;
public static void main (String[] args)
{
init();
mysqlConnection();
getList catList = new getList();
boolean status = catList.success;
}
public static void mysqlConnection()
{
try
{
String dbName = "access03tst";
String userName = "";
String password = "";
String url = "jdbc:mysql://localhost:3306/";
String driver = "com.mysql.jdbc.driver";
String foo = url+dbName+userName+password;
System.out.println("FOO: " + foo);
//Register the JDBC driver for MySQL.
Class.forName("com.mysql.jdbc.Driver"); //.newInstance ();
System.out.println("Surprise we got here ");
conn = DriverManager.getConnection(url+dbName+userName+password);
System.out.println ("Database connection established");
System.out.println("URL: " + url);
System.out.println("Connection: " + conn);
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
} // end finally
}
public static void init()
{
//
// Public static indicates that this is a class variable not an instance
// variable. It is part of the class as a whole.
conn = null;
boolean success = false;
boolean CatRequest = false;
}
}
最佳答案
我终于找到答案了。我使用了许多引用文献中的 url 语法。
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost:3306/";
conn = DriverManager.getConnection(url+dbName+userName+password);
这会导致 url 传递到 getConnection 代码中,显示为 “jdbc:mysql://localhost:3306/dbaccess03tstroot密码” MySQL 查找名为 dbaccess03tstrootpassword 的数据库,该数据库不存在并导致失败的连接为 null。
我将代码更改为以下语法,并获得了有效的连接。
String urlFormated = "jdbc:mysql://localhost:3306/"
+ dbName
+ "?user="
+ userName
+ "&password="
+ password;
conn = DriverManager.getConnection(urlFormated);
关于java - 使用java jdbc在vista上连接mysql,通讯异常,尝试了常见的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9574298/