标签: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;
}
标签:puts put resultset close etc manager stat ace ref
原文地址:http://www.cnblogs.com/afterhours/p/7289847.html