java连接数据库增、删、改、查工具类_java

java连接数据库增、删、改、查工具类

数据库操作工具类,因为各厂家数据库的分页条件不同,目前支持Mysql、Oracle、Postgresql的分页查询
在Postgresql环境测试过了,其他数据库未测试。
sql语句需要使用预编译形式的

复制代码 代码如下:

package db;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 数据库查询工具类
 * 使用预编译的sql
 *
 * @author XueLiang
 *
 */
public class DBUtil {

 private static String driver;

 private static DataSource ds = null;

 private static String url = "jdbc:postgresql://192.168.56.101/db";
 private static String user = "test";
 private static String password = "12345678";

 static {
  try {
   Class.forName("org.postgresql.Driver");
   //ds = (DataSource)SpringContextUtil.getBean("dataSource");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 /**
  * 建立连接
  *
  * @return con Connection
  * @throws Exception
  */
 private static Connection getConnection() throws Exception {
  Connection conn = DriverManager.getConnection(url, user, password);
//  Connection conn = ds.getConnection();
  Driver d = DriverManager.getDriver(conn.getMetaData().getURL());
  driver = d.getClass().getName();
  return conn;
 }

 /**
  * 关闭连接
  *
  * @param conn
  * @param stmt
  * @param preStmt
  * @param rs
  * @throws SQLException
  */
 private static void replease(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
  if (rs != null) {
   rs.close();
   rs = null;
  }
  if (stmt != null) {
   stmt.close();
   stmt = null;
  }
  if (conn != null) {
   conn.close();
   conn = null;
  }
 }

 /**
  * 利用正则表达式,获得SELECT SQL中的列名
  *
  * @param sql
  * @return
  */
 private static List<String> getColumnsFromSelect(String sql) {
  List<String> colNames = new ArrayList<String>();
  // 取出sql中列名部分
  Pattern p = Pattern.compile("(?i)select\\s(.*?)\\sfrom.*");
  Matcher m = p.matcher(sql.trim());
  String[] tempA = null;
  if (m.matches()) {
   tempA = m.group(1).split(",");
  }
  if (tempA == null) {
   return null;
  }
  String p1 = "(\\w+)";
  String p2 = "(?:\\w+\\s(\\w+))";
  String p3 = "(?:\\w+\\sas\\s(\\w+))";
  String p4 = "(?:\\w+\\.(\\w+))";
  String p5 = "(?:\\w+\\.\\w+\\s(\\w+))";
  String p6 = "(?:\\w+\\.\\w+\\sas\\s(\\w+))";
  String p7 = "(?:.+\\s(\\w+))";
  String p8 = "(?:.+\\sas\\s(\\w+))";
  p = Pattern.compile("(?:" + p1 + "||" + p2 + "||" + p3 + "||" + p4
    + "||" + p5 + "||" + p6 + "||" + p7 + "||" + p8 + ")");
  for (String temp : tempA) {
   m = p.matcher(temp.trim());
   if (!m.matches()) {
    continue;
   }
   for (int i = 1; i <= m.groupCount(); i++) {
    if (m.group(i) == null || "".equals(m.group(i))) {
     continue;
    }
    colNames.add(m.group(i));
   }
  }
  return colNames;
 }

 /**
  * 利用正则表达式,获得INSERT SQL中的列名
  *
  * @param sql
  * @return
  */
 private static List<String> getColumnsFromInsert(String sql) {
  List<String> colNames = new ArrayList<String>();
  // 取出sql中列名部分
  Pattern p = Pattern.compile("(?i)insert\\s+into.*\\((.*)\\)\\s+values.*");
  Matcher m = p.matcher(sql.trim());
  String[] tempA = null;
  if (m.matches()) {
   tempA = m.group(1).split(",");
  }
  if (tempA == null) {
   return null;
  }
  String p1 = "(\\w+)";
  String p2 = "(?:\\w+\\s(\\w+))";
  String p3 = "(?:\\w+\\sas\\s(\\w+))";
  String p4 = "(?:\\w+\\.(\\w+))";
  String p5 = "(?:\\w+\\.\\w+\\s(\\w+))";
  String p6 = "(?:\\w+\\.\\w+\\sas\\s(\\w+))";
  String p7 = "(?:.+\\s(\\w+))";
  String p8 = "(?:.+\\sas\\s(\\w+))";
  p = Pattern.compile("(?:" + p1 + "||" + p2 + "||" + p3 + "||" + p4
    + "||" + p5 + "||" + p6 + "||" + p7 + "||" + p8 + ")");
  for (String temp : tempA) {
   m = p.matcher(temp.trim());
   if (!m.matches()) {
    continue;
   }
   for (int i = 1; i <= m.groupCount(); i++) {
    if (m.group(i) == null || "".equals(m.group(i))) {
     continue;
    }
    colNames.add(m.group(i));
   }
  }
  return colNames;
 }

 /**
  * 利用正则表达式,获得UPDATE SQL中的列名, 包括WHERE字句的
  *
  * @param sql
  * @return
  */
 private static List<String> getColumnsFromUpdate(String sql) {
  List<String> colNames = new ArrayList<String>();
  // 取出sql中列名部分
  Pattern p = Pattern.compile("(?i)update(?:.*)set(.*)(?:from.*)*where(.*(and)*.*)");
  Matcher m = p.matcher(sql.trim());
  String[] tempA = null;
  if (m.matches()) {
   tempA = m.group(1).split(",");
   if(m.groupCount() > 1){
    String[] tmp = m.group(2).split("and");
    String[] fina = new String[tempA.length + tmp.length];
    System.arraycopy(tempA, 0, fina, 0, tempA.length);
    System.arraycopy(tmp, 0, fina, tempA.length, tmp.length);
    tempA = fina;
   }
  }
  if (tempA == null) {
   return null;
  }
  String p1 = "(?i)(\\w+)(?:\\s*\\=\\s*.*)";
  String p2 = "(?i)(?:\\w+\\.)(\\w+)(?:\\s*\\=\\s*.*)";
  p = Pattern.compile(p1 + "||" + p2);
  for (String temp : tempA) {
   m = p.matcher(temp.trim());
   if (!m.matches()) {
    continue;
   }
   for (int i = 1; i <= m.groupCount(); i++) {
    if (m.group(i) == null || "".equals(m.group(i))) {
     continue;
    }
    colNames.add(m.group(i));
   }
  }
  return colNames;
 }

 /**
  * 为sql添加统计代码
  *
  * @param sql
  * @return
  */
 private static String addCountSQL(String sql) {
  StringBuffer sb = new StringBuffer();
  sb.append(" select count(*) as dataCount from (");
  sb.append(sql);
  sb.append(") as a");
  return sb.toString();
 }

 /**
  * 为sql添加分页代码
  *
  * @param sql
  * @param start
  * @param limit
  * @return
  */
 private static String addPagingSQL(String sql, int start, int limit) {
  StringBuffer sb = new StringBuffer();
  if ("com.microsoft.jdbc.sqlserver.SQLServerDviver".equals(driver)) {//SQLServer 0.7 2000

  } else if ("com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver)) {//SQLServer 2005 2008

  } else if ("com.mysql.jdbc.Driver".equals(driver)) {//MySQL
   sb.append(sql);
   sb.append(" LIMIT ");
   sb.append(start);
   sb.append(",");
   sb.append(limit);
  } else if ("oracle.jdbc.driver.OracleDriver".equals(driver)) {//Oracle8/8i/9i/10g数据库(thin模式)
   List<String> list = getColumnsFromSelect(sql);
   sb.append("select ");
   for (String str : list)
    sb.append(str).append(", ");
   sb.deleteCharAt(sb.lastIndexOf(","));
   sb.append(" from (").append(sql).append(") as a");
   sb.append(" where rownum between ").append(start == 0 ? 1 : start).append(" and ").append(limit);
  } else if ("com.ibm.db2.jdbc.app.DB2Driver".equals(driver)) {//DB2

  } else if ("com.sybase.jdbc.SybDriver".equals(driver)) {//Sybase

  } else if ("com.informix.jdbc.IfxDriver".equals(driver)) {//Informix

  } else if ("org.postgresql.Driver".equals(driver)) {//PostgreSQL
   sb.append(sql);
   sb.append(" LIMIT ");
   sb.append(limit);
   sb.append(" OFFSET ");
   sb.append(start);
  }
  return sb.toString();
 }
 /**
  * 将RusultSet对象实例化T对象
  *
  * @param <T>
  * @param t
  * @param rs
  * @param sql
  * @return t
  * @throws Exception
  */
 private static <T> T instance(Class<T> t, ResultSet rs, String sql) throws Exception{
  List<String> columns = getColumnsFromSelect(sql);
  T obj = t.newInstance();
  for (String col : columns) {
   try{
    Field f = t.getDeclaredField(col);
    f.setAccessible(true);
    Object v = getValue(col, f.getType().getName(), rs);
    f.set(obj, v);
   }catch(NoSuchFieldException e){
    Field[] fields = t.getDeclaredFields();
    for (Field f : fields) {
     Column column = f.getAnnotation(Column.class);
     if(column != null && column.name().equals(col)){
      f.setAccessible(true);
      Object v = getValue(col, f.getType().getName(), rs);
      f.set(obj, v);
     }
    }
   }
  }

  return obj;
 }

 private static Object getValue(String columnName, String type, ResultSet rs) throws SQLException{
  Object obj = null;
//  System.out.println("name="+f.getName()+", type="+f.getType().getName() );
  if("java.lang.Integer".equals(type) || "int".equals(type)) {
   obj = rs.getInt(columnName);
  }else if("java.lang.Long".equals(type) || "long".equals(type)) {
   obj = rs.getLong(columnName);
  }else if("java.lang.Short".equals(type)||"short".equals(type)) {
   obj = rs.getShort(columnName);
  }else if("java.lang.Float".equals(type)||"float".equals(type)) {
   obj = rs.getFloat(columnName);
  }else if("java.lang.Double".equals(type)||"double".equals(type)) {
   obj = rs.getDouble(columnName);
  }else if("java.lang.Byte".equals(type)||"byte".equals(type)) {
   obj = rs.getByte(columnName);
  }else if("java.lang.Boolean".equals(type)||"boolean".equals(type)) {
   obj = rs.getBoolean(columnName);
  }else if("java.lang.String".equals(type)) {
   obj = rs.getString(columnName);
  }else {
   obj = rs.getObject(columnName);
  }
//  System.out.println("name="+f.getName() +", type="+f.getType().getName()+", value="+(obj == null ? "NULL" : obj.getClass())+",{"+columnName+":"+obj+"}");
  return obj;
 }
 /**
  * 将param中的参数添加到pstate
  *
  * @param pstate
  * @param columns
  * @throws SQLException
  */
 private static <T> void setParameters(PreparedStatement pstate, Object... params) throws Exception {
  if (params != null && params.length > 0) {
   for (int i = 0; i < params.length; i++) {
    Object value = params[i];
    int j = i + 1;
    if (value == null)
     pstate.setString(j, "");
    if (value instanceof String)
     pstate.setString(j, (String) value);
    else if (value instanceof Boolean)
     pstate.setBoolean(j, (Boolean) value);
    else if (value instanceof Date)
     pstate.setDate(j, (Date) value);
    else if (value instanceof Double)
     pstate.setDouble(j, (Double) value);
    else if (value instanceof Float)
     pstate.setFloat(j, (Float) value);
    else if (value instanceof Integer)
     pstate.setInt(j, (Integer) value);
    else if (value instanceof Long)
     pstate.setLong(j, (Long) value);
    else if (value instanceof Short)
     pstate.setShort(j, (Short) value);
    else if (value instanceof Time)
     pstate.setTime(j, (Time) value);
    else if (value instanceof Timestamp)
     pstate.setTimestamp(j, (Timestamp) value);
    else
     pstate.setObject(j, value);
   }
  }

 }
 /**
  * 将param中的参数添加到pstate
  *
  * @param pstate
  * @param columns
  * @param t
  * @throws SQLException
  */
 private static <T> void setParameters(PreparedStatement pstate, List<String> columns, T t) throws Exception {
  if (columns != null && columns.size() > 0) {
   for (int i = 0; i < columns.size(); i++) {
    String attr = columns.get(i);
    Object value = null;
    Class<?> c = t.getClass();
    try{
     Field f = c.getDeclaredField(attr);
     value = f.get(t);
    } catch (NoSuchFieldException e){
     Field[] fields = c.getDeclaredFields();
     for (Field f : fields) {
      Column column = f.getAnnotation(Column.class);
      if(column != null && column.name().equals(attr))
       value = f.get(t);
     }
    }
    int j = i + 1;
    if (value == null)
     pstate.setString(j, "");
    if (value instanceof String)
     pstate.setString(j, (String) value);
    else if (value instanceof Boolean)
     pstate.setBoolean(j, (Boolean) value);
    else if (value instanceof Date)
     pstate.setDate(j, (Date) value);
    else if (value instanceof Double)
     pstate.setDouble(j, (Double) value);
    else if (value instanceof Float)
     pstate.setFloat(j, (Float) value);
    else if (value instanceof Integer)
     pstate.setInt(j, (Integer) value);
    else if (value instanceof Long)
     pstate.setLong(j, (Long) value);
    else if (value instanceof Short)
     pstate.setShort(j, (Short) value);
    else if (value instanceof Time)
     pstate.setTime(j, (Time) value);
    else if (value instanceof Timestamp)
     pstate.setTimestamp(j, (Timestamp) value);
    else
     pstate.setObject(j, value);
   }
  }

 }

 /**
  * 执行insert操作
  *
  * @param sql 预编译的sql语句
  * @param t sql中的参数
  * @return 执行行数
  * @throws Exception
  */
 public static <T> int insert(String sql, T t) throws Exception {
  Connection conn = null;
  PreparedStatement pstate = null;
  int updateCount = 0;
  try {
   conn = getConnection();
   List<String> columns = getColumnsFromInsert(sql);
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, columns, t);
   updateCount = pstate.executeUpdate();
  } finally {
   replease(conn, pstate, null);
  }
  return updateCount;
 }
 /**
  * 执行insert操作
  *
  * @param sql 预编译的sql语句
  * @param param 参数
  * @return 执行行数
  * @throws Exception
  */
 public static <T> int insert(String sql, Object... param) throws Exception {
  Connection conn = null;
  PreparedStatement pstate = null;
  int updateCount = 0;
  try {
   conn = getConnection();
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, param);
   updateCount = pstate.executeUpdate();
  } finally {
   replease(conn, pstate, null);
  }
  return updateCount;
 }
 /**
  * 执行update操作
  *
  * @param sql 预编译的sql语句
  * @param t sql中的参数
  * @return 执行行数
  * @throws Exception
  */
 public static <T> int update(String sql, T t) throws Exception {
  Connection conn = null;
  PreparedStatement pstate = null;
  int updateCount = 0;
  try {
   conn = getConnection();
   List<String> columns = getColumnsFromUpdate(sql);
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, columns, t);
   updateCount = pstate.executeUpdate();
  } finally {
   replease(conn, pstate, null);
  }
  return updateCount;
 }
 /**
  * 执行update操作
  *
  * @param sql
  * @param param 参数
  * @return 执行行数
  * @throws Exception
  */
 public static <T> int update(String sql, Object... param) throws Exception {
  Connection conn = null;
  PreparedStatement pstate = null;
  int updateCount = 0;
  try {
   conn = getConnection();
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, param);
   updateCount = pstate.executeUpdate();
  } finally {
   replease(conn, pstate, null);
  }
  return updateCount;
 }
 /**
  * 查询复数的对象
  *
  * @param t 查询结果封装的对象类型
  * @param sql 预编译的sql
  * @param param 查询条件
  * @return List<T>
  * @throws Exception
  */
 public static <T> List<T> queryPlural(Class<T> t, String sql, Object... param) throws Exception {
  Connection conn = null;
  PreparedStatement stmt = null;
  ResultSet rs = null;
  List<T> list = new ArrayList<T>();
  try {
   conn = getConnection();
   stmt = conn.prepareStatement(sql);
   setParameters(stmt, param);
   rs = stmt.executeQuery();
   while (rs.next()) {
    list.add(instance(t, rs, sql));
   }
  } finally {
   replease(conn, stmt, rs);
  }
  return list;
 }

 /**
  * 分页查询复数的对象
  *
  * @param t 查询结果封装的对象类型
  * @param start 开始页
  * @param limit 页大小
  * @param sql 预编译的sql语句
  * @param param 查询参数
  * @throws Exception
  */
 public static <T> List<T> queryPluralForPagging(Class<T> t, int start, int limit, String sql, Object... param) throws Exception {
  Connection conn = null;
  PreparedStatement stmt = null;
  ResultSet rs = null;
  List<T> list = new ArrayList<T>();
  try {
   conn = getConnection();
   //添加分页代码
   sql = addPagingSQL(sql, start, limit);
   stmt = conn.prepareStatement(sql);
   setParameters(stmt, param);
   rs = stmt.executeQuery();
   while (rs.next()) {
    list.add(instance(t, rs, sql));
   }
  } finally {
   replease(conn, stmt, rs);
  }
  return list;
 }

 /**
  * 查询单个的对象
  *
  * @param t 查询结果对象
  * @param sql 预编译的sql
  * @param param 查询参数
  * @return T
  * @throws Exception
  */
 public static <T> T querySingular(Class<T> t, String sql, Object... param) throws Exception {
  T obj = null;
  ResultSet rs = null;
  Connection conn = null;
  PreparedStatement pstate = null;
  try {
   conn = getConnection();
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, param);
   rs = pstate.executeQuery();
   if (rs.next()) {
    obj = instance(t, rs, sql);
   }
  } finally {
   replease(conn, pstate, rs);
  }
  return obj;
 }

 /**
  * 查询数据量
  *
  * @param param 查询参数
  * @param sql
  * @return
  * @throws SQLException
  * @throws NamingException
  */
 public static int queryDataCount(String sql, Object... param)
   throws Exception {
  int dataCount = 0;
  Connection conn = null;
  PreparedStatement pstate = null;
  ResultSet rs = null;
  try {
   conn = getConnection();
   sql = addCountSQL(sql);
   pstate = conn.prepareStatement(sql);
   setParameters(pstate, param);
   rs = pstate.executeQuery();
   if (rs.next()) {
    dataCount = rs.getInt("dataCount");
   }
  } finally {
   replease(conn, pstate, rs);
  }
  return dataCount;
 }

 /**
  * 属性字段的注释,用于标记该属性对应的数据库字段
  * 例如:
  * @Column(name="user_name");
  * String userName;
  * 表示userName这个属性对应的数据库字段是user_name
  *
  * 如果属性和数据库字段完全一致,则不必标注
  * @author xueliang
  */
 @Target({ ElementType.FIELD })
 @Retention(RetentionPolicy.RUNTIME)
 public @interface Column{
  String name() default "";
 } 

}

