当前位置: 技术问答>java相关
Java连接mySQL数据库的问题~~mm在线求救!
来源: 互联网 发布时间:2015-11-19
本文导语: 程序中有以下代码段: String url = "jdbc:mysql:notedb"; String username = "taba"; String password = ""; try{ Class.forName( "com.mysql.jdbc.Driver" ); ...
程序中有以下代码段:
String url = "jdbc:mysql:notedb";
String username = "taba";
String password = "";
try{
Class.forName( "com.mysql.jdbc.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to loead mysql/jdbc driver." );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
}
运行时抛出
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at MysqlTest.(MysqlTest.java:20)
at MysqlTest.main(MysqlTest.java:45)
如果把url改成 "jdbc:mysql://localhost/notedb",则抛出
Exception in thread "main" java.lang.ExceptionInInitializerError
偶好笨,搞了一晚上这点小问题也没解决,只好求助大家了.
String url = "jdbc:mysql:notedb";
String username = "taba";
String password = "";
try{
Class.forName( "com.mysql.jdbc.Driver" );
connection = DriverManager.getConnection(
url, username, password );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to loead mysql/jdbc driver." );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
}
运行时抛出
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at MysqlTest.(MysqlTest.java:20)
at MysqlTest.main(MysqlTest.java:45)
如果把url改成 "jdbc:mysql://localhost/notedb",则抛出
Exception in thread "main" java.lang.ExceptionInInitializerError
偶好笨,搞了一晚上这点小问题也没解决,只好求助大家了.
|
不是字符串不正确,我也遇到过这样的问题,就是你的mySQL jdbc驱动包没有放对,你用什么工具开发的,jbuilder?
|
localhost:3306
试试看?
试试看?
|
Class.forName( "com.mysql.jdbc.Driver" )
你的驱动名肯定定错了,!!,才会有SQLException: No suitable driver异常,
另,不知你的class path是怎写的,如你下驱动,应把它配好,
并import必要的类,
在装载类时最好用好newInstance()来实例化,.
你的驱动名肯定定错了,!!,才会有SQLException: No suitable driver异常,
另,不知你的class path是怎写的,如你下驱动,应把它配好,
并import必要的类,
在装载类时最好用好newInstance()来实例化,.
|
你的jdbc驱动大概没包进来吧,因为它找不到你的JDBC驱动
|
1、确定驱动在classpath,可在www.mysql.com下载。
mysql-connector-j-2.0.13-bin.jar
2、A simple connection example looks like:
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=blah&password=blah");
3、如果下载了mysql-connector-java-2.0.14.zip解压之后
看看里面的README很有用的。
mysql-connector-j-2.0.13-bin.jar
2、A simple connection example looks like:
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=blah&password=blah");
3、如果下载了mysql-connector-java-2.0.14.zip解压之后
看看里面的README很有用的。
|
more information !!!
this mistake is maybe in the function of main() .
this mistake is maybe in the function of main() .
|
请问你用的什么 服务器,什么驱动程序
|
你把驱动放在那了?classpath指向了么?
最简单的就是把驱动放在%java_home%/jre/lib/ext下
扩展包java自动找到。
最简单的就是把驱动放在%java_home%/jre/lib/ext下
扩展包java自动找到。