public class TestJDBC {
@Test
public void test3(){
// String sql = "select id, name, email, birth from customers where id <10";
// List<Customer> list = getForList(sql, Customer.class);
// for(Customer customer : list){
// System.out.println(customer);
// }
String sql1 = "select order_id id, order_name name, order_date date from `order` where order_id <3";
List<Order> list1 = getForList(sql1, Order.class);
for(Order order : list1){
System.out.println(order);
}
}
// 通用的查询方法,返回多个对象的集合(version 1.0)
public <T> List<T> getForList(String sql, Class<T> clazz){
Connection connection = null;
Statement s = null;
ResultSet rs = null;
try {
connection = JDBCUtils.getConnection();
s = connection.createStatement();
rs = s.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
//获取列的个数
int count = rsmd.getColumnCount();
List<T> list = new ArrayList<T>();
while(rs.next()){
T t = clazz.newInstance();
for(int i=0;i<count; i++){
Object columnValue = rs.getObject(i+1);// 获取列值
// getColumnLabel():获取列的别名,此别名由java类的属性名决定
String columnLable = rsmd.getColumnLabel(i+1);
PropertyUtils.setProperty(t, columnLable, columnValue);
}
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(rs, s, connection);
}
return null;
}
@Test
public void test2(){
String sql = "select id, name, email, birth from customers where id = 22;";
Customer customer = getInstance(sql,Customer.class);
System.out.println(customer);
}
// 通用的查询方法,返回一个对象(version 1.0)
public <T>T getInstance(String sql, Class<T> clazz){
Connection connection = null;
Statement s = null;
ResultSet rs = null;
try {
connection = JDBCUtils.getConnection();
s = connection.createStatement();
rs = s.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
// getColumnCount():获取结果集的列数
int count = rsmd.getColumnCount();
while(rs.next()){
T t = clazz.newInstance();
for(int i=0; i<count; i++){
// 获取列值
Object columnValue = rs.getObject(i + 1);
// getColumnLabel():获取列的别名,此别名由java类的属性名决定
String columnLable = rsmd.getColumnLabel(i + 1);
PropertyUtils.setProperty(t, columnLable, columnValue);
}
return t;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(rs, s, connection);
}
return null;
}
@Test
public void test(){
String sql = "insert into customers(name, email, birth) values('shawn', 'shawn@163.com', '1998-09-09')";
update(sql);
}
// 通用的增、删、改的方法(version 1.0)
public void update(String sql){
Connection connection = null;
Statement s = null;
try {
connection = JDBCUtils.getConnection();
s = connection.createStatement();
s.execute(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(null, s, connection);
}
}
@Test
public void test1() throws Exception{
Connection connection = JDBCUtils.getConnection();
System.out.println(connection);
}
}
- 浏览: 6574 次
相关推荐
java statement一次执行多条不同类型的sql语句-附件资源
prepareStatement和Statement的区别
hsbc statement template
Statement Tracer for Oracle绿色
project scope statement template
爱思唯尔旗下的Credit Author Statement模板 论文已正式出版~ 相关博客:https://blog.csdn.net/qq_39763246/article/details/126797385
项目范围说明书(Project Scope Statement)适用于各个行业的范围说明书,中英文对照标题
simple templet of income statement in english
jdbc中statement和prepared区别,jdbc中statement和prepared区别
Generate code statement for hardcode
SQL Statement,一些sql语句的基本语句,方便学习数据库
主要介绍了MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found)的Error处理方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
statement-tracer-for-oracle 跟踪器
java数据库连接PrepareStatement
NULL 博文链接:https://lao1984wang.iteye.com/blog/977055
Statement和PreparedStatement之间的区别
报错信息:Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See ...
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql... 当指针定位到一行时,可以通过调用getXxx(index) 方法或 getXxx(columnName) 方法获取 * 每一列的值。例如:getI
Statement Tracer for ADO 绿色版,捕捉ADO SQL代码.