zoukankan      html  css  js  c++  java
  • Preparestatement 反射创建对象

    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;

    }

  • 相关阅读:
    Review Python装饰器
    Python自动化开发三元运算 列表解析 生成器表达式
    Python自动化开发函数02
    Python自动化开发函数03
    Python自动化开发文件
    ELK02ELK收集Linux系统平台应用系统日志
    ELK01Elasticsearch
    html5调用摄像头并拍照
    Docker 安装 PostgreSQL
    《TensorFlow+Keras自然语言处理实战》图书介绍
  • 原文地址:https://www.cnblogs.com/afterhours/p/7289847.html
Copyright © 2011-2022 走看看