用java连接mysql问题
代码如下:
package ch13;
import java.sql.*;
public class UseDriverManager {
public static void main(String args[]){
String url="jdbc:mysql://localhost/sql_test";
String userName="root";
String password="sinopatic";
Connection conn=null;
try{
System.out.println("Before the first time connect to Database");
conn=DriverManager.getConnection(url,userName,password);
System.out.println("After the first time connect to Database");
}
catch(SQLException e){
System.out.println("There are some problems occur when first connect to database");
}
if(conn==null)
System.out.println("First connection fail...");
else
System.out.println("First connection successful!");
try{
System.out.println("\n Before mount the driver");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("After mount the driver");
}
catch(ClassNotFoundException e){
System.out.println("There are some problems occur when mount the driver...");
}
try{
conn = null;
System.out.println("\n Before the sencond time connecting the the Database");
conn=DriverManager.getConnection(url,userName,password);
System.out.println("After the second time connect to the Database");
}
catch(SQLException e){
System.out.println("There are some problems occur when first connect to database");
}
if(conn==null)
System.out.println("Sencond connection fail...");
else
System.out.println("Second connection successful!");
try{
conn.close();
}
catch(SQLException e){
System.out.println("There are some problems occur when close the connection to the Database");
}
}
}
------------------------
启动mysql,在eclipse中运行,连接不上:
ClassNotFoundException
Insert SQLException
Exception in thread "main" java.lang.NullPointerException
at ch13.UseDriver.main(UseDriver.java:33)