一、使用详解
在mysql中创建一个库mall_system,并创建user表和插入表的数据。
新建一个Java工程jdbc,并导入数据驱动。
二、详细步骤 1、加载数据库驱动 1 2 3 4 5 Class.forName("com.mysql.jdbc.Driver" ); Class.forName("com.mysql.cj.jdbc.Driver" );
2、建立连接 1、数据库URL URL用于标识数据库的位置,程序员通过URL地址告诉JDBC程序连接哪个数据库,URL的写法为:
jdbc:mysql:[]//localhost:3306/mall_system?参数名:参数值
常用数据库URL地址 的写法:
Oracle:jdbc:oracle:thin:@localhost:1521:mall_system
SqlServer:jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=mall_system
MySql:jdbc:mysql://localhost:3306/mall_system?useUnicode=true&characterEncoding=UTF-8&userSSL=true&serverTimezone=Hongkong
注意:如果是localhost:3306,mysql可以简写为jdbc:mysql:///sid(尽量不这样)
2、Connection Jdbc程序中的Connection,它用于代表数据库的链接,Collection是数据库编程中最重要的一个对象,客户端与数据库所有交互都是通过connection对象完成的,创建方法为:
1 Connection conn = DriverManager.getConnection(url,user,pass);
这个对象的常用方法:
方法
描述
createStatement()
创建向数据库发送sql的statement对象。
prepareStatement(sql)
创建向数据库发送预编译sql的PrepareSatement对象。
prepareCall(sql)
创建执行存储过程的callableStatement对象。
setAutoCommit(boolean autoCommit)
设置事务是否自动提交。
commit()
在链接上提交事务。
rollback()
在此链接上回滚事务。
1 2 3 4 5 6 7 String url = "jdbc:mysql://localhost:3306/mall_system" ; String username = "egret" ; String password = "123456" ; Connection conn = null ; conn = DriverManager.getConnection(url, username, password);
3、执行SQL语句 1、Statement Jdbc程序中的Statement对象用于向数据库发送SQL语句,创建方法为:
1 Statement st = conn.createStatement();
Statement对象常用方法 :
方法
含义
executeQuery(String sql)
用于向数据发送查询语句。
executeUpdate(String sql)
用于向数据库发送insert、update或delete语句
execute(String sql)
用于向数据库发送任意sql语句
addBatch(String sql)
把多条sql语句放到一个批处理中。
executeBatch()
向数据库发送一批sql语句执行。
1 2 3 4 5 6 7 Statement st = null ; st = conn.createStatement(); String sql = "select id,name,password,email,birthday from users" ; st.executeQuery(sql);
2、PreperedStatement PreperedStatement是Statement的子类,它的实例对象的获取可以通过调用:
1 PreperedStatement st = conn.preparedStatement()
示例:
1 2 3 4 5 6 7 8 9 10 PreperedStatement st = null ; String sql = "select * from users where name=? and password=?" ; st = conn.preparedStatement(sql); st.setString(1 , username); st.setString(2 , password); st.executeQuery();
比较:相对于Statement对象而言
PreperedStatement可以避免SQL注入的问题。
1 2 3 4 5 6 7 如:String sql="select * from admin where loginname='" +loginName+"' and loginpwd='" +loginPwd+"'" ;
Statement会使数据库频繁编译SQL,可能造成数据库缓冲区溢出。PreparedStatement 可对SQL进行预编译,从而提高数据库的执行效率。
并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写。
4、获取结果 jdbc程序中的ResultSet用于代表Sql语句的执行结果。Resultset封装执行结果时,采用的类似于表格的方式,ResultSet 对象维护了一个指向表格数据行的游标,初始的时候,游标在第一行之前,调用ResultSet.next() 方法,可以使游标指向具体的数据行,进行调用方法获取该行的数据。
1、获取行 ResultSet提供了对结果集进行滚动的方法:
next():移动到下一行
Previous():移动到前一行
absolute(int row):移动到指定行
beforeFirst():移动resultSet的最前面。
afterLast() :移动到resultSet的最后面。
2、获取值 ResultSet既然用于封装执行结果的,所以该对象提供的都是用于获取数据的get方法:
附加:
常用数据类型转换:
SQL类型
Jdbc对应方法
返回类型
bit(1),bit(n)
getBoolean,getBytes()
Boolean,byte[]
tinyint
getByte()
Byte
smallint
getShort()
Short
int
getInt
Int
bigint
getLong()
Long
char,varchar,longvarchar
getString
String
text(clob) blob
getClob(),getblob()
Clob,blob
date
getDate()
java.sql.Date
time
getTime()
java.sql.Time
timestamp
getTimestamp
java.sql.Timestamp
1 2 3 4 5 6 7 8 9 10 11 12 13 ResultSet rs = null ; String sql = "select id,name,password,email,birthday from users" ; rs = st.executeQuery(sql); rs.afterLast(); rs.previous(); System.out.println("id=" + rs.getObject("id" )); System.out.println("name=" + rs.getObject("name" )); System.out.println("password=" + rs.getObject("password" )); System.out.println("email=" + rs.getObject("email" )); System.out.println("birthday=" + rs.getObject("birthday" ));
或者
1 2 3 4 5 6 while (rs.next()){ String id=rs.getString(1 ); System.out.println(id+" " ); }
5、释放资源 Jdbc程序运行完后,切记要释放程序在运行过程中,创建的那些与数据库进行交互的对象,这些对象通常是ResultSet, Statement和Connection对象。
注意:为确保资源释放代码能运行,资源释放代码也一定要放在finally语句中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 if (rs!=null ){ try { rs.close(); }catch (Exception e) { e.printStackTrace(); } rs = null ; } if (st!=null ){ try { st.close(); }catch (Exception e) { e.printStackTrace(); } } if (conn!=null ){ try { conn.close(); }catch (Exception e) { e.printStackTrace(); } }
三、基本操作 1、DDL 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 package com.shen.study2;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;public class Test1 { public static void main (String[] args) { Test1 test=new Test1(); } public Test1 () { this .lianjie(); } public void lianjie () { PreparedStatement ps=null ; Connection ct=null ; ResultSet rs=null ; try { Class.forName("com.mysql.cj.jdbc.Driver" ); ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/mall_system" ); ps=ct.prepareStatement("create database vvv" ); boolean b=ps.execute(); if (b) { System.out.println("创建成功!" ); }else { System.out.println("失败" ); } } catch (Exception e) { }finally { try { if (ps!=null ) ps.close(); if (ct!=null ) ct.close(); } catch (Exception e2) { } } } }
2、CRUD 实体类:user
1 2 3 4 5 6 7 8 9 public class User { private int id; private String name; private String password; private String email; private Date birthday; }
连接工具:JdbcUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties; public class JdbcUtils { private static String driver = null ; private static String url = null ; private static String username = null ; private static String password = null ; static { try { InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties" ); Properties prop = new Properties(); prop.load(in); driver = prop.getProperty("driver" ); url = prop.getProperty("url" ); username = prop.getProperty("username" ); password = prop.getProperty("password" ); Class.forName(driver); }catch (Exception e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection () throws SQLException { return DriverManager.getConnection(url, username,password); } public static void release (Connection conn,Statement st,ResultSet rs) { if (rs!=null ){ try { rs.close(); }catch (Exception e) { e.printStackTrace(); } rs = null ; } if (st!=null ){ try { st.close(); }catch (Exception e) { e.printStackTrace(); } } if (conn!=null ){ try { conn.close(); }catch (Exception e) { e.printStackTrace(); } } } }
资源文件:db.properties
1 2 3 4 driver =com.mysql.cj.jdbc.Driver url =jdbc:mysql://localhost:3306/mall_system username =egret password =123456
功能实现:Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement; import org.junit.Test; import cn.itcast.utils.JdbcUtils;. public class Demo { @Test public void insert () { Connection conn = null ; Statement st = null ; ResultSet rs = null ; try { conn = JdbcUtils.getConnection(); st = conn.createStatement(); String sql = "insert into users(id,name,password,email,birthday) values(4,'xxx','123','xx@sina.com',to_date('1980-09-09','YYYY-MM-DD'))" ; int num = st.executeUpdate(sql); if (num>0 ){ System.out.println("插入成功!!" ); } }catch (Exception e) { e.printStackTrace(); }finally { JdbcUtils.release(conn, st, rs); } } @Test public void delete () { Connection conn = null ; Statement st = null ; ResultSet rs = null ; try { conn = JdbcUtils.getConnection(); String sql = "delete from users where id=4" ; st = conn.createStatement(); int num = st.executeUpdate(sql); if (num>0 ){ System.out.println("删除成功!!" ); } }catch (Exception e) { }finally { JdbcUtils.release(conn, st, rs); } } @Test public void update () { Connection conn = null ; Statement st = null ; ResultSet rs = null ; try { conn = JdbcUtils.getConnection(); String sql = "update users set name='wuwang',email='wuwang@sina.com' where id=3" ; st = conn.createStatement(); int num = st.executeUpdate(sql); if (num>0 ){ System.out.println("更新成功!!" ); } }catch (Exception e) { }finally { JdbcUtils.release(conn, st, rs); } } @Test public void find () { Connection conn = null ; Statement st = null ; ResultSet rs = null ; try { conn = JdbcUtils.getConnection(); String sql = "select * from users where id=1" ; st = conn.createStatement(); rs = st.executeQuery(sql); if (rs.next()){ System.out.println(rs.getString("name" )); } }catch (Exception e) { }finally { JdbcUtils.release(conn, st, rs); } } }
3、JdbcUtil 完整的JdbcUtil工具包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 package cn.egret.util;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.net.URL;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;public class JdbcUtil { private static String url; private static String username; private static String password; private static String driver; static { Properties properties = new Properties(); try { ClassLoader classLoader = JdbcUtil.class.getClassLoader(); URL res = classLoader.getResource("jdbc.properties" ); String path = res.getPath(); System.out.println(path); properties.load(new FileReader(path)); url = properties.getProperty("url" ); username = properties.getProperty("username" ); password = properties.getProperty("password" ); driver = properties.getProperty("driver" ); Class.forName(driver); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection () throws SQLException { return DriverManager.getConnection(url, username, password); } public static boolean executeUpdate (String sql, Object... obj) { Connection con = null ; try { con = getConnection(); } catch (SQLException e1) { e1.printStackTrace(); } PreparedStatement ps = null ; try { ps = con.prepareStatement(sql); for (int i = 0 ; i < obj.length; i++) { ps.setObject(i + 1 , obj[i]); } int count = ps.executeUpdate(); if (count > 0 ) { return true ; } } catch (SQLException e) { e.printStackTrace(); } finally { close(null , ps, con); } return false ; } public static List<Map<String, Object>> executeQuery(String sql, Object... obj) { Connection con = null ; try { con = getConnection(); } catch (SQLException e1) { e1.printStackTrace(); } ResultSet rs = null ; PreparedStatement ps = null ; try { ps = con.prepareStatement(sql); for (int i = 0 ; i < obj.length; i++) { ps.setObject(i + 1 , obj[i]); } rs = ps.executeQuery(); List<Map<String, Object>> list = new ArrayList<>(); int count = rs.getMetaData().getColumnCount(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); for (int i = 0 ; i < count; i++) { Object ob = rs.getObject(i + 1 ); String key = rs.getMetaData().getColumnName(i + 1 ); map.put(key, ob); } list.add(map); } return list; } catch (SQLException e) { e.printStackTrace(); } finally { close(rs, ps, con); } return null ; } public static void close (ResultSet rs, PreparedStatement ps, Connection conn) { 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(); } } } } }
四、C3P0连接池 1、连接池定义 C3P0 是一个开源的JDBC连接池 ,它实现了数据源与JNDI绑定,支持JDBC3规范和实现了JDBC2的标准扩展说明的Connection和Statement池的DataSources对象。
C3P0 将用于连接数据库的连接 整合在一起形成一个随取随用的数据库连接池 (Connection pool)。
2、使用C3P0的必要性 当我们在进行基于数据库的web程序开发时,我们可以先在主程序(如Servlet、Bean)中通过JDBC中的DriverManager 建立数据库连接,然后将要对数据库进行操作的sql语句封装到Statement 中,最后在返回结果集后断开数据库连接 。以上是较为传统的开发模式,然而用这种模式开发会埋下严重的安全隐患。
时间和内存资源消耗巨大
普通的JDBC数据库连接使用DriverManager 来获取,每次向数据库建立连接的时候都要将Connection加载到内存中,再根据JDBC代码(或配置文件)中的用户名和密码进行验证其正确性。这一过程一般会花费0.05~1s ,一旦需要数据库连接的时候就必须向数据库请求一个,执行完后再断开连接。显然,如果同一个数据库在同一时间有数十人甚至上百人请求连接势必会占用大量的系统资源,严重的会导致服务器崩溃 。
有内存泄漏的风险
因为每一次数据库连接使用完后都需要断开连接,但如果程序出现异常致使连接未能及时关闭,这样就可能导致内存泄漏 ,最终只能以重启数据库的方法来解决;另外使用传统JDBC模式开发不能控制需要创建的连接数,系统一般会将资源大量分出给连接以防止资源不够用,如果连接数超出一定数量 也会有极大的可能导致内存泄漏 。
为了解决由使用传统开发模式 创建连接导致的一系列问题,我们可以采用数据库连接池技术 。
数据库连接池的基本原理就是为数据库建立一个缓冲池 。在缓冲池中先创建指定数量的数据库连接,当有连接请求时就从缓冲池中取出处于“空闲 ”状态的连接,并将此连接标记为“忙碌 ”,直到该请求进程结束后,它所使用的连接才会重新回到“空闲 ”状态,并等待下一次请求调用 。
数据库连接池的主要作用就是负责分配、管理和释放数据库连接 ,它允许程序重复使用同一个现有的数据库连接,大大缩短了运行时间,提高了执行效率。这里需要强调一点的是,数据库连接池中的连接数是在其初始化时根据c3p0-config.xml 中的最小连接数 来确定的。当然,无论连接池的连接数是否有被使用,它都至少会保持最小连接数,如果请求连接数超过最小连接数也会根据c3p0-config.xml 中指定的自增长数增加连接数直到达到最大连接数,这时如果请求连接数量还是大于连接池中的连接数的话,剩下的请求将会被放入等待队列直到有空闲连接出现 。
这样一来,数据库连接池 相较于传统JDBC模式 等到请求发出才创建连接的做法有着显而易见的优势 。
资源的高效利用
由于数据库连接得以重用,避免了频繁创建 ,释放连接引起的大量性能开销,减小了系统资源消耗的同时也提高了系统运行环境的平稳性。
更快的系统反应速度
数据库连接池在初始化过程中,往往已经创建了若干数据库连接置于连接池中备用 。此时连接的初始化工作均已完成。对于业务请求处理而言,直接利用现有可用连接 可以避免数据库在连接初始化和释放过程所需的时间开销,从而减少了系统的响应时间,提高了系统的反应速度 。
减少了资源独占的风险
新的资源分配手段对于多应用共享同一数据库的系统而言,可在应用层通过数据库连接池的配置实现对某一应用最大可用数据库连接数的限制,避免了应用独占所有数据库资源的风险 。
统一的连接管理,避免数据库连接泄露
在实现较为完善的数据库连接池时,可根据预先的占用超时设定,强制回收被占用连接 ,从而避免了常规数据库连接操作中可能出现的资源泄露。
3、使用步骤 3.1、导入jar包
3.2、配置c3p0-config.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 <?xml version="1.0" encoding="UTF-8"?> <c3p0-config > <named-config name ="mysql" > <property name ="driverClass" > com.mysql.cj.jdbc.Driver</property > <property name ="jdbcUrl" > jdbc:mysql://localhost:3306/mall_system?useUnicode=true& characterEncoding=UTF-8& userSSL=true& serverTimezone=Hongkong</property > <property name ="user" > root</property > <property name ="password" > 123456</property > <property name ="initialPoolSize" > 10</property > <property name ="maxPoolSize" > 20</property > <property name ="minPoolSize" > 5</property > <property name ="acquireIncrement" > 5</property > <property name ="maxIdleTime" > 30</property > <property name ="maxConnectorAge" > 30</property > <property name ="maxIdleTimeExcessConnection" > 5</property > <property name ="automaticTestTable" > null</property > <property name ="connectionTesterClassName" > com.mchange.v2.c3p0.impl.DefaultConnectionTester</property > <property name ="preferredTestQuery" > null</property > <property name ="idleConnectionTestPeriod" > 0</property > <property name ="testConnectionOnCheckin" > false</property > <property name ="testConnectionOnCheckout" > false</property > <property name ="maxStatements" > 0</property > <property name ="maxStatementsPerConnection" > 0</property > <property name ="statementCacheNumDeferredCloseThreads" > 0</property > <property name ="acquireRetryAttempts" > 30</property > <property name ="acquireRetryDelay" > 1000</property > <property name ="breakAfterAcquireFailure" > false</property > <property name ="autoCommitOnClose" > false</property > <property name ="forceIgnoreUnresolvedTransactions" > false</property > <property name ="checkoutTimeout" > 0</property > <property name ="factoryClassLocation" > null</property > <property name ="numHelperThreads" > 3</property > </named-config > </c3p0-config >
3.3、C3P0连接池的JdbcUtil 获取连接池的主要代码:
1 private static ComboPooledDataSource pool = new ComboPooledDataSource("mysql" );
JdbcUtil代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 package cn.egret.util;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.mchange.v2.c3p0.ComboPooledDataSource;public class JdbcUtil { private static ComboPooledDataSource pool; private JdbcUtil () { } static { pool = new ComboPooledDataSource("mysql" ); } public static Connection getConnection () { try { return pool.getConnection(); } catch (SQLException e) { e.printStackTrace(); return null ; } } public static void close (Connection con, Statement st, ResultSet rs) { try { try { if (rs != null ) { rs.close(); rs = null ; } } finally { try { if (st != null ) { st.close(); st = null ; } } finally { if (con != null ) { con.close(); con = null ; } } } } catch (SQLException e) { e.printStackTrace(); } } public static int queryForCount (String sql, Object... args) { Connection con = null ; PreparedStatement ps = null ; ResultSet rs = null ; int count = 0 ; try { con = JdbcUtil.getConnection(); ps = con.prepareStatement(sql); for (int i = 0 ; args != null && i < args.length; i++) { ps.setObject(i + 1 , args[i]); } rs = ps.executeQuery(); while (rs.next()) { count++; } return count; } catch (Exception e) { e.printStackTrace(); } finally { close(con, ps, rs); } return count; } public static <T> List<T> queryAll (String sql, Class<T> c, Object... args) { Connection con = null ; PreparedStatement ps = null ; ResultSet rs = null ; List<T> list = null ; try { con = pool.getConnection(); ps = con.prepareStatement(sql); for (int i = 0 ; args != null && i < args.length; i++) { ps.setObject(i + 1 , args[i]); } rs = ps.executeQuery(); Field fields[] = c.getDeclaredFields(); list = new ArrayList<T>(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { T o = c.newInstance(); for (int i = 0 ; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName(); Class<?> fieldType = field.getType(); String methodName = "set" + (fieldName.charAt(0 ) + "" ).toUpperCase() + fieldName.substring(1 ); Method method = c.getMethod(methodName, fieldType); if (isExists(rsmd, fieldName)) { Object value = rs.getObject(fieldName); if (value != null ) { method.invoke(o, value); } } } list.add(o); } } catch (Exception e) { e.printStackTrace(); } finally { close(con, ps, rs); } return list; } private static boolean isExists (ResultSetMetaData rsmd, String fieldName) { try { int count = rsmd.getColumnCount(); for (int i = 0 ; i < count; i++) { String columnName = rsmd.getColumnLabel(i + 1 ); if (columnName.equals(fieldName)) { return true ; } } } catch (SQLException e) { e.printStackTrace(); } return false ; } public static <T> T queryForObject (String sql, Class<T> c, Object... args) { List<T> list = queryAll(sql, c, args); if (list != null && list.size() > 0 ) { return list.get(0 ); } return null ; } public static int update (String sql, Object... args) { Connection con = null ; PreparedStatement ps = null ; try { con = JdbcUtil.getConnection(); ps = con.prepareStatement(sql); for (int i = 0 ; args != null && i < args.length; i++) { ps.setObject(i + 1 , args[i]); } return ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { close(con, ps, null ); } return 0 ; } }