读属性文件,操作数据库
2005-02-23 15:59
344 查看
读属性文件,操作数据库
/*
* 创建日期 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=
/*
* 创建日期 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=
相关文章推荐
- 使用properties属性文件做连接数据库的操作代码
- QT sqlite3数据库读取、容器操作、文件读写——学习笔记。
- 还原bak后缀的数据库文件如何操作
- C# 能连接4种数据库(外加文件操作)DatabaseHelp 类
- Linux文件基本属性和操作
- 操作、压缩MDB文件;使用ADO读写Mysql数据库;建立数据库和表的VBS脚本代码(2013-08-26 11:39:34)
- 无法升级数据库 'SchoolPlatForm1',因为它是只读的,或者具有只读文件。请将数据库或文件设为可写,然后重新运行恢复操作。 (Microsoft SQL Server,错误: 3415)
- 解决修改properties 属性文件存在缓存问题,附带操作properties文件工具类
- 基于Java的XML文件模拟数据库进行增删改查操作
- Linux文件系统文件属性及目录操作函数
- Java本地文件操作(四)文件属性的设置
- (转)java中对于文件属性的一些操作
- BackgroundWorker 组件用来执行诸如数据库事务、文件下载等耗时的异步操作
- 数据库操作文件 具有极高的重用价值
- 属性文件转码操作
- 使用chmod改变文件属性简单操作g
- C#操作mdb数据库文件
- access数据库的一些少用操作,ASP,创建数据库文件,创建表,创建字段,ADOX
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- Hibernate之*hbm.xml文件和实体类操作数据库实现插入和查询