jdbc 反射赋值

/***

jdbc连接获取请参考 文章-----jdbc连接池配置

*/

 

 


import java.lang.reflect.Field;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.lottery.db.PoolManager;

@SuppressWarnings("unchecked")
public class SuperDao
{
 /**
  * 用存储过程查询单个实例结果集
  *
  * @procName 存储过程名称
  * @clazz JavaBean的class类型,注意JavaBean全用对象类型,不能用原生类型(赋值时JavaBean无需生成get,set方法 )
  * @args 动态参数值列表
  */
 public Object queryByProc(String procName, Class clazz, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   String str = "{call ";
   str += procName;
   str += "(";
   // 防止注入不直接将参数传入
   for (int i = 0;i < args.length;i++ )
   {
    if(i < args.length - 1)
    {
     str += "?,";
    }
    else
    {
     str += "?";
    }
   }
   str += ")";
   str += " }";
   CallableStatement cstmt = conn.prepareCall (str);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   if(rs.next ())
   {
    // 利用Java反射给JavaBean赋值
    Object o = clazz.newInstance ();
    Class c = o.getClass ();
    Field [] fields = c.getDeclaredFields ();
    for (int i = 0;i < fields.length;i++ )
    {
     Field f = fields [i];
     f.setAccessible (true);
     String strType = f.getType ().toString ();
     if(strType.indexOf ("Long") != -1)
     {
      f.set (o,rs.getLong (f.getName ()));
     }
     if(strType.indexOf ("Integer") != -1)
     {
      f.set (o,rs.getInt (f.getName ()));
     }
     if(strType.indexOf ("String") != -1)
     {
      f.set (o,rs.getString (f.getName ()));
     }
     if(strType.indexOf ("Date") != -1)
     {
      f.set (o,rs.getDate (f.getName ()));
     }
     if(strType.indexOf ("Double") != -1)
     {
      f.set (o,rs.getDouble (f.getName ()));
     }
    }
    return o;
    
   }
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return null;
 }
 
