package com.easyjf.dbo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.easyjf.dbo.config.DBOConfig;
import com.easyjf.dbo.sql.*;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
/**
* 数据库处理引擎,实现DBObject对象到数据库的转换,完成具体的数据库操作
* @author 蔡世友
*
*/
public class EasyJDBEngine {
private final static Logger logger = Logger.getLogger(EasyJDBEngine.class);
private DataSource dataSource;
private ISqlQuery sqlQuery;
private static EasyJDBEngine engine;
public EasyJDBEngine(DataSource dataSource)
{
this(dataSource,new MySqlQuery());
}
public EasyJDBEngine(DataSource dataSource,ISqlQuery sqlQuery)
{
this.dataSource=dataSource;
this.sqlQuery=sqlQuery;
}
public static EasyJDBEngine getInstance()//数据库操作,将会修改为由配置文件中读取参数
{
if(engine==null){
logger.info("初始化数据库查询引擎!");
ISqlQuery dialect=null;
try{
dialect=(ISqlQuery)Class.forName(DBOConfig.getInstance().getDialect()).newInstance();
}
catch (Exception e)
{
logger.error("数据库类型(方言)配置错误,系统将使用默认MySQL数据类型方言!"+e);
}
if(dialect==null)dialect=new MySqlQuery();
engine=new EasyJDBEngine(DataSourceManager.getDataSource(),dialect);
}
return engine;
}
public Object uniqueResult(String sql)
{
Object ret=new Object();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(DBOConfig.getInstance().isShow_sql())System.out.println("EasyDBO:"+sql);
rs = ps.executeQuery();
if(rs.next())ret=rs.getObject(1);
}
catch(Exception e)
{
logger.error("数据查询错误:"+e+":sql="+sql);
}
finally
{
try{
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
logger.error("释放数据库资源错误:"+e);
}
}
return ret;
}
public Object uniqueResult(String sql,Collection params)
{
Object ret=new Object();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(DBOConfig.getInstance().isShow_sql())System.out.println("EasyDBO:"+sql);
JDBCUtil.setQueryParams(params,ps);
rs = ps.executeQuery();
if(rs.next())ret=rs.getObject(1);
}
catch(Exception e)
{
logger.error("数据查询错误:"+e+":sql="+sql);
}
finally
{
try{
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
logger.error("释放数据库资源错误:"+e);
}
}
return ret;
}
public DBObject get(String sql)
{
return get(sql,null);
}
public DBObject get(String sql,Collection params)//通过完整的sql语句读取得单个对象
{
DBObject obj=new DBObject();
List list = new ArrayList();
list=query(sql,params,0,1);
if(list!=null && (list.size()>0))
{
obj=(DBObject)list.get(0);
//obj.setIdValue(obj.getValue().get(obj.getTable().getId()));
}
/*
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(DBOConfig.getInstance().isShow_sql())System.out.println("EasyDBO:"+sql);
JDBCUtil.setQueryParams(params,ps);
rs = ps.executeQuery();
list = JDBCUtil.resultSet2List(rs);
if(list!=null && (list.size()>0))
{
obj.setValue((Map)list.get(0));
obj.setTable(new DBTable());
//obj.setIdValue(obj.getValue().get(obj.getTable().getId()));
}
else//没有查询到数据
{
return null;
}
}
catch(Exception e)
{
logger.error("数据查询错误:"+e+":sql="+sql);
}
finally
{
try{
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
logger.error("释放数据库资源错误:"+e);
}
}*/
return obj;
}
public DBObject get(DBObject obj,Object idvalue)//通过ID值读取数据表数据
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
if(obj==null)return null;
String sql=sqlQuery.getQuerySql(obj);
if(sql.equals(""))
{
logger.error("找不到查询参数!");
return null;
}
List list = new ArrayList();
try
{
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
if(DBOConfig.getInstance().isShow_sql())System.out.println("EasyDBO:"+sql);
JDBCUtil.setQueryParam(idvalue,1,ps);
rs = ps.executeQuery();
list = JDBCUtil.resultSet2List(rs);
if(list!=null && (list.size()>0))
{
obj.setValue((Map)list.get(0));
obj.setIdValue(obj.getValue().get(obj.getTable().getId()));
}
else//没有查询到数据
{
return null;
}
}
catch(Exception e)
{
logger.error("数据查询错误:"+e+":sql="+sql);
}
finally
{
try{
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
logger.error("释放数据库资源错误:"+e);
}
}
return obj;
}
public DBObject get(DBObject obj,String scope,Collection params)//通过ID值读取数据表数据
{
if(obj==null)return null;
List list = new ArrayList();
String sql=sqlQuery.getQuerySql(obj,scope);
list=query(sql,params,0,1);
if(list!=null && (list.size()>0))
{
DBObject dbo=(DBObject)list.get(0);
obj.setValue(dbo.getValue());
obj.setIdValue(obj.get(obj.getTable().getId()));
}
else//没有查询到数据
{
return null;
}
return obj;
}
public boolean add(DBObject obj)//添加一个对象
{
String sql=sqlQuery.getInsertSql(obj);
if(sql.equals(""))
{
logger.error("空的sql语句无法执行!");
return false;
}
Connection conn = null;
PreparedStatement ps = null;
boolean ret=false;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql);
if(DBOConfig.getInstance().isShow_sql())System.out.println("EasyDBO:"+sql);
if(obj.getValue()!=null)
JDBCUtil.setQueryParams(obj.getValue().values(), ps);
System.out.println(obj.getValue().size());
if(ps.executeUpdate()>0)ret=true;
}
catch(Exception e)
{
logger.error("添加数据错误:"+e+":sql="+sql);
}
finally
{
try{
if (ps != null)
ps.close();
if (conn != null)
conn.close();
}
catch(Exception e)
{
logger.error("释放数据库资源错误:"+e);
}
}
return ret;
}
public boolean update(DBObject obj)//修改一个对象
{
String sql=sqlQuery.getUpdateSql(obj);
if(sql.equals(""))
{
logger.error("空的sql语句无法执行!");
return false;
}
Connection conn = null;
PreparedStatement ps = null;
boolean ret=false;
try {
conn = dataSource.getConnection();
ps = conn.prepareStatement(sql);
if(DBOConfig.getInstance().isShow_sql())System.out
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
简易数据库关系映射框架EasyDBO.zip (122个子文件)
stylesheet.css 1KB
inherit.gif 57B
HibDbOperation.html 28KB
index-6.html 24KB
DBField.html 23KB
EasyJDBEngine.html 22KB
DBObject.html 21KB
DBTable.html 20KB
EasyJDB.html 20KB
IDbOperation.html 19KB
MSSqlServerQuery.html 17KB
MySqlQuery.html 17KB
DBOConfig.html 15KB
XMLConfigFactory.html 15KB
index-16.html 14KB
DBObject.html 14KB
IConfigFactory.html 13KB
DBMapping.html 13KB
ISqlQuery.html 13KB
DataSourceManager.html 13KB
HibIdGenerator.html 12KB
JDBCUtil.html 12KB
index-3.html 12KB
IdGenerator.html 11KB
index-14.html 10KB
index-8.html 10KB
DBOConfig.html 10KB
DBTable.html 10KB
HibernateSessionFactory.html 9KB
JDBOperation.html 9KB
package-summary.html 9KB
index-2.html 9KB
overview-tree.html 9KB
ISqlQuery.html 9KB
index-11.html 9KB
index-4.html 9KB
IIdGenerator.html 9KB
help-doc.html 9KB
package-use.html 9KB
IObject.html 9KB
index-10.html 9KB
DBField.html 8KB
constant-values.html 8KB
DBKey.html 8KB
package-tree.html 8KB
index-18.html 8KB
index-15.html 8KB
index-1.html 8KB
XMLConfigFactory.html 7KB
index-5.html 7KB
IConfigFactory.html 7KB
index-7.html 7KB
package-use.html 7KB
EasyJDBEngine.html 7KB
DBMapping.html 7KB
IDbOperation.html 7KB
IIdGenerator.html 7KB
EasyJDB.html 7KB
index-9.html 7KB
index-19.html 7KB
package-use.html 7KB
package-summary.html 7KB
package-summary.html 7KB
index-17.html 7KB
index-13.html 6KB
index-12.html 6KB
package-tree.html 6KB
package-tree.html 6KB
MSSqlServerQuery.html 6KB
HibernateSessionFactory.html 6KB
MySqlQuery.html 6KB
DataSourceManager.html 6KB
overview-summary.html 6KB
HibIdGenerator.html 6KB
HibDbOperation.html 6KB
JDBOperation.html 6KB
IdGenerator.html 6KB
IObject.html 6KB
JDBCUtil.html 5KB
DBKey.html 5KB
serialized-form.html 5KB
deprecated-list.html 5KB
allclasses-frame.html 3KB
allclasses-noframe.html 3KB
package-frame.html 3KB
package-frame.html 1KB
package-frame.html 1KB
overview-frame.html 1KB
index.html 922B
packages.html 696B
mysql-connector-java-3.1.11-bin.jar 430KB
log4j-1.2.9.jar 344KB
dom4j-1.6.jar 306KB
msbase.jar 278KB
jaxen-1.1-beta-4.jar 233KB
commons-beanutils.jar 184KB
commons-collections-2.1.1.jar 171KB
mssqlserver.jar 63KB
msutil.jar 58KB
EasyJDBEngine.java 13KB
共 122 条
- 1
- 2
资源评论
努力学习的小初雪
- 粉丝: 313
- 资源: 98
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tensorflow-gpu-2.5.3-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-gpu-2.2.1-cp35-cp35m-win-amd64.whl
- tensorflow-gpu-2.5.3-cp38-cp38-manylinux2010-x86-64.whl
- 2023-04-06-项目笔记 - 第一百三十五阶段 - 4.4.2.133全局变量的作用域-133 -2024.05.16
- Screenshot_20240516_224007.jpg
- IEEE802.3dj-800G DR4 & 1.6T DR8-welch-3dj-01a-230206 .pdf
- Matlab三维图绘制基础了解
- IEEE802.3dj-800GBASE FR4-welch-3dj-01a-2401 .pdf
- 实验3-拆弹专家-3023244203-刘宇翔
- base.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功