最基本的JDBC连接代码,代码如下:

 

 
  
  1. package dao;  
  2.  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8.  
  9. public class BaseDao {  
  10.     public static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
  11.     public static final String url = "jdbc:sqlserver://127.0.0.1:1433;database=ebook";  
  12.     public static final String uid = "sa";  
  13.     public static final String pwd = "123456";  
  14.       
  15.     /**  
  16.      * 创建连接对象  
  17.      * @return  
  18.      */ 
  19.     public Connection getConnection(){  
  20.         Connection connection = null;  
  21.         try {  
  22.             Class.forName(driver);  
  23.             connection = DriverManager.getConnection(url,uid,pwd);  
  24.         } catch (ClassNotFoundException e) {  
  25.             e.printStackTrace();  
  26.         } catch (SQLException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.         return connection;  
  30.     }  
  31.       
  32.     /**  
  33.      * 关闭全部连接  
  34.      * @param connection  
  35.      * @param statement  
  36.      * @param result  
  37.      */ 
  38.     public void closeAll(Connection connection,PreparedStatement statement,ResultSet result){  
  39.         if (result != null){  
  40.             try {  
  41.                 result.close();  
  42.                 result = null;  
  43.             } catch (SQLException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.         if (statement != null){  
  48.             try {  
  49.                 statement.close();  
  50.                 statement = null;  
  51.             } catch (SQLException e) {  
  52.                 e.printStackTrace();  
  53.             }  
  54.         }  
  55.         try {  
  56.             if (connection != null && !(connection.isClosed())){  
  57.                 connection.close();  
  58.             }  
  59.         } catch (SQLException e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.     }  
  63.       
  64.     /**  
  65.      * 执行增、删、改操作  
  66.      * @param sql  
  67.      * @param param  
  68.      * @return  
  69.      */ 
  70.     public int executeQuery(String sql,String[] param){  
  71.         Connection connection = null;  
  72.         PreparedStatement statement = null;  
  73.         int num = 0;  
  74.         try {  
  75.             connection = this.getConnection();  
  76.             statement = connection.prepareStatement(sql);  
  77.             for (int i = 0 ; i < param.length ; i++){  
  78.                 statement.setString(i+1, param[i]);  
  79.             }  
  80.             num = statement.executeUpdate();  
  81.         } catch (SQLException e) {  
  82.             e.printStackTrace();  
  83.         } finally {  
  84.             this.closeAll(connection, statement, null);  
  85.         }  
  86.         return num;  
  87.     }