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

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

package 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)


------解决方案--------------------
用getConnection带用户名密码的那个重载方法
------解决方案--------------------
此处有问题:Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test");
你的数据库的用户名和密码呢?