• 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;

    }

  • 相关阅读:
    The following untracked working tree files would be overwritten by merge
    如何删除git远程仓库项目的所有内容,重新提交所有内容
    Vue中引入bootstrap导致的CSS问题
    CSS实现垂直居中
    RESTful三问
    SAPUI5 freestyle vs SAP Fiori Elements —— 两种开发SAPUI5 Apps的方式对比
    团队1
    用户规格说明书
    MElv2.kkkK
    ME.kkkK
  • 原文地址:https://www.cnblogs.com/afterhours/p/7289847.html
Copyright © 2020-2023  润新知