 /**
  * 存储过程查询 return单个实例结果集
  *
  * @procName 存储过程名称
  * @args 动态参数值列表
  */
 public Object executeByReturnProc(String procName, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   String str = "{?=call ";
   str += procName;
   str += "(";
   // 防止注入不直接将参数传入
   for (int i = 0;i < args.length;i++ )
   {
    if(i < args.length - 1)
    {
     str += "?,";
    }
    else
    {
     str += "?";
    }
   }
   str += ")";
   str += " }";
   CallableStatement cstmt = conn.prepareCall (str);
   cstmt.registerOutParameter (1,Types.INTEGER); //
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    // return 从2开始
    cstmt.setString (i + 2,args [i]);
   }
   cstmt.execute ();
   return cstmt.getObject (1);
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return null;
 }
 
 /**
  * 存储过程查询 select单个实例结果集
  *
  * @procName 存储过程名称
  * @args 动态参数值列表
  */
 public Object executeBySelectProc(String procName, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   String str = "{call ";
   str += procName;
   str += "(";
   // 防止注入不直接将参数传入
   for (int i = 0;i < args.length;i++ )
   {
    if(i < args.length - 1)
    {
     str += "?,";
    }
    else
    {
     str += "?";
    }
   }
   str += ")";
   str += " }";
   CallableStatement cstmt = conn.prepareCall (str);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    // return 从2开始
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   if(rs.next ())
   {
    return rs.getObject (1);
   }
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return null;
 }
 
 /**
  * @author zdw 用存储过程查询简单类型结果集
  * @procName 存储过程名称
  * @args 动态参数值列表
  */
 public String queryByProc(String procName, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   String str = "{call ";
   str += procName;
   str += "(";
   // 防止注入不直接将参数传入
   for (int i = 0;i < args.length;i++ )
   {
    if(i < args.length - 1)
    {
     str += "?,";
    }
    else
    {
     str += "?";
    }
   }
   str += ")";
   str += " }";
   System.out.println ("str:" + str);
   CallableStatement cstmt = conn.prepareCall (str);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   if(rs.next ())
   {
    return rs.getString (1);
   }
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  
  return null;
 }
 
 /**
  * 用存储过程查询多个实例结果集
  *
  * @procName 存储过程名称
  * @clazz JavaBean的class类型,注意JavaBean全用对象类型,不能用原生类型(赋值时JavaBean无需生成get,set方法 )
  * @args 动态参数值列表
  */
 public List queryListByProc(String procName, Class clazz, String...args)
 {
  Connection conn = null;
  List list = new ArrayList ();
  try
  {
   conn = PoolManager.getConnection ();
   String str = "{call ";
   str += procName;
   str += "(";
   // 防止注入不直接将参数传入
   for (int i = 0;i < args.length;i++ )
   {
    if(i < args.length - 1)
    {
     str += "?,";
    }
    else
    {
     str += "?";
    }
   }
   str += ")";
   str += " }";
   System.out.println ("str:" + str);
   CallableStatement cstmt = conn.prepareCall (str);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   while (rs.next ())
   {
    // 利用Java反射给JavaBean赋值
    Object o = clazz.newInstance ();
    Class c = o.getClass ();
    Field [] fields = c.getDeclaredFields ();
    for (int i = 0;i < fields.length;i++ )
    {
     Field f = fields [i];
     f.setAccessible (true);
     String strType = f.getType ().toString ();
     if(strType.indexOf ("Long") != -1)
     {
      f.set (o,rs.getLong (f.getName ()));
     }
     if(strType.indexOf ("Integer") != -1)
     {
      f.set (o,rs.getInt (f.getName ()));
     }
     if(strType.indexOf ("String") != -1)
     {
      f.set (o,rs.getString (f.getName ()));
     }
     if(strType.indexOf ("Date") != -1)
     {
      f.set (o,rs.getDate (f.getName ()));
     }
     if(strType.indexOf ("Double") != -1)
     {
      f.set (o,rs.getDouble (f.getName ()));
     }
    }
    list.add (o);
    
   }
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  
  return list;
 }
 
 /**
  * 用原生的sql语句查询多个实例结果集
  *
  * @sql 原生的sql语句
  * @clazz JavaBean的class类型,注意JavaBean全用对象类型,不能用原生类型(赋值时JavaBean无需生成get,set方法 )
  * @args 动态参数值列表
  */
 public List queryListBySql(String sql, Class clazz, String...args)
 {
  List list = new ArrayList ();
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   PreparedStatement cstmt = conn.prepareStatement (sql);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   while (rs.next ())
   {
    // 利用Java反射给JavaBean赋值
    Object o = clazz.newInstance ();
    Class c = o.getClass ();
    Field [] fields = c.getDeclaredFields ();
    for (int i = 0;i < fields.length;i++ )
    {
     Field f = fields [i];
     f.setAccessible (true);
     String strType = f.getType ().toString ();
     if(strType.indexOf ("Long") != -1)
     {
      f.set (o,rs.getLong (f.getName ()));
     }
     if(strType.indexOf ("Integer") != -1)
     {
      f.set (o,rs.getInt (f.getName ()));
     }
     if(strType.indexOf ("String") != -1)
     {
      f.set (o,rs.getString (f.getName ()));
     }
     if(strType.indexOf ("Date") != -1)
     {
      f.set (o,rs.getDate (f.getName ()));
     }
     if(strType.indexOf ("Double") != -1)
     {
      f.set (o,rs.getDouble (f.getName ()));
     }
    }
    list.add (o);
   }
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return list;
 }
 
 /**
  * 用原生的sql语句查询单个对象
  *
  * @sql 原生的sql语句
  * @clazz JavaBean的class类型,注意JavaBean全用对象类型,不能用原生类型(赋值时JavaBean无需生成get,set方法 )
  * @args 动态参数值列表
  */
 public Object queryByClassSql(String sql, Class clazz, String...args)
 {
  Object o = null;
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   PreparedStatement cstmt = conn.prepareStatement (sql);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   if(rs.next ())
   {
    // 利用Java反射给JavaBean赋值
    o = clazz.newInstance ();
    Class c = o.getClass ();
    Field [] fields = c.getDeclaredFields ();
    for (int i = 0;i < fields.length;i++ )
    {
     Field f = fields [i];
     f.setAccessible (true);
     String strType = f.getType ().toString ();
     if(strType.indexOf ("Long") != -1)
     {
      f.set (o,rs.getLong (f.getName ()));
     }
     if(strType.indexOf ("Integer") != -1)
     {
      f.set (o,rs.getInt (f.getName ()));
     }
     if(strType.indexOf ("String") != -1)
     {
      f.set (o,rs.getString (f.getName ()));
     }
     if(strType.indexOf ("Date") != -1)
     {
      f.set (o,rs.getDate (f.getName ()));
     }
     if(strType.indexOf ("Double") != -1)
     {
      f.set (o,rs.getDouble (f.getName ()));
     }
    }
   }
   
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return o;
 }
 
 /**
  * 用原生的sql语句查询Object 值
  *
  * @sql 原生的sql语句
  * @args 动态参数值列表
  */
 public Object queryBySql(String sql, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   PreparedStatement cstmt = conn.prepareStatement (sql);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   ResultSet rs = cstmt.executeQuery ();
   if(rs.next ())
   {
    return rs.getObject (1);
   }
   
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return null;
 }
 
 /**
  * 用原生的sql语句操作(insert ,update,delete)
  *
  * @sql 原生的sql语句
  * @args 动态参数值列表
  */
 public boolean executeBySql(String sql, String...args)
 {
  Connection conn = null;
  try
  {
   conn = PoolManager.getConnection ();
   conn.setAutoCommit (false);
   PreparedStatement cstmt = conn.prepareStatement (sql);
   // 给?赋值,传参
   for (int i = 0;i < args.length;i++ )
   {
    cstmt.setString (i + 1,args [i]);
   }
   
   cstmt.execute ();
   conn.commit ();
   conn.setAutoCommit (true);
   return true;
  }catch (Exception e)
  {
   e.printStackTrace ();
  }
  finally
  {
   PoolManager.freeConnection (conn);
  }
  return false;
 }

}

你可能感兴趣的:(sql,C++,c,jdbc,F#)