时间: 2024-12-27 07:49:43

java连接数据库增、删、改、查工具类_java的相关文章

java实现excel导入数据的工具类_java

导入Excel数据的工具类,调用也就几行代码,很简单的. 复制代码 代码如下: import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;import org.apache.commons.beanutils.BeanUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java.io.IOExc

简单的php数据库操作类代码(增,删,改,查)_php实例

数据库操纵基本流程为: 1.连接数据库服务器 2.选择数据库 3.执行SQL语句 4.处理结果集 5.打印操作信息 其中用到的相关函数有 •resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] ) 连接数据库服务器•resource mysql_pconnect ( [string server [, strin

Struts2实现CRUD(增 删 改 查)功能实例代码_java

CRUD是Create(创建).Read(读取).Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影.如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了,所以大家使用新框架开发OLTP(Online Transaction Processing)应用程序时,首先会研究一下如何编写CRUD.这类似于大家在学习新编程语言时喜欢编写"Hello World". 本文旨在讲述Struts 2上的CRUD开发,所以为了例子的简单易懂,我不会花时间在数

简单的增 删 改 查

一:insert语句into 关键字是可选的values关键字前面的()是可选的,这里是要接收数据的列values后面,有两种方式提供值1:显式的给出值  2:从select语句中导出值 insert语句注意几点1:不要理标志列,系统会给你插入的2:给出实际的值,如果没有,那就null3:给出默认的值,default关键字,告诉数据库取默认值 insert into ... select什么时候会这么用,当成批的数据来自1:数据库中的另一个表2:同一台服务器完全不同的数据库3:另一个SQLSER

计算java常用类型字节大小的工具类

问题描述 /***Name:Sizeof.java*Copyright:zhaozhihua**/packagecom.tywire.tadu.utils;importjava.lang.reflect.Array;importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;importjava.util.Enumeration;importjava.util.HashMap;importjava.util.Hashtable;i

Java程序员的日常—— FileUtils工具类的使用

package cn.xingoo.learn.commons; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.FileFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; imp

Java程序员的日常—— Arrays工具类的使用

这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排序算法,这个还是非常有用的. 所有的方法都是在下面的类中进行测试的: public class ArraysTest { String[] array = new String[]{"a","c","2","1","b&

我的Java开发学习之旅------&amp;gt;工具类:Java获取字符串和文件进行MD5值

ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一下才发现所谓的"秒传"是常见的"忽略式"上传方式,就是您上传了一个文件名为111.exe,MD5为一个数,有一个网友以前也上传一个叫222.exe,MD5和您上传的文件MD5码一模一样,所以这个文件上传到服务器上的时间就很短了,这是因为别人上传过这个文件,您上传这个文件

Java图像处理工具类_java

本工具类的功能:缩放图像.切割图像.图像类型转换.彩色转黑白.文字水印.图片水印等 复制代码 代码如下: package net.kitbox.util; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Re