我正在尝试设置 mySQL 连接器,但不确定出了什么问题。这是错误:
SQLException: No suitable driver found for jdbc:mysql://localhost/db?user=root&password=
这是我的java:
// ESTABLISH DRIVER INSTANCE
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
// ESTABLISH DB CONNECTION
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/db?" +
"user="+user+"&password="+pass);
// Do something with the Connection
//
//
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
这是我的 build.xml:
<project>
<target name="run">
<javac srcdir="." destdir=".">
<classpath>
<pathelement path="./mysql-connector-java-5.1.38i/mysql-connector-java-5.1.38-bin.jar"/>
<pathelement location="./DB_Test.class"/>
</classpath>
</javac>
<java classname="DB_Test">
<classpath>
<pathelement path="./mysql-connector-java-5.1.38i/mysql-connector-java-5.1.38-bin.jar"/>
<pathelement location="."/>
</classpath>
</java>
</target>
</project>
我认为问题可能出在 build.xml 中,因为我对 ant 非常缺乏经验。我不确定这两个“路径元素路径”是否都是必要的,或者其中之一是否有效。我想知道我是否需要仅仅为了这个 jar 而诉诸 Eclipse。
我还认为我的连接 URL 语法可能存在问题。 “db”是连接的名称吗?或者架构?我的本地主机后面需要端口号吗?
最佳答案
也许你可以将其更改为
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/XX","root","yourpassword"
关于java - 准系统 mySQL java 连接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423104/