码迷,mamicode.com
首页 > 其他好文 > 详细

Preparestatement 反射创建对象

时间:2017-08-05 13:20:47      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:puts   put   resultset   close   etc   manager   stat   ace   ref   

package com.hu;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.ResultSetMetaData;

public class Tools {
public static Connection connection=null;
public static PreparedStatement preparedStatement=null;
public static ResultSet resultSet=null;

public static String DriverClass=null;
public static String Url=null;
public static String user=null;
public static String PassWord=null;

static Properties properties=null;
public static Connection getConnection() throws Exception{
InputStream inputStream=Tools.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties=new Properties();
properties.load(inputStream);
DriverClass=properties.getProperty("DriverClass");
Url=properties.getProperty("Url");
user=properties.getProperty("User");
PassWord=properties.getProperty("PassWord");
Class.forName(DriverClass);
connection=DriverManager.getConnection(Url, user, PassWord);
return connection;

}
public static void update(String sql,Object...args) throws Exception{
connection=getConnection();
preparedStatement=(PreparedStatement) connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedStatement.setObject(i+1, args[i]);
}
preparedStatement.executeUpdate();
}
public static void release(Connection connection,PreparedStatement preparedStatement,ResultSet
resultSet,ResultSetMetaData resultSetMetaData){
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

 

查询方法的测试方法

public <T> T getT(Class<T> clazz,String sql,Object...args) throws Exception{
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
ResultSetMetaData rsmd=null;
T entity=null;

connection=Tools.getConnection();
preparedStatement=(PreparedStatement) connection.prepareStatement(sql);
for(int i=0;i<args.length;i++){
preparedStatement.setObject(i+1, args[i]);
}
resultSet=(ResultSet) preparedStatement.executeQuery();
rsmd=(ResultSetMetaData) resultSet.getMetaData();
Map<String, Object> map=new HashMap<>();
while(resultSet.next()){
for(int i=0;i<rsmd.getColumnCount();i++){
String labelString=rsmd.getColumnLabel(i+1);
Object valueObject=resultSet.getObject(labelString);
map.put(labelString, valueObject);
}
}
if(map.size()>0){
entity=clazz.newInstance();
for(Map.Entry<String, Object> entry:map.entrySet()){
String fieldString=entry.getKey();
Object fieldoObject=entry.getValue();
ReflectionUtils.setFieldValue(entity, fieldString, fieldoObject);
}
}
return entity;

}

 

Preparestatement 反射创建对象

标签:puts   put   resultset   close   etc   manager   stat   ace   ref   

原文地址:http://www.cnblogs.com/afterhours/p/7289847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!