关于JDBC连接MYSQL的问题求高手解决啊
import java.sql.*;
public class R {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost/sql_test";
String userName = "root";
String password = "root";
Connection conn = null;
try {
System.out.println("第一次连接数据库之前");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("第一次连接数据库之后");
} catch(SQLException e) {
System.out.println("第一次连接数据库的过程中出现SQL异常");
}
if (conn==null)
System.out.println("第一次连接数据库失败");
else
System.out.println("第一次连接数据库成功");
try {
System.out.println("\n加载驱动器类之前");
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("加载驱动器类之后");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
conn = null;
System.out.println("\n第二次连接数据库之前");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("第二次连接数据库之后");
} catch(SQLException e) {
System.out.println("第一次连接数据库的过程中出现SQL异常");
}
if (conn==null)
System.out.println("第二次连接数据库失败");
else
System.out.println("第二次连接数据库成功");
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库连接时出现SQL异常");
}
}
}
结果为
第一次连接数据库之前
第一次连接数据库的过程中出现SQL异常
第一次连接数据库失败
加载驱动器类之前
加载驱动器类之后
第二次连接数据库之前
第一次连接数据库的过程中出现SQL异常
第二次连接数据库失败
Exception in thread "main" java.lang.NullPointerException
at R.main(R.java:47)
这是什么原因呢 求高手给解决啊
谢谢
连不上啊 怎么办 啊