package jp.co.greenblue.ecodasweb.dao;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import jp.co.greenblue.ecodasweb.dao.impl.DBUtilImpl;
import jp.co.greenblue.ecodasweb.util.JavaSqlTypeConverter;
public abstract class BaseDao {
// Sql文
private String sqlString = null;
// データベースの接続
private Connection connection = null;
protected ResultSet resultSet=null;
private PreparedStatement preparedStatement = null;
// prepared statement parameters
private Map<Integer, Object> parameters = new HashMap<Integer, Object>();
private Map<Integer, Integer> outParameters = new HashMap<Integer, Integer>();
public void openConnect()throws SQLException{
DBUtil dbUtil=new DBUtilImpl();
this.connection=dbUtil.getConnection();
}
public void closeConnect() throws SQLException{
if(this.connection!=null || !this.connection.isClosed())this.connection.close();
}
// @SuppressWarnings("unchecked")
// public static BaseDao getSimpleDBAccess() {
// String dataBaseName;
// String serverIp;
// String serverPort;
// String dataBaseObject;
// String userName;
// String passWord;
// String temp;
// String[] useConnect = null;
// Map<String, String[]> conTypes = new HashMap<String, String[]>();
// try {
// String path = CommonData.getAppPath(AppMain.class);
// SAXBuilder sb = new SAXBuilder();
// Document doc;
// doc = sb.build(path + "/WebContent/META-INF/context.xml");
// Element ele = doc.getRootElement();
// List<Element> eles = ele.getChildren("Resource");
// for (Element each : eles) {
// temp = each.getAttributeValue("name");
// dataBaseName = temp.substring(temp.indexOf("/") + 1);
// userName = each.getAttributeValue("username");
// passWord = each.getAttributeValue("password");
// temp = each.getAttributeValue("url");
// temp = temp.substring(temp.indexOf("@") + 1);
// serverIp = temp.substring(0, temp.indexOf(":"));
// serverPort = temp.substring(temp.indexOf(":") + 1, temp
// .lastIndexOf(":"));
// dataBaseObject = temp.substring(temp.lastIndexOf(":") + 1);
// String[] oneConnect = { serverIp, dataBaseObject, serverPort,
// userName, passWord };
// conTypes.put(dataBaseName, oneConnect);
// }
// useConnect =conTypes.get(CommonData.dbType.getDataBaseName());
//
// return DBAccess.getDBAccess(useConnect[0], useConnect[1], Integer
// .parseInt(useConnect[2]), useConnect[3], useConnect[4]);
// } catch (JDOMException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
// }
/**
*
* @throws SQLException
*/
public void rollbackTransaction() throws SQLException {
connection.rollback();
connection.setAutoCommit(true);
}
/**
*
* @throws SQLException
*/
public void commitTransaction() throws SQLException {
connection.commit();
connection.setAutoCommit(true);
}
/**
* @return 行数
* @throws SQLException
*/
public int executeNoneQuery() throws SQLException {
int count;
preparedStatement = connection.prepareStatement(sqlString,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// statement = connection.prepareStatement(sqlString,1005,1008);
prepareStatement();
count = preparedStatement.executeUpdate();
preparedStatement.close();
return count;
}
/**
*
* @return
* @throws SQLException
*/
public int executeNoneQueryProcedure() throws SQLException {
preparedStatement = connection.prepareCall(sqlString,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
prepareStatement();
registerOutParameters();
return preparedStatement.executeUpdate();
}
public int[] executeBatch(List<String> sqlList) throws SQLException {
Statement batchStatement = connection.createStatement();
for (String sql : sqlList) {
batchStatement.addBatch(sql);
}
int[] re = batchStatement.executeBatch();
batchStatement.clearBatch();
batchStatement.close();
return re;
}
/**
* 出力パラメータを取得する
*
* @param parameterIndex
* パラメータ
* @return パラメータの値
* @throws SQLException
*/
public Object getOutParameters(int parameterIndex) throws SQLException {
return ((CallableStatement) preparedStatement).getObject(parameterIndex);
}
/**
*
* @return
* @throws SQLException
*/
public ResultSet getQueryProcedureResultSet() throws SQLException {
preparedStatement = connection.prepareCall(sqlString,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
prepareStatement();
registerOutParameters();
return preparedStatement.executeQuery();
}
/**
* データを取得する
*
* @return false: データを取得するが失敗です true:データを取得するが成功です
* @throws ClassNotFoundException
* @throws SQLException
*/
public ResultSet getQueryResultSet() throws SQLException {
preparedStatement = connection.prepareStatement(sqlString,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
prepareStatement();
return preparedStatement.executeQuery();
}
/**
*
* @param parameterIndex
* @param sqlType
*/
public void setOutParameter(int parameterIndex, int sqlType) {
this.outParameters.put(parameterIndex, sqlType);
}
/**
*
* @param parameterIndex
* @param parameter
*/
public void setParameter(int parameterIndex, Object parameter) {
this.parameters.put(parameterIndex, parameter);
}
/**
*
* @param parameters
*/
public void setParameters(Object... parameters) {
int i = 1;
for (Object parameter : parameters) {
this.parameters.put(i++, parameter);
}
}
/**
*
* @throws SQLException
*/
private void registerOutParameters() throws SQLException {
if (!outParameters.isEmpty()) {
Set<Integer> keySet = outParameters.keySet();
for (int parameterIndex : keySet) {
int sqlType = outParameters.get(parameterIndex);
((CallableStatement) preparedStatement).registerOutParameter(
parameterIndex, sqlType);
}
}
}
/**
*
* @throws SQLException
*/
public void closeStatement() throws SQLException {
preparedStatement.close();
}
/**
* @param sqlString
* the sqlString to set
*/
public void setSqlString(String sqlString) {
this.sqlString = sqlString;
this.parameters.clear();
this.outParameters.clear();
}
private void prepareStatement() throws SQLException {
if (!parameters.isEmpty()) {
Set<Integer> keySet = parameters.keySet();
for (int parameterIndex : keySet) {
Object parameter = parameters.get(parameterIndex);
int sqlType;
if(parameter==null)
sqlType=0;//add by zhangshiying 20081211(add Type is null)
else
sqlType = JavaSqlTypeConverter.getSqlType(parameter
.getClass().getName());
switch (sqlType) {
case java.sql.Types.NULL://add by zhangshiying 20081211(add Type is null)
preparedStatement.setObject(parameterIndex, (Object) null);
break;
case java.sql.Types.INTEGER:
preparedStatement.setInt(parameterIndex, (Integer) parameter);
break;
case java.sql.Types.VARCHAR:
preparedStatement.setString(parameterIndex, (String) parameter);
break;
case java.sql.Types.BIGINT:
preparedStatement.setLong(parameterIndex, (Long) parameter);
break;
case java.sql.Types.BOOLEAN:
preparedStatement.setBoolean(parameterIndex, (Boolean) parameter);
break;
case java.sq
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
自己搭的WEB项目简易流程源,希望各位有空帮忙看一下代码,提出你们的宝贵意见和建议,使这个流程和开发更为方便,本源码未使用SSH框架,但借鉴了STRUTS,SPRING里面的一些东西,设计模式也没有怎么用,希望各位能把这个WEB开发流程规范弄的更为简洁,开发流程清晰,简单明了。我的e-mail:jonsion@gmail.com希望能与各位交流,把您改好的代码发到我的邮箱,大家共同进步。我的QQ:309523830 加时注明我的名字:蒋新川 ,另有两个J2EE讨论群:4275710(还差两人),3667378(位置还很多:))希望能得到您的指点。 转载请注明:出自http://jonsion.javaeye.com,谢谢
资源推荐
资源详情
资源评论
收起资源包目录
自己搭的WEB项目简易流程源码 (286个子文件)
BaseDao.class 6KB
ActionServlet.class 4KB
UserDaoImpl.class 3KB
UserActionImpl.class 2KB
CommonData.class 2KB
CharacterEncodingFilter.class 2KB
ReturnPageEnum.class 2KB
DataBaseType.class 2KB
ActionTypeEnum.class 2KB
UserForm.class 2KB
DBUtilImpl.class 1KB
JavaSqlTypeConverter.class 1KB
UserServiceImpl.class 1KB
User.class 1KB
BaseAction.class 702B
UserDao.class 688B
UserAction.class 671B
BaseService.class 558B
UserService.class 531B
DatabaseAccessException.class 440B
AppMain.class 424B
LogicException.class 413B
BaseForm.class 356B
DBUtil.class 173B
.classpath 571B
org.eclipse.wst.common.component 539B
entries 997B
entries 812B
entries 799B
entries 699B
entries 688B
entries 656B
entries 645B
entries 564B
entries 560B
entries 558B
entries 547B
entries 543B
entries 532B
entries 529B
entries 528B
entries 526B
entries 515B
entries 511B
entries 487B
entries 412B
entries 411B
entries 409B
entries 406B
entries 406B
entries 398B
entries 392B
entries 392B
entries 391B
entries 390B
entries 388B
entries 385B
entries 385B
entries 377B
entries 371B
entries 371B
entries 367B
entries 361B
entries 357B
entries 289B
entries 279B
entries 255B
entries 251B
entries 251B
entries 249B
entries 246B
entries 242B
entries 241B
entries 241B
entries 241B
entries 240B
entries 237B
entries 232B
entries 231B
entries 231B
entries 231B
entries 227B
entries 225B
entries 222B
entries 221B
entries 220B
entries 219B
entries 219B
entries 218B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
format 2B
共 286 条
- 1
- 2
- 3
资源评论
- JYaung2013-12-02学习借鉴一下
jxjt_666
- 粉丝: 1
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于串口通信的光通信上位机,包括运动控制和通信协议
- 串口与以太网文件传送协议(或自定义控制协议)
- Qt开发windows系统安装教程与代码实例.txt
- QT6实现的附带文件传输协议的串口终端
- 一个串口通讯类和调用Demo 通过设置串口、设置串口自定义协议,可方便对串口发送数据与接收数据
- 华为OD模拟题及参考答案.仅供学习和模拟考试使用
- stm32f103c8t6基于modbus协议和使用串口读取温湿度
- 英雄联盟LOL金克斯4K电脑壁纸
- Microbrain道闸产品上位机,以MahApps库为基础搭建界面,集成了串口(UART)、CAN、WIFI通信,十六进制协议
- Android串口通讯, 支持发送数据回调, 支持并发处理, 自定义协议, CRC校验, 自动粘包, 自动去除冗余的干扰数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功