读属性文件,操作数据库
/*
* 创建日期 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=