日期:2014-05-16  浏览次数:20817 次

java连接MYSQL的问题
下载了mysql,在Eclipse里想用代码连接这个数据库。
用MySQLWorkbench创建了一个表lktable
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:Mysql://localhost:3306/lktable";
String user = "root";
String password = "";
try {
Class.forName(driverName);
Connection connection = DriverManager.getConnection(url, user, password);
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

并且设置了 Classpath 
D:\程序开发\MySql\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar;.
请问为什么还是报一大堆错误!

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'lktable'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:920)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4000)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1285)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2186)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MytestReadDB.main(MytestReadDB.java:24)


------解决方案--------------------
java开发人员要学会排错啊,特别是这种那么简单的错误.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'lktable'

这里写得够清楚了
------解决方案--------------------
同楼上 , 应该是库名而不是表名 ! 
运行时先编译 ! 这很重要 ! 
我的 MySQL 的驱动程序是 “org.gjt.mm.mysql.Driver" ;
------解决方案--------------------
探讨
应该为:
String url = "jdbc:Mysql://localhost:3306/数据库名";
而不是表名。
如果楼主没有新建数据库的话,那这个表应该是建在test 数据库下了
所以应该改为:String url = "jdbc:Mysql://localhost:3306/test";

------解决方案--------------------
探讨
应该为:
String url = "jdbc:Mysql://localhost:3306/数据库名";
而不是表名。
如果楼主没有新建数据库的话,那这个表应该是建在test 数据库下了
所以应该改为:String url = "jdbc:Mysql://localhost:3306/test";