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

关于数据库的问题,急!
今天安装了MySQL数据库,然后学写了一个关于JDBC的程序(下载了JDBC关于MySQL的驱动,并且用Build Path加入到了项目中,但是运行程序出现了问题。源代码如下:

[code=Java][/codepackage buaa;import java.sql.*;

public class SimpleJdbc {

 public static void main(String[] args)

  throws SQLException,ClassNotFoundException{

  Class.forName("com.mysql.jdbc.Driver");

  System.out.println("Driver loaded");

   

  Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test");

  System.out.println("DateBase connected");

   

  Statement statement=connection.createStatement();

   

  ResultSet resultSet=statement.executeQuery("select firstName,mi,lastName from Student where lastName "

  +" ='Smith'");

  while(resultSet.next())

  System.out.println(resultSet.getString(1)+"\t"+

  resultSet.getString(2)+"\t"+resultSet.getString(3));

   

  connection.close();

 }

}]

 错误信息:

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2412)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at buaa.SimpleJdbc.main(SimpleJdbc.java:10)

------解决方案--------------------
DriverManager.getConnection(url, user, pwd);
------解决方案--------------------
嗯。。应该就是参数问题吧。。没用过LZ的那种参数组
------解决方案--------------------
Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test",username,password)
------解决方案--------------------
端口号都没设置,应该是端口号的问题,还有密码,用户名都没输入,Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:8080/数据库名",username,password); username 如果你没设置名字的话,应该为root,密码就是你登陆数据库的密码
------解决方案--------------------
就是没有用户名密码,当然用户名密码可以写在连接串上,不过用 DriverManager.getConnection(url, user, pwd); 比较简单。
------解决方案--------------------
问题1: Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test");