讀屬性文件,操作數據庫

讀屬性文件,操作數據庫

/*
 * 創建日期 2005-1-13
 *
 * TODO 要更改此生成的文件的模板,請轉至
 * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
package com.wen.db;

import java.sql.*;
import com.wen.work.ConfigResource;

/**
 * @author explorerwen
 *
 * TODO 要更改此生成的類型註釋的模板,請轉至 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
public final class DbConnection {
 private static DbConnection DBconn = null;
 private Statement stmt;
 private ResultSet rset;
 private Connection conn;
 private DbConnection() {
  stmt = null;
  rset = null;
  conn = null;
 }

 public synchronized static DbConnection getDbConnection() {
  if (DBconn == null)
   DBconn = new DbConnection();
  return DBconn;
 }

 /**
  * 返回數據庫的連接
  *
  * @param realPath
  *            配置文件的絕對路徑
  * @return
  * @throws Exception
  */
 public Connection getConnection(String realPath) throws Exception {
  try {
   ConfigResource.getConfigResource().setRealPath(realPath);
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   conn = DriverManager.getConnection(ConfigResource
     .getConfigResource().getProperty("DB.Url"), ConfigResource
     .getConfigResource().getProperty("DB.User"), ConfigResource
     .getConfigResource().getProperty("DB.Pass"));
   conn.setAutoCommit(false);
   return conn;
  } catch (SQLException ex) {
   ex.printStackTrace();
   return conn = null;
  } catch (Exception ex) {
   ex.printStackTrace();
   return conn = null;
  }

 }

 /**
  * 提交conn
  *
  * @throws SQLException
  */
 public void commit() throws SQLException {
  conn.commit();
 }

 /**
  * 回滾conn
  *
  * @throws SQLException
  */
 public void rollback() throws SQLException {
  conn.rollback();
 }

 /**
  * 查詢,返回ResultSet
  *
  * @param query
  * @return
  * @throws SQLException
  */
 public ResultSet executeQuery(String query) throws SQLException {
  try {
   stmt = conn.createStatement();
   rset = stmt.executeQuery(query);
  } catch (SQLException e) {
   System.out.println(query);
   throw new SQLException(e.toString());
  }
  return rset;
 }

 /**
  * 更新,返回更新的記錄數
  *
  * @param query
  * @return
  * @throws SQLException
  */
 public int executeUpdate(String query) throws SQLException {
  stmt = conn.createStatement();
  int count = stmt.executeUpdate(query);
  if (stmt != null)
   stmt.close();
  return count;
 }
 /**
  * 返回記錄集的列數
  * @return
  * @throws SQLException
  */
 public int getColumnCount() throws SQLException {
  ResultSetMetaData rsmd = rset.getMetaData();
  return rsmd.getColumnCount();
 }
 /**
  * 根據索引返回列名
  * @param index
  * @return
  * @throws SQLException
  */
 public String getColumnName(int index) throws SQLException {
  ResultSetMetaData rsmd = rset.getMetaData();
  return rsmd.getColumnName(index);
 }
 /**
  * 根據索引返回列值
  * @param index
  * @return
  * @throws SQLException
  */
 public String getData(int index) throws SQLException {
  return rset.getString(index).trim();
 }
 /**
  * 根據列名返回列值
  * @param columnName
  * @return
  * @throws SQLException
  */
 public String getData(String columnName) throws SQLException {
  return rset.getString(columnName).trim();
 }
 /**
  * 查找記錄集的下一行
  * @return
  * @throws SQLException
  */
 public boolean next() throws SQLException {
  return rset.next();
 }
 /**
  * 關閉記錄集
  * @throws SQLException
  */
 public void rsclose() throws SQLException {
  if (stmt != null)
   stmt.close();
  if (rset != null)
   rset.close();
 }
 /**
  * 關閉記錄集和連接
  * @throws SQLException
  */
 public void close() throws SQLException {

  if (stmt != null)
   stmt.close();
  if (rset != null)
   rset.close();
  if (conn != null)
   conn.close();
 }
 /**
  * 關閉記錄集和連接
  */
 protected void finalize() throws Throwable {
  close();
 }

}

