首先介绍一下Dbutils:

   Common
Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
  补充一下,传统操作数据库的类指的是JDBC(java database
connection:java数据库连接,java的数据库操作的基础API。)。
  DBUtils是java编程中的数据库操作实用工具,小巧简单实用。有兴趣的话可以到官网下载:http://commons.apache.org/dbutils/ 
下面的工具包正是符合了Common Dbutils 的思想

package com.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class JdbcUtils
{

// 表示定义数据库的用户名
    private
final String USERNAME = "root";
    //
定义数据库的密码
    private
final String PASSWORD = "admin";
    //
定义数据库的驱动信息
    private
final String DRIVER = "com.mysql.jdbc.Driver";
    //
定义访问数据库的地址
    private
final String URL = "jdbc:mysql://localhost:3306/mydb";
    //
定义数据库的链接
    private
Connection connection;
    //
定义sql语句的执行对象
    private
PreparedStatement pstmt;
    //
定义查询返回的结果集合
    private
ResultSet resultSet;

public JdbcUtils()
    {
  
   
 try
  
   
 {
  
   
   
 Class.forName(DRIVER);
  
   
   
 System.out.println("注册驱动成功!!");
  
   
 }
  
   
 catch (Exception e)
  
   
 {
  
   
   
 // TODO: handle exception
  
   
 }
    }

// 定义获得数据库的链接
    public
Connection getConnection()
    {
  
   
 try
  
   
 {
  
   
   
 connection = DriverManager.getConnection(URL,
USERNAME, PASSWORD);
  
   
   
 System.out.println("数据库连接成功");
  
   
 }
  
   
 catch (Exception e)
  
   
 {
  
   
   
 // TODO: handle exception
  
   
 }
  
   
 return connection;
    }

public
boolean updateByPreparedStatement(String sql,
Listparams)
  
   
   
 throws SQLException
    {
  
   
 boolean flag = false;
  
   
 int result = -1;//
表示当用户执行添加删除和修改的时候所影响数据库的行数
  
   
 pstmt = connection.prepareStatement(sql);
  
   
 int index = 1;
  
   
 // 填充sql语句中的占位符
  
   
 if (params != null
&& !params.isEmpty())
  
   
 {
  
   
   
 for (int i = 0; i < params.size();
i++)
  
   
   
 {
  
   
   
   
 pstmt.setObject(index++, params.get(i));
  
   
   
 }
  
   
 }
  
   
 result = pstmt.executeUpdate();
  
   
 flag = result > 0 ? true :
false;
  
   
 return flag;
    }

public Map
findSimpleResult(String sql, Listparams)
  
   
   
 throws SQLException
    {
  
   
 Map map = new HashMap();
  
   
 int index = 1;
  
   
 pstmt = connection.prepareStatement(sql);
  
   
 if (params != null
&& !params.isEmpty())
  
   
 {
  
   
   
 for (int i = 0; i < params.size();
i++)
  
   
   
 {
  
   
   
   
 pstmt.setObject(index++, params.get(i));
  
   
   
 }
  
   
 }
  
   
 resultSet = pstmt.executeQuery();// 返回查询结果
  
   
 // 获取此 ResultSet 对象的列的编号、类型和属性。
  
   
 ResultSetMetaData metaData =
resultSet.getMetaData();
  
   
 int col_len = metaData.getColumnCount();//
获取列的长度
  
   
 while (resultSet.next())// 获得列的名称
  
   
 {
  
   
   
 for (int i = 0; i < col_len;
i++)
  
   
   
 {
  
   
   
   
 String cols_name = metaData.getColumnName(i +
1);
  
   
   
   
 Object cols_value =
resultSet.getObject(cols_name);
  
   
   
   
 if (cols_value == null)// 列的值没有时,设置列值为“”
  
   
   
   
 {
  
   
   
   
   
 cols_value = "";
  
   
   
   
 }
  
   
   
   
 map.put(cols_name, cols_value);
  
   
   
 }
  
   
 }
  
   
 return map;
    }

public
List> findMoreResult(String
sql,
  
   
   
 Listparams) throws SQLException
    {
  
   
 List> list = new
ArrayList>();
  
   
 int index = 1;
  
   
 pstmt = connection.prepareStatement(sql);
  
   
 if (params != null
&& !params.isEmpty())
  
   
 {
  
   
   
 for (int i = 0; i < params.size();
i++)
  
   
   
 {
  
   
   
   
 pstmt.setObject(index++, params.get(i));
  
   
   
 }
  
   
 }
  
   
 resultSet = pstmt.executeQuery();
  
   
 ResultSetMetaData metaData =
resultSet.getMetaData();
  
   
 int cols_len = metaData.getColumnCount();
  
   
 while (resultSet.next())
  
   
 {
  
   
   
 Map map = new HashMap();
  
   
   
 for (int i = 0; i < cols_len;
i++)
  
   
   
 {
  
   
   
   
 String cols_name = metaData.getColumnName(i +
1);
  
   
   
   
 Object cols_value =
resultSet.getObject(cols_name);
  
   
   
   
 if (cols_value == null)
  
   
   
   
 {
  
   
   
   
   
 cols_value = "";
  
   
   
   
 }
  
   
   
   
 map.put(cols_name, cols_value);
  
   
   
 }
  
   
   
 list.add(map);
  
   
 }
  
   
 return list;
    }

public T
findSimpleRefResult(String sql, Listparams,
  
   
   
 Class cls) throws Exception
    {
  
   
 T resultObject = null;
  
   
 int index = 1;
  
   
 pstmt = connection.prepareStatement(sql);
  
   
 if (params != null
&& !params.isEmpty())
  
   
 {
  
   
   
 for (int i = 0; i < params.size();
i++)
  
   
   
 {
  
   
   
   
 pstmt.setObject(index++, params.get(i));
  
   
   
 }
  
   
 }
  
   
 resultSet = pstmt.executeQuery();
  
   
 ResultSetMetaData metaData =
resultSet.getMetaData();
  
   
 int cols_len = metaData.getColumnCount();
  
   
 while (resultSet.next())
  
   
 {
  
   
   
 // 通过反射机制创建实例
  
   
   
 resultObject = cls.newInstance();
  
   
   
 for (int i = 0; i < cols_len;
i++)
  
   
   
 {
  
   
   
   
 String cols_name =
metaData.getColumnName(i + 1);

  
   
   
   
 Object cols_value =
resultSet.getObject(cols_name);
  
   
   
   
 if (cols_value == null)
  
   
   
   
 {
  
   
   
   
   
 cols_value = "";
  
   
   
   
 }
  
   
   
   
 // 返回一个 Field 对象,该对象反映此 Class
对象所表示的类或接口的指定已声明字段。
  
   
   
    Field
field = cls.getDeclaredField(cols_name);

  
   
   
   
 field.setAccessible(true);//
打开javabean的访问private权限
  
   
   
   
 field.set(resultObject, cols_value);//
为resultObject对象的field的属性赋值
  
   
   
 } //上面的两行红题字就是要求实体类中的属性名一定要和数据库中
的字段名一定要严//格相同(包括大小写),而oracle数据库中一般都是大写的,如何让oracle区分大小写,请看博///文:http:
//blog.sina.com.cn/s/blog_7ffb8dd501013xkq.html

}
  
   
 return resultObject;
    }

public List
findMoreRefResult(String sql, Listparams,
  
   
   
 Class cls) throws Exception
    {
  
   
 List list = new ArrayList();
  
   
 int index = 1;
  
   
 pstmt = connection.prepareStatement(sql);
  
   
 if (params != null
&& !params.isEmpty())
  
   
 {
  
   
   
 for (int i = 0; i < params.size();
i++)
  
   
   
 {
  
   
   
   
 pstmt.setObject(index++, params.get(i));
  
   
   
 }
  
   
 }
  
   
 resultSet = pstmt.executeQuery();
  
   
 ResultSetMetaData metaData =
resultSet.getMetaData();
  
   
 int cols_len = metaData.getColumnCount();
  
   
 while (resultSet.next())
  
   
 {
  
   
   
 T resultObject = cls.newInstance();
  
   
   
 for (int i = 0; i < cols_len;
i++)
  
   
   
 {
  
   
   
   
 String cols_name = metaData.getColumnName(i +
1);
  
   
   
   
 Object cols_value =
resultSet.getObject(cols_name);
  
   
   
   
 if (cols_value == null)
  
   
   
   
 {
  
   
   
   
   
 cols_value = "";
  
   
   
   
 }
  
   
   
   
 Field field =
cls.getDeclaredField(cols_name);
  
   
   
   
 field.setAccessible(true);
  
   
   
   
 field.set(resultObject, cols_value);
  
   
   
 }
  
   
   
 list.add(resultObject);
  
   
 }
  
   
 return list;
    }

public void
releaseConn()
    {
  
   
 if (resultSet != null)
  
   
 {
  
   
   
 try
  
   
   
 {
  
   
   
   
 resultSet.close();
  
   
   
 }
  
   
   
 catch (SQLException e)
  
   
   
 {
  
   
   
   
 // TODO Auto-generated catch block
  
   
   
   
 e.printStackTrace();
  
   
   
 }
  
   
 }
  
   
 if (pstmt != null)
  
   
 {
  
   
   
 try
  
   
   
 {
  
   
   
   
 pstmt.close();
  
   
   
 }
  
   
   
 catch (SQLException e)
  
   
   
 {
  
   
   
   
 // TODO Auto-generated catch block
  
   
   
   
 e.printStackTrace();
  
   
   
 }
  
   
 }
  
   
 if (connection != null)
  
   
 {
  
   
   
 try
  
   
   
 {
  
   
   
   
 connection.close();
  
   
   
 }
  
   
   
 catch (SQLException e)
  
   
   
 {
  
   
   
   
 // TODO Auto-generated catch block
  
   
   
   
 e.printStackTrace();
  
   
   
 }
  
   
 }
    }

public
static void writeProperties(String url, String user, String
password)
    {
  
   
 Properties pro = new Properties();
  
   
 FileOutputStream fileOut = null;
  
   
 try
  
   
 {
  
   
   
 fileOut = new
FileOutputStream("Config.ini");
  
   
   
 pro.put("url", url);
  
   
   
 pro.put("user", user);
  
   
   
 pro.put("password", password);
  
   
   
 pro.store(fileOut, "My Config");
  
   
 }
  
   
 catch (Exception e)
  
   
 {
  
   
   
 e.printStackTrace();
  
   
 }
  
   
 finally
  
   
 {

try
  
   
   
 {
  
   
   
   
 if (fileOut != null)
  
   
   
   
   
 fileOut.close();
  
   
   
 }
  
   
   
 catch (IOException e)
  
   
   
 {
  
   
   
   
 e.printStackTrace();
  
   
   
 }
  
   
 }
    }

public
static List readProperties()
    {
  
   
 List list = new ArrayList();
  
   
 Properties pro = new Properties();
  
   
 FileInputStream fileIn = null;
  
   
 try
  
   
 {
  
   
   
 fileIn = new FileInputStream("Config.ini");
  
   
   
 pro.load(fileIn);
  
   
   
 list.add(pro.getProperty("url"));
  
   
   
 list.add(pro.getProperty("user"));
  
   
   
 list.add(pro.getProperty("password"));
  
   
 }
  
   
 catch (Exception e)
  
   
 {
  
   
   
 e.printStackTrace();
  
   
 }
  
   
 finally
  
   
 {

try
  
   
   
 {
  
   
   
   
 if (fileIn != null)
  
   
   
   
   
 fileIn.close();
  
   
   
 }
  
   
   
 catch (IOException e)
  
   
   
 {
  
   
   
   
 e.printStackTrace();
  
   
   
 }
  
   
 }
  
   
 return list;
    }

public
static void main(String[] args)
    {
  
   
 // TODO Auto-generated method stub
  
   
 JdbcUtils jdbcUtils = new JdbcUtils();
  
   
 jdbcUtils.getConnection();
  
   
 // String sql = "insert into
userinfo(username,pswd) values(?,?)";
  
   
 // Listparams = new
ArrayList();
  
   
 // params.add("rose");
  
   
 // params.add("123");
  
   
 // try {
  
   
 // boolean flag =
jdbcUtils.updateByPreparedStatement(sql, params);
  
   
 // System.out.println(flag);
  
   
 // } catch (SQLException e) {
  
   
 // // TODO Auto-generated catch block
  
   
 // e.printStackTrace();
  
   
 // }
  
   
 String sql = "select * from userinfo ";
  
   
 // Listparams = new
ArrayList();
  
   
 // params.add(1);
  
   
 
    }

}

