随笔-0  评论-15  文章-10  trackbacks-0

  1 package cn.zbvc.java;
  2 import java.io.InputStream;
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 import java.util.Properties;
  9 
 10 
 11 public class JDBConnection {
 12     public Connection conn = null// 声明Connection对象的实例
 13     public Statement stmt = null// 声明Statement对象的实例
 14     public ResultSet rs = null// 声明ResultSet对象的实例
 15     
 16     private static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//定义保存数据库驱动的变量
 17     private static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DB_ATM";
 18     private static String dbUser = "sa";
 19     private static String dbPwd = "sa";
 20 
 21     public JDBConnection(String propertyFileName) {// 带属性文件名的构造方法
 22         Properties prop = new Properties();// 属性集合对象
 23         InputStream is = null;
 24         try {
 25             is = JDBConnection.class.getClassLoader().getResourceAsStream(
 26                     propertyFileName);// 属性文件输入流
 27             // is = new FileInputStream("src/" + propertyFileName);
 28             prop.load(is);// 将属性文件流装载到Properties对象中
 29             is.close();// 关闭流
 30             dbClassName = prop.getProperty("dbClassName");
 31             dbUrl = prop.getProperty("dbUrl");
 32             dbUser = prop.getProperty("dbUser");
 33             dbPwd = prop.getProperty("dbPwd");
 34         } catch (Exception e) {
 35             System.out.println("属性文件  " + propertyFileName + " 打开失败!");
 36         }
 37         try {
 38 
 39             Class.forName(dbClassName);// 1.注册驱动
 40         } catch (ClassNotFoundException e) {
 41             e.printStackTrace();
 42         }
 43     }
 44 
 45     public JDBConnection() {// 默认的不带参数的构造函数
 46         try {
 47 
 48             Class.forName(dbClassName);// 1.注册驱动
 49         } catch (ClassNotFoundException e) {
 50             e.printStackTrace();
 51         }
 52     }
 53 
 54     public static Connection getConnection() {
 55         Connection conn = null;
 56         try {
 57             // Class.forName(dbClassName);// 1.注册驱动
 58             conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//2.建立与数据库的链接
 59         } catch (Exception ee) {
 60             ee.printStackTrace();
 61         }
 62         if (conn == null) {
 63             System.err
 64                     .println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
 65                             + dbClassName
 66                             + "\r\n链接位置:"
 67                             + dbUrl
 68                             + "\r\n用户/密码"
 69                             + dbUser + "/" + dbPwd);
 70         }
 71         return conn;
 72     }
 73 
 74     /*
 75      * 功能:执行查询语句
 76      */
 77     public ResultSet executeQuery(String sql) {
 78         try { // 捕捉异常
 79             conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
 80             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,//3.创建语句
 81                     ResultSet.CONCUR_READ_ONLY);
 82             rs = stmt.executeQuery(sql);//4.执行查询
 83         } catch (SQLException ex) {
 84             System.err.println(ex.getMessage()); // 输出异常信息
 85         }
 86         return rs; // 返回结果集对象 5.结果处理
 87     }
 88 
 89     /*
 90      * 功能:执行更新操作
 91      */
 92     public int executeUpdate(String sql) {
 93         int result = 0// 定义保存返回值的变量
 94         try { // 捕捉异常
 95             conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
 96             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
 97                     ResultSet.CONCUR_READ_ONLY);
 98             result = stmt.executeUpdate(sql); // 执行更新操作
 99         } catch (SQLException ex) {
100             result = 0// 将保存返回值的变量赋值为0
101         }
102         return result; // 返回保存返回值的变量
103     }
104 
105     /*
106      * 功能:关闭数据库的连接
107      */
108     public void close() {//6.释放资源
109         try { // 捕捉异常
110             try {
111                 if (rs != null) { // 当ResultSet对象的实例rs不为空时
112                     rs.close(); // 关闭ResultSet对象
113                 }
114             } finally {
115                 try {
116                     if (stmt != null) { // 当Statement对象的实例stmt不为空时
117                         stmt.close(); // 关闭Statement对象
118                     }
119                 } finally {
120                     if (conn != null) { // 当Connection对象的实例conn不为空时
121                         conn.close(); // 关闭Connection对象
122                     }
123                 }
124             }
125         } catch (Exception e) {
126             e.printStackTrace(System.err); // 输出异常信息
127         }
128     }
129 
130 }
131 
132 /*
133  *属性文件
134 dbClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver
135 dbClassName2=com.mysql.jdbc.Driver
136 dbPwd=sa
137 dbPwd2=root
138 dbUrl=jdbc\:microsoft\:sqlserver\://localhost\:1433;DatabaseName\=DB_ATM
139 dbUrl2=jdbc\:mysql\://localhost\:3306/db_atm
140 dbUser=sa
141 dbUser2=root
142  **/
posted on 2011-07-10 10:06 吕鹏-Roc 阅读(2179) 评论(0)  编辑  收藏 所属分类: Java常用代码

只有注册用户登录后才能发表评论。


网站导航: