Posted on 2008-11-24 19:38 
JAVAamateur 阅读(278) 
评论(0)  编辑  收藏  
			
			
		 
		package XXX;
import java.sql.*;
public class JDBCTest {
 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("com.mysql.jdbc.Driver");
  System.out.println("加载驱动器类之后");
 }
 catch(ClassNotFoundException e){
  System.out.println("加载驱动器类时出现异常");
 } 
 
 
 try{
  System.out.println("第二次连接数据库之前");
  conn=DriverManager.getConnection(url,userName,password);
  System.out.println("第二次连接数据库之后");
 }catch(SQLException e){
  System.out.println("第二次连接数据库的过程中出现了SQL异常");
 }