public class BaseDao {
private static Log logger = LogFactory.getLog(BaseDao.class); // 查询数据
public void selectSql(String sql, Object[] obj) {
try {
PreparedStatement stmt = null;
Connection conn = null;
conn = ConnectionTools.getConn();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
int key = i + 1;
stmt.setObject(key, obj[i]);
}
}
stmt.executeUpdate();
conn.commit();
stmt.close();
} catch (Exception e) {
logger.error("查询出错:", e);
} } public void executeUpdate(String sql) {
executeUpdate(sql, null);
} public void executeUpdate(String sql, Object obj[]) {
try {
PreparedStatement stmt = null;
Connection conn = null;
conn = ConnectionTools.getConn();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
int temp = i + 1;
stmt.setObject(temp, obj[i]);
}
}
stmt.executeUpdate();
conn.commit();
stmt.close();
} catch (Exception e) {
logger.error("保存出错:", e);
} } public int addDate(String sql, Object[] obj) {
int key = 0;
try {
PreparedStatement stmt = null;
Connection conn = null;
conn = ConnectionTools.getConn();
conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
if (obj != null) {
for (int i = 0; i < obj.length; i++) {
int temp = i + 1;
stmt.setObject(temp, obj[i]);
}
}
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
key = rs.getInt(1);
}
conn.commit();
stmt.close();
} catch (Exception e) {
logger.info("保存出错:", e);
}
return key; } // 删除表
public void deleteTable(String tableName) {
StringBuffer dropTableSql = new StringBuffer();
dropTableSql.append("DROP TABLE IF EXISTS `" + tableName + "`");
executeUpdate(dropTableSql.toString());
} // 创建一个表
public void makeTableSql(String tableName, Map<String, String> cloumNames) {
StringBuffer createTableSQL = new StringBuffer(); createTableSQL.append("CREATE TABLE " + tableName + " (");
createTableSQL.append(" id int(11) NOT NULL AUTO_INCREMENT,");
Set<String> cs = cloumNames.keySet(); for (String e : cs) { String t = cloumNames.get(e); e = e.replaceAll("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]", ""); if (e.trim().isEmpty()) {
continue;
}
if (e.contains("detailurl")) { createTableSQL.append(e + " varchar(255) DEFAULT NULL,"); // } else if (t.length() > 10 && t.length() < 255) {
//
// createTableSQL.append(e + " varchar(255) DEFAULT NULL,");
//
} else {
createTableSQL.append(e + " text DEFAULT NULL,");
}
}
createTableSQL.append("TIME datetime NOT NULL , ");
createTableSQL.append(" PRIMARY KEY (id) , UNIQUE KEY (detailurl)");
createTableSQL.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
executeUpdate(createTableSQL.toString());
} // 插入数据
public void insertSql(String tableName, Map<String, String> cloumNames) {
StringBuffer insertSQL = new StringBuffer(); insertSQL.append("insert into " + tableName + " (");
Set<String> cs = cloumNames.keySet();
StringBuffer keys = new StringBuffer();
StringBuffer values = new StringBuffer();
for (String e : cs) { String value = cloumNames.get(e);
e = e.replaceAll("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]", "");
keys.append(e + ",");
values.append("'" + value + "',"); } insertSQL.append(keys.toString());
insertSQL.append(" TIME) values (");
insertSQL.append(values.toString());
insertSQL.append(" NOW())");
executeUpdate(insertSQL.toString());
}

BaseDao的更多相关文章

  1. 【01-05】hibernate BaseDao

    BaseDao接口定义 package org.alohaworld.util.dao; import java.io.Serializable; import java.util.List; imp ...

  2. orm映射 封装baseDao

    是用orm映射封装自己封装dao层 思路:通过映射获得实体类的属性拼接sql语句 import java.lang.reflect.Field; import java.lang.reflect.In ...

  3. BaseDao代码,用于连接数据库实行增删改查等操作

    在学习JavaWeb时会用到此代码,用于实行增删改查操作 1 package com.bdqn.dao; import java.sql.Connection; import java.sql.Dri ...

  4. java项目常用 BaseDao BaseService

    java项目常用 BaseDao BaseService IBaseDao 1 package com.glht.sim.dao; 2 3  import java.util.List; 4 5 6 ...

  5. ssh注解basedao简单的实现

    @Repository public class BaseDao extends HibernateDaoSupport{ protected Class objectClass; protected ...

  6. baseDao 使用spring3+hibernate4方式

    启动异常: java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast  ...

  7. baseDao 使用spring3+hibernate3方式

    package cn.zk.pic.service.dao; import java.io.Serializable; import java.util.List; import java.util. ...

  8. 一种好的持久层开发方法——建立BaseDao和BaseDaoImpl

    使用hibernate开发持久层时,我们会发现:虽然entity类的含义和需求不同,其对应的Dao层类对应的方法也是不同的.但是有许多方法操作确实相同的.比如实体的增加,删除,修改更新,以及许多常用的 ...

  9. Hibernate的BaseDao辅助类

    1.BaseDao接口类,该类封装了一些hibernate操作数据库的一些常用的方法,包括分页查询,使用该类极大的简化了hibernate的开发 BaseDao.java package com.kj ...

随机推荐

  1. ASP.NET MVC传送参数至服务端

    ASP.NET MVC传送参数至服务端,前端与服务端的写法,你可以参考与采用适合你的需求的.当你只传递一两个参数也许觉得没有什么,如果一个方法中带的参数多的话,可以考虑model,前端可以考虑对象进行 ...

  2. Can you explain Lazy Loading?

    Introduction Lazy loading is a concept where we delay the loading of the object until the point wher ...

  3. cppcheck

    http://sourceforge.net/projects/cppcheck/files/?source=navbar https://github.com/danmar/cppcheck htt ...

  4. u-boot中nandflash初始化流程分析(转)

    u-boot中nandflash初始化流程分析(转) 原文地址http://zhuairlunjj.blog.163.com/blog/static/80050945201092011249136/ ...

  5. js removeChild 方法

    1. 概述 删除后的节点虽然不在文档树中了,但其实它还在内存中,可以随时再次被添加到别的位置. 当你遍历一个父节点的子节点并进行删除操作时,要注意,children属性是一个只读属性,并且它在子节点变 ...

  6. git 上传项目到github

    1.本地新建文件夹GIT,Git Bash打开命令窗口, ①git config --global user.name "名字"  eg:  git config --global ...

  7. 使用正则表达式获取Sql查询语句各项(表名、字段、条件、排序)

    string text = "select * from [admin] where aa=1 and cc='b' order by aa desc "; Regex reg = ...

  8. [Tool] 使用Visual Studio Code开发TypeScript

    [Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在「Windows」.「OS X」操作系统上,使用Visual Studio Code ...

  9. Underscore学习笔记1

    项目用了很久underscore.每次都是临时查手册,没有系统的研究过,最近有空正好看看 github地址:https://github.com/lily1010/underscore_learn 一 ...

  10. 精简CSS代码

    精简CSS代码可以帮助减小样式文件的大小,使代码清晰,方便维护. 使用简写属性及默认值 .header { margin-top: 10px; margin-right: 20px; margin-b ...