利用反射机制模拟Hibernate的Session

Java代码 复制代码  收藏代码
  1. package com.quan.hibernate.model;   
  2.   
  3. import java.lang.reflect.Method;   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.PreparedStatement;   
  7. import java.sql.SQLException;   
  8. import java.util.HashMap;   
  9. import java.util.Map;   
  10.   
  11. /**  
  12.  * Session的模拟  
  13.  */  
  14. public class create {   
  15.   
  16.     String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hibernate";   
  17.     String user = "sa";   
  18.     String password = "123";   
  19.        
  20.     String tableName = "_Student";   
  21.     Map<String,String> cfs = new HashMap<String, String>();   
  22.        
  23.     String[] methodNames;   
  24.        
  25.     public create() {   
  26.         cfs.put("_id""id");   
  27.         cfs.put("_name","name");   
  28.         cfs.put("_age""age");   
  29.         methodNames = new String[cfs.size()];   
  30.     }   
  31.        
  32.     public void save(Student s) throws Exception {   
  33.         String sql = createSQL();   
  34.         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");   
  35.         Connection conn = DriverManager.getConnection(url, user, password);   
  36.         PreparedStatement ps = conn.prepareStatement(sql);   
  37.            
  38.         for(int i = 0; i<methodNames.length;i++) {   
  39.               Method m = s.getClass().getMethod(methodNames[i]);   
  40.               Class r= m.getReturnType();   
  41.               if(r.getName() == "java.lang.String") {   
  42.                   String returnValue = (String)m.invoke(s);   
  43.                   ps.setString(i+1, returnValue);   
  44.               }   
  45.               if(r.getName() == "int") {   
  46.                   Integer returnValue = (Integer)m.invoke(s);   
  47.                   ps.setInt(i+1, returnValue);   
  48.               }   
  49.         }   
  50.            
  51.         /*for(int i = 0; i<cfs.size(); i++) {  
  52.             ps.setXXX(s.getXXX);  
  53.         }*/  
  54.            
  55.         ps.executeUpdate();   
  56.         ps.close();   
  57.         conn.close();   
  58.     }   
  59.   
  60.     private String createSQL() {   
  61.        
  62.         String str1 = "";   
  63.         String str2 = "";   
  64.         int index = 0;   
  65.         for(String s : cfs.keySet()) {   
  66.             String v = cfs.get(s);   
  67.             v= Character.toUpperCase(v.charAt(0)) + v.substring(1);   
  68.             methodNames[index] = "get" + v;   
  69.             str1 += s + ",";   
  70.         }   
  71.         str1 = str1.substring(0, str1.length()-1);   
  72.            
  73.         for(int i = 0; i<cfs.size();i++) {   
  74.             str2 += "?,";   
  75.         }   
  76.         str2 = str2.substring(0, str2.length()-1);   
  77.            
  78.         String sql = "insert into "+tableName+"("+str1+")"+"valses ("+str2+")";   
  79.            
  80.         return sql;   
  81.     }   
  82. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值