日期:2014-05-20  浏览次数:22149 次

用Java连接mysql数据库出问题了?求大虾指点........
我的代码:
package connect;

import java.sql.*;

public class Get_message {
public String name;
public String datetime;
public int no;
public String content;
public static void main(String[] args) throws Exception{

Connection con;
Statement stmt;
ResultSet rs;
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/message","User=root","Password=123");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT no,name,datetime,content FROM message_save ");
while(rs.next())
{
System.out.println(rs.getString("name"));
rs.getString("datetime");
rs.getInt("no");
rs.getString("content");
}
}
}

错误提示代码:

Exception in thread "main" java.sql.SQLException: Access denied for user 'User=root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4101)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1300)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at connect.Get_message.main(Get_message.java:16)

我的用户名和密码绝对没有错误,如果有大虾知道问题出在哪里,请教教小弟,感激不尽啊!

------解决方案--------------------
DriverManager.getConnection("jdbc:mysql://localhost:3306/message","User=root","Password=123");

改为:

DriverManager.getConnection("jdbc:mysql://localhost:3306/message","root","123");