博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jdbc中PreparedStatment通用增删改查
阅读量:3963 次
发布时间:2019-05-24

本文共 5586 字,大约阅读时间需要 18 分钟。

1.DBUtil

package loey.DBUtil;import org.junit.Test;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.Properties;/** * JDBC工具类,简化JDBC编程 */public class DBUtil {
/** * 工具类中的构造方法是私有的 * 因为工具类中的方法都是静态的,直接通过类名去调即可。 */ private DBUtil() {
} private static Properties getProperties(){
//ResourceBundle bundle = ResourceBundle.getBundle("jdbc"); //String driver = bundle.getString("driver"); //String url = bundle.getString("url"); //String user = bundle.getString("user"); //String password = bundle.getString("password"); //String sql = bundle.getString("sql"); // 1.读取配置文件中的4个基本信息 Properties pros = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"); try {
pros.load(is); } catch (IOException e) {
e.printStackTrace(); } return pros; } /** * 静态代码块,类加载的时候执行 * 把注册驱动程序的代码放在静态代码块中,避免多次获取连接对象时重复调用 */ static{
Properties pros = getProperties(); String driver = pros.getProperty("driver"); try {
Class.forName(driver); } catch (ClassNotFoundException e) {
e.printStackTrace(); } } /** * @return 获取连接 * @throws SQLException */ public static Connection getConnection(String database) throws Exception {
Properties pros = getProperties(); String url = pros.getProperty("url"); String user = pros.getProperty("user"); String password = pros.getProperty("password"); Connection conn = DriverManager.getConnection(url + database, user, password); return conn; } public static void close(Connection conn, Statement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close(); } catch (SQLException e) {
e.printStackTrace(); } } if (ps != null) {
try {
ps.close(); } catch (SQLException e) {
e.printStackTrace(); } } if (conn != null) {
try {
conn.close(); } catch (SQLException e) {
e.printStackTrace(); } } }}

2.通用增删改

public void update(String sql,Object ...args){
//sql中占位符的个数与可变形参的长度相同! Connection conn = null; PreparedStatement ps = null; try {
//1.获取数据库的连接 conn = DBCUtils.getConnection(); //2.预编译sql语句,返回PreparedStatement的实例 ps = conn.prepareStatement(sql); //3.填充占位符 for(int i = 0;i < args.length;i++){
ps.setObject(i + 1, args[i]);//小心参数声明错误!! } //4.执行 ps.executeUpdate(); } catch (Exception e) {
e.printStackTrace(); }finally{
//5.资源的关闭 JDBCUtils.closeResource(conn, ps); } }

3.通用的查询操作,返回表中的一条记录

/**     * 说明:     *     1)public 与 返回值中间
非常重要,可以理解为声明此方法为泛型方法。 * 2)只有声明了
的方法才是泛型方法,泛型类中的使用了泛型的成员方法并不是泛型方法。 * 3)
表明该方法将使用泛型类型T,此时才可以在方法中使用泛型类型T。 * 4)与泛型类的定义一样,此处T可以随便写为任意标识,常见的如T、E、K、V等形式的参数常用于表示泛型。 * * 针对于不同的表的通用的查询操作,返回表中的一条记录 * * @param clazz 将查询的结果放哪个bean文件中 * @param sql SQL语句 * @param args 占位符的具体信息 * @param
哪个表对应哪个对象 * @return 带查询结果的对象 * */ public
T getInstance(Class
clazz,String sql,Object...args){ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBUtil.getConnection("test"); ps = conn.prepareStatement(sql); for (int i = 0; i < args.length; i++) { ps.setObject(i + 1 ,args[i]); } rs = ps.executeQuery(); // 获取结果集的元数据 :ResultSetMetaData ResultSetMetaData rsmd = rs.getMetaData(); //获取列数 int columnCount = rsmd.getColumnCount(); if(rs.next()){ T t = clazz.newInstance(); for (int i = 0; i < columnCount; i++) { Object columnValue = rs.getObject(i + 1); //获取表字段的别名 String columnLabel = rsmd.getColumnLabel(i + 1); Field field = clazz.getDeclaredField(columnLabel); field.setAccessible(true); field.set(t,columnValue); } return t; } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(conn,ps,rs); } return null; }

4.表的通用的查询操作,返回多条数据

public  
List
getForList(Class
clazz, String sql, Object...args){
Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try {
conn = DBUtil.getConnection("test"); ps = conn.prepareStatement(sql); for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1,args[i]); } rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); //创建集合对象 ArrayList
list = new ArrayList<>(); while(rs.next()){
T t = clazz.newInstance(); for (int i = 0; i < columnCount; i++) {
Object columnValue = rs.getObject(i + 1); String columnLabel = rsmd.getColumnLabel(i + 1); Field field = clazz.getDeclaredField(columnLabel); field.setAccessible(true); field.set(t,columnValue); } list.add(t); } return list; } catch (Exception e) {
e.printStackTrace(); } finally {
DBUtil.close(conn,ps,rs); } return null; }

转载地址:http://kauki.baihongyu.com/

你可能感兴趣的文章
Linux 系统内核空间与用户空间通信的实现与分析
查看>>
64位int类型用printf输出问题
查看>>
进程的状态转换
查看>>
如何查看进程的信息(线程数)
查看>>
Linux中的chage命令
查看>>
linux-详细解析密码文件passwd与shadow
查看>>
su- 与su的区别
查看>>
linux下发邮件mail
查看>>
echo如何手动输出换行
查看>>
身份证的正确使用方法——非常重要的知识
查看>>
ExtJS & Ajax
查看>>
Tomcat在Windows下的免安装配置
查看>>
JMeter常用测试元件
查看>>
JMeter——使用技巧
查看>>
Hibernate 实体层设计--Table per subclass
查看>>
Ruby解决方案:The 'ffi' native gem requires installed build tools ; 含最新DevKit下载地址
查看>>
Python之操作MySQL数据库(二)
查看>>
简单介绍如何使用robotium进行自动化测试
查看>>
Python之操作XML文件
查看>>
eclipse+ADT 进行android应用签名详解
查看>>