JDBC深度封装的工具类 (具有高度可重用性)的更多相关文章

  1. 一、JDBC的概述 二、通过JDBC实现对数据的CRUD操作 三、封装JDBC访问数据的工具类 四、通过JDBC实现登陆和注册 五、防止SQL注入

    一.JDBC的概述###<1>概念 JDBC:java database connection ,java数据库连接技术 是java内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...

  2. JDBC封装的工具类

    1. JDBC封装的工具类 public class JDBCUtil { private static Properties p = new Properties(); private static ...

  3. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  4. JAVA中封装JSONUtils工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

  5. Workbook导出excel封装的工具类

    在实际中导出excel非常常见,于是自己封装了一个导出数据到excel的工具类,先附上代码,最后会写出实例和解释.支持03和07两个版本的 excel. HSSF导出的是xls的excel,XSSF导 ...

  6. writeValueAsString封装成工具类

    封装成工具类 <span style="font-family:Microsoft YaHei;">public static String toJsonByObjec ...

  7. 转:轻松把玩HttpClient之封装HttpClient工具类(一)(现有网上分享中的最强大的工具类)

    搜了一下网络上别人封装的HttpClient,大部分特别简单,有一些看起来比较高级,但是用起来都不怎么好用.调用关系不清楚,结构有点混乱.所以也就萌生了自己封装HttpClient工具类的想法.要做就 ...

  8. 打印 Logger 日志时,需不需要再封装一下工具类?

    在开发过程中,打印日志是必不可少的,因为日志关乎于应用的问题排查.应用监控等.现在打印日志一般都是使用 slf4j,因为使用日志门面,有助于打印方式统一,即使后面更换日志框架,也非常方便.在 < ...

  9. 【JDBC】Java 连接 MySQL 基本过程以及封装数据库工具类

    一. 常用的JDBC API 1. DriverManager类 : 数据库管理类,用于管理一组JDBC驱动程序的基本服务.应用程序和数据库之间可以通过此类建立连接.常用的静态方法如下 static ...

随机推荐

  1. UNICODE串转换成char类型串的四种方法

    1. 调用 WideCharToMultiByte() API int WideCharToMultiByte (     UINT    CodePage,                //1 U ...

  2. 用LaTeX写线性规划

    线性规划由目标函数和若干约束构成,Latex中并没有直接的命令来写线性规划.简单的做法是使用\begin{eqnarray} … \end{eqnarray}命令,但eqnarray命令是使若干方程按 ...

  3. jquery dialog close icon missing 关闭图片丢失,样式丢失问题

    http://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon

  4. Vue使用中遇到问题汇总(一)32个

    1.安装一些需要编译的包:提示没有安装python.build失败等 因为一些 npm 的包安装需要编译的环境,mac 和 linux 都还好,大多都齐全 .window 用户依赖 visual st ...

  5. axios 同时执行多个请求

    http://chuansong.me/n/394228451820 同时执行多个请求 axios.all([ axios.get('https://api.github.com/xxx/1'), a ...

  6. Win7开机提示group policy client无法登陆怎么办

    1 开机按F8,进入安全模式 2 进入系统之后运行注册表,定位到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prof ...

  7. iOS_Objective-C測试

    1. iOS中程序正常载入UIViewControlle时,下面四个方法哪个最先运行? A.viewVillAppear B.viewDidLoad C.viewDidAppear D.viewWil ...

  8. 001-使用idea开发环境安装部署,npm工具栏,脚本运行

    一.概述 参看官方文档:https://ant.design/docs/spec/introduce-cn 其中包含了设计价值观.设计原则.视觉.模式.可视化.动态等. 其中Ant Design 的 ...

  9. ES6 Reflect

    1.Reflect概述 ES6 为了操作对象而提供的新 API 2.Reflect设计目的 (1)将Object对象的一些明显属于语言内部的方法(比如Object.defineProperty),放到 ...

  10. Vue学习小结

    ES6 let完全可以取代var const声明一个只读的常量 箭头函数:可以绑定this对象,大大减少了显式绑定this对象的写法(call.apply.bind) 函数绑定(function bi ...