[code="java"]import java.sql.*;
public class MySQLtest{
Connection con = null;
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println ("数据库驱动加载成功!");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StudentSystem","ms","123456");
System.out.println ("数据库连接成功!");
}
catch (ClassNotFoundException e1) {
System.out.println ("数据库驱动加载失败!");
}catch(SQLException e2){
System.out.println ("数据库连接失败!");
}
return con;
}
public static void main(String[] args){
MySQLtest mysql = new MySQLtest();
mysql.getConnection();
try {
Statement sql = mysql.con.createStatement();
ResultSet result = sql.executeQuery("Select * from studentinformation");
while(result.next()){
System.out.print (result.getString(1)+" ");
System.out.print (result.getString(2)+" ");
System.out.print (result.getString(3)+" ");
System.out.print (result.getString(4)+" ");
System.out.print (result.getString(5)+" ");
System.out.print (result.getString(6)+" ");
System.out.println ();
}
result.close();
sql.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}[/code]
运行时候会出现错误:
数据库驱动加载成功!
数据库连接失败!
java.lang.NullPointerException
at MySQLtest.main(MySQLtest.java:24)
可能是连接数据库时候发生的错误,已经能够确定用户名密码正确,求解答,本人新手,如果问题低端请谅解。