基本mySQL java连接器问题
我正在尝试设置一个mySQL连接器,我不确定出了什么问题。以下是错误:基本mySQL java连接器问题,java,mysql,ant,Java,Mysql,Ant,我正在尝试设置一个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
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非常缺乏经验。我不确定这两个“pathelement路径”是否都是必需的,或者其中一个是否有效。我想知道我是否需要为了这一个罐子而求助于eclipse
我还认为我的连接url语法可能有问题。“db”是连接的名称吗?还是模式?我的本地主机后是否需要端口号?也许您可以将其更改为
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/XX","root","yourpassword"
我的问题是我没有正确地将jar添加到类路径中。 我在命令行中用-cp参数尝试了一下,结果成功了。我觉得很有帮助 如果您在命令控制台中执行“普通”操作,则需要在-cp或-classpath中指定JAR文件的路径 执行Java应用程序时的参数
java -cp .;/path/to/mysql-connector.jar com.example.YourClass
这个。只需将当前目录添加到类路径即可
这样它就可以找到com.example.YourClass和;是
Windows中的类路径分隔符。在Unix和克隆中:应该
被使用
在全局解决方案上无法提供帮助,但在url语法上:“db”是要连接的数据库,如果不提供端口号,则默认为3306(请参阅)。