一个数据库连接小问题
package test;import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class DbTest {
public static void main(String args[])
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbTest.class.getName()).log(Level.SEVERE, null, ex);
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&pass=1234"); } catch (SQLException ex) {
Logger.getLogger(DbTest.class.getName()).log(Level.SEVERE, null, ex);
}
try {
stmt = (Statement) conn.createStatement();
rs = (ResultSet) stmt.executeQuery("SELECT * FROM student");
while(rs.next())
{
System.out.println("姓名:"+rs.getString("name")+"---号码:"+rs.getString("number"));
}
} catch (SQLException ex) {
Logger.getLogger(DbTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//数据库 test 用户名 root 密码 1234 为什么会报这样的信息
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2748)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at test.DbTest.main(DbTest.java:34)
Exception in thread "main" java.lang.NullPointerException
at test.DbTest.main(DbTest.java:39)