================================================================
/*
 * 創建日期 2005-1-15
 *
 * TODO 要更改此生成的文件的模板,請轉至
 * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
package com.wen.db;

import java.util.*;
import java.sql.*;
/**
 * @author explorerwen
 *
 * TODO 要更改此生成的類型註釋的模板,請轉至
 * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
public class DbOpr {
 private static DbOpr dbOpr = null;
 private DbOpr(){
  
 }
 public static DbOpr getDbOpr(){
  if(dbOpr == null)
   dbOpr = new DbOpr();
  return dbOpr;
 }
 /**
  * 執行查詢將結果集轉化爲HashMap返回
  * @param sqlStr 查詢語句
  * @param realPath 配置文件絕對路徑
  * @return HashMap
  * @throws Exception
  */
 public HashMap runQueryHm(String sqlStr,String realPath)throws Exception{
  DbConnection DbConn = DbConnection.getDbConnection();
  DbConn.getConnection(realPath);
  int colCount = 0;
  HashMap hm = new HashMap();
  try{
   if(DbConn.executeQuery(sqlStr) != null && DbConn.next()){
    colCount = DbConn.getColumnCount();
    for(int i=1;i<=colCount;i++){
     hm.put(DbConn.getColumnName(i),DbConn.getData(i));
    }
   }
   DbConn.close();
   return hm;
  }catch(SQLException ex){
   ex.printStackTrace();
   DbConn.close();
   return hm = null;   
  }catch(Exception ex){
   ex.printStackTrace();
   DbConn.close();
   return hm = null;
  }
 }
 /**
  * 執行查詢,將記錄集轉化爲Vector返回
  * @param sqlStr 查詢語句
  * @param realPath 配置文件絕對路徑
  * @return Vector
  * @throws Exception
  */
 public Vector runQueryVc(String sqlStr,String realPath)throws Exception{
  DbConnection DbConn = DbConnection.getDbConnection();
  DbConn.getConnection(realPath);
  int colCount = 0;
  Vector vc = new Vector();
  HashMap hm = new HashMap();
  try{
   if(DbConn.executeQuery(sqlStr)!=null){
    colCount = DbConn.getColumnCount();
    while(DbConn.next()){
     for(int i=1;i<colCount;i++){
      hm.put(DbConn.getColumnName(i),DbConn.getData(i));
     }
     vc.add(hm);
    }
   }
   DbConn.close();
   return vc;
  }catch(SQLException ex){
   ex.printStackTrace();
   DbConn.close();
   return vc = null;
  }catch(Exception ex){
   ex.printStackTrace();
   DbConn.close();
   return vc = null;
  } 
 }
 /**
  * 更新或者刪除成功返回true,否則返回false
  * @param sqlStr 更新或者刪除語句
  * @param realPath 配置文件絕對路徑
  * @return boolean
  * @throws Exception
  */
 public boolean runUpdate(String sqlStr,String realPath)throws Exception{
  DbConnection DbConn = DbConnection.getDbConnection();
  DbConn.getConnection(realPath);
  boolean flag = false;
  try{
   if(DbConn.executeUpdate(sqlStr)>0){
    DbConn.commit();
    flag = true;
   }else{
    DbConn.rollback();
    flag = false;
   } 
   DbConn.close();
   return flag;
  }catch(SQLException ex){
   ex.printStackTrace();
   DbConn.rollback();
   DbConn.close();
   return false;
  }catch(Exception ex){
   ex.printStackTrace();
   DbConn.rollback();
   DbConn.close();
   return false;   
  }
 }

}
===============================================================================
/*
 * 創建日期 2005-1-13
 *
 * TODO 要更改此生成的文件的模板,請轉至
 * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
package com.wen.work;

import java.util.Properties;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
 * @author Administrator
 *
 * TODO 要更改此生成的類型註釋的模板,請轉至
 * 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
 */
public final class ConfigResource {
 /**
  * 構造函數
  */
 private static ConfigResource config = null;
 private String realPath = null;
 private ConfigResource() {
 }
 /**
  * 返回實例
  * @return
  */
 public synchronized static ConfigResource getConfigResource(){
  if(config == null)
   config = new ConfigResource();
  return config;
 }
 /**
  * 設置絕對路徑
  * @param realPath
  */
 public void setRealPath(String realPath){
  this.realPath = realPath;
 }
 /**
  * 返回數據庫配置文件的屬性值
  * @param key 配置文件鍵值
  * @return
  * @throws Exception
  */
 public String getProperty(String key)throws Exception{
  if(realPath == null || realPath.equals(""))
   throw new Exception("請先設置realPath屬性");
  String path = realPath+"/WEB-INF/classes/com/wen/work/DBConfig.properties";
  try{
   InputStream in = new FileInputStream(path);
   Properties pop = new Properties();
   pop.load(in);
   return pop.getProperty(key);
  }catch(FileNotFoundException ex){
   ex.printStackTrace();
   return "文件沒有發現異常!";
  }catch(Exception ex){
   ex.printStackTrace();
   return "讀配置文件異常!";
  }  
 }
 public static void main(String []arg)throws Exception{
  System.out.println("3333");
 }

}
===========================================================================================
屬性文件DBConfig.properties格式:
#=========================================================
#  DB Config properties
#    explorerwen 2005-1-13
#---------------------------------------------------------
# 說明:
# DB.Name 數據庫名
# DB.User 用戶名
# DB.Pass 用戶密碼
#=========================================================

DB.Name=
DB.User=
DB.Pass=

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章