首页 > 代码库 > Java_JDBC连接数据库

Java_JDBC连接数据库

  1 package com.accp.dao;  2 import java.sql.Connection;  3 import java.sql.DriverManager;  4 import java.sql.PreparedStatement;  5 import java.sql.ResultSet;  6 import java.sql.SQLException;  7   8   9 /** 10  * 持久类父类 11  * @author Administrator 12  * 13  */ 14 public abstract class BaseDao { 15     private static final String DB_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 16     private static final String DB_URL = "jdbc:sqlserver://localhost:1433;databasename=epet"; 17     private static final String DB_USER = "sa"; 18     private static final String DB_PWD = "sa"; 19      20     protected Connection conn = null; 21     protected PreparedStatement ps = null; 22     protected ResultSet rs = null; 23      24     static{ 25         //1、加载驱动类 26         try { 27             Class.forName(DB_DRIVER); 28         } catch (ClassNotFoundException e) { 29             // TODO Auto-generated catch block 30             e.printStackTrace(); 31         } 32     } 33      34     /** 35      * 打开连接方法 36      * @return 37      */ 38     protected void openConn(){ 39         //2、创建连接 40         try { 41             conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PWD); 42         } catch (SQLException e) { 43             // TODO Auto-generated catch block 44             e.printStackTrace(); 45         } 46     } 47      48     /** 49      * 执行增加、修改、删除 50      * @param sql 51      * @param paras 52      * @return 53      */ 54     // select count(*) from [master] where name=? and password=? 55     // Object[] objs = {"张三","123456"}; 56     protected int executeUpdate(String sql,Object...paras){ 57         int ret = -1; 58          59         try { 60             //打开连接 61             openConn(); 62             //获取执行SQL对象 63             ps = conn.prepareStatement(sql); 64             //给sql参数赋值 65             if(paras != null){ 66                 for (int i = 0; i < paras.length; i++) { 67                     ps.setObject(i+1, paras[i]); 68                 } 69             } 70              71             //执行SQL  72             ret = ps.executeUpdate(); 73         } catch (SQLException e) { 74             // TODO Auto-generated catch block 75             e.printStackTrace(); 76         } finally{ 77             //关闭资源 78             closeConn(); 79         } 80          81         return ret; 82     } 83  84     /** 85      * 执行所有的查询 86      * @param sql 87      * @param paras 88      */ 89     protected void executeQuery(String sql,Object...paras){         90         try { 91             //打开连接 92             this.openConn(); 93             //获取执行SQL对象 94             ps = conn.prepareStatement(sql); 95             if(paras != null){ 96                 for (int i = 0; i < paras.length; i++) { 97                     ps.setObject(i+1, paras[i]); 98                 } 99             }100             //执行SQL 101             rs = ps.executeQuery();102         } catch (SQLException e) {103             // TODO Auto-generated catch block104             e.printStackTrace();105         }106     }107     108     /**109      * 释放资源110      * @param conn111      * @param stmt112      * @param rs113      */114     protected void closeConn(){115         try {116             if(rs != null){117                 rs.close();118             }119             if(ps != null){120                 ps.close();121             }122             if(conn != null){123                 conn.close();124             }125         } catch (SQLException e) {126             // TODO Auto-generated catch block127             e.printStackTrace();128         }129     }130 }

 

Java_JDBC连接数据库