我在Vista 64位平台上使用java获取mysql连接时遇到了新手问题。我一直在阅读其他有类似问题的人的网络信息,到目前为止运气不佳。这可能是由于我缺乏理解。我可以看到端口3306正在使用netstat-an进行侦听。我禁用了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:UsersJerry>java -cp c:Usersjerry;c:ProgramFiles(86)ConnectorJmysql-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.mic.Launcher$AppClassLoader@1054f93url是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:UsersJerry;c:ProgramFiles(86)ConnectorJmysql-connector-java-5.1.11-bin c:UsersJerrydbaccess.java
我的执行命令是:
java -cp c:Usersjerry;c:ProgramFiles(86)ConnectorJmysql-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;
}
}
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/dbaccess03tstrootpassword"MySQL查找一个名为dbaccess03tstrootpassword的数据库,该数据库不存在,导致连接失败为null。
我将代码更改为以下语法,并获得了有效的连接。
String urlFormated = "jdbc:mysql://localhost:3306/"
+ dbName
+ "?user="
+ userName
+ "&password="
+ password;
conn = DriverManager.getConnection(urlFormated);