java_JDBC连接数据库

  1. import java.sql.Connection;  
  1. import java.sql.DriverManager;  
  1. import java.sql.PreparedStatement;  
  1. import java.sql.ResultSet;  
  1. import java.sql.ResultSetMetaData;  
  1. import java.sql.SQLException;  
  1. import java.util.ArrayList;  
  1. import java.util.HashMap;  
  1. import java.util.List;  
  1. import java.util.Map;  
  1.   
  1. public class ConnectionDB {  
  1.   
  1.     /** 
  1.      * 数据库驱动类名称 
  1.      */  
  1.     private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
  1.   
  1.     /** 
  1.      * 连接字符串 
  1.      */  
  1.     private static final String URLSTR = "jdbc:sqlserver://localhost:1433; databaseName=Northwind";  
  1.   
  1.     /** 
  1.      * 用户名 
  1.      */  
  1.     private static final String USERNAME = "sa";  
  1.   
  1.     /** 
  1.      * 密码 
  1.      */  
  1.     private static final String USERPASSWORD = "111111";  
  1.   
  1.     /** 
  1.      * 创建数据库连接类 
  1.      */  
  1.     private Connection connnection = null;  
  1.   
  1.     private PreparedStatement preparedStatement = null;  
  1.   
  1.     /** 
  1.      * 创建结果集对象 
  1.      */  
  1.     private ResultSet resultSet = null;  
  1.   
  1.     static {  
  1.         try {  
  1.             // 加载数据库驱动程序  
  1.             Class.forName(DRIVER);  
  1.         } catch (ClassNotFoundException e) {  
  1.             System.out.println("加载驱动错误");  
  1.             System.out.println(e.getMessage());  
  1.         }  
  1.     }  
  1.   
  1.     /** 
  1.      * 建立数据库连接 
  1.      *  
  1.      * @return 
  1.      */  
  1.     public Connection getConnection() {  
  1.         try {  
  1.             // 获取连接  
  1.             connnection = DriverManager.getConnection(URLSTR, USERNAME,  
  1.                     USERPASSWORD);  
  1.         } catch (SQLException e) {  
  1.             System.out.println(e.getMessage());  
  1.         }  
  1.         return connnection;  
  1.     }  
  1.   
  1.     public int executeUpdate(String sql, Object[] params) {  
  1.         int affectedLine = 0;  
  1.         try {  
  1.             connnection = this.getConnection();  
  1.             preparedStatement = connnection.prepareStatement(sql);  
  1.             for (int i = 0; i < params.length; i++) {  
  1.                 preparedStatement.setObject(i + 1, params[i]);  
  1.             }  
  1.   
  1.             affectedLine = preparedStatement.executeUpdate();  
  1.   
  1.         } catch (SQLException e) {  
  1.             System.out.println(e.getMessage());  
  1.         } finally {  
  1.             closeAll();  
  1.         }  
  1.         return affectedLine;  
  1.     }  
  1.       
  1.   
  1.     private ResultSet executeQueryRS(String sql, Object[] params) {  
  1.         try {  
  1.             connnection = this.getConnection();  
  1.             preparedStatement = connnection.prepareStatement(sql);  
  1.             for (int i = 0; i < params.length; i++) {  
  1.                 preparedStatement.setObject(i + 1, params[i]);  
  1.             }  
  1.   
  1.             resultSet = preparedStatement.executeQuery();  
  1.   
  1.         } catch (SQLException e) {  
  1.             System.out.println(e.getMessage());  
  1.         }   
  1.           
  1.         return resultSet;  
  1.     }  
  1.       
  1.     /** 
  1.      * 获取结果集,并将结果放在List中 
  1.      * @param sql SQL语句 
  1.      * @return List结果集 
  1.      */  
  1.     public List excuteQuery(String sql, Object[] params) {   
    1.         ResultSet rs = executeQueryRS(sql,params);  
    1.         ResultSetMetaData rsmd = null;  
    1.         int columnCount = 0;  
    1.         try {  
    1.             rsmd = rs.getMetaData();  
    1.             columnCount = rsmd.getColumnCount();  
    1.         } catch (SQLException e1) {  
    1.             System.out.println(e1.getMessage());  
    1.         }  
    1.           
    1.         List list = new ArrayList();  
      1.           
      1.         try {  
      1.             while(rs.next()) {  
      1.                 Map map = new HashMap();  
      1.                 for(int i = 1; i <= columnCount; i++) {  
      1.                     map.put(rsmd.getColumnLabel(i), rs.getObject(i));                 
      1.                 }  
      1.                 list.add(map);  
      1.             }  
      1.         } catch (SQLException e) {  
      1.             System.out.println(e.getMessage());  
      1.         } finally {  
      1.             closeAll();  
      1.         }  
      1.           
      1.         return list;  
      1.     }  
      1.   
      1.     private void closeAll() {  
      1.   
      1.         if (resultSet != null) {  
      1.             try {  
      1.                 resultSet.close();  
      1.             } catch (SQLException e) {  
      1.                 System.out.println(e.getMessage());  
      1.             }  
      1.         }  
      1.   
      1.         if (preparedStatement != null) {  
      1.             try {  
      1.                 preparedStatement.close();  
      1.             } catch (SQLException e) {  
      1.                 System.out.println(e.getMessage());  
      1.             }  
      1.         }  
      1.           
      1.         if (connnection != null) {  
      1.             try {  
      1.                 connnection.close();  
      1.             } catch (SQLException e) {  
      1.                 System.out.println(e.getMessage());  
      1.             }  
      1.         }  
      1.     }  
      1. }  

      你可能感兴趣的:(Java,object,jdbc,string,数据库驱动程序,数据库)