ExtJS写的小系统,有源码

来源:互联网 发布:蓝牙4.0耳机知乎 编辑:程序博客网 时间:2024/04/28 17:00
Java代码 复制代码
  1. package com.soa.userbean;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.ResultSet;   
  5. import java.sql.ResultSetMetaData;   
  6. import java.sql.SQLException;   
  7. import java.sql.Statement;   
  8. import java.util.ArrayList;   
  9. import java.util.HashMap;   
  10. import java.util.Iterator;   
  11. import java.util.Map;   
  12. import java.util.Set;   
  13.   
  14. import javax.naming.Context;   
  15. import javax.naming.InitialContext;   
  16. import javax.sql.DataSource;   
  17.   
  18. import org.apache.log4j.Logger;   
  19.   
  20. /**  
  21.  * @author Julycn  
  22.  *   
  23.  */  
  24. public class InitDB {   
  25.      Connection conn=null;   
  26.      Statement stmt=null;   
  27.      ResultSet rs=null;   
  28.      ResultSetMetaData rsmd=null;   
  29.      static Logger logger=Logger.getLogger(InitDB.class);   
  30.        
  31.     //初始化数据库连接   
  32.     public static Connection InitDB(String jndiName) {   
  33.         Connection conn=null;   
  34.         try{   
  35.         Context initContext=(Context) new InitialContext().lookup("java:/comp/env");   
  36.         DataSource ds=(DataSource) initContext.lookup(jndiName);   
  37.         conn=ds.getConnection();   
  38.         }catch(Exception e){   
  39.             logger.error("链接数据库失败:"+e);   
  40.         }   
  41.         return conn;   
  42.     }   
  43.        
  44.   
  45.     //直接执行sql,用于更新,删除,添加   
  46.     public boolean execute(String sql,String jndiName){   
  47.            
  48.         conn=InitDB.InitDB(jndiName);   
  49.         try{   
  50.             if(conn!=null){   
  51.                 stmt=conn.createStatement();   
  52.                 stmt.execute(sql);   
  53.                    
  54.                 return true;   
  55.             }   
  56.         }catch(Exception e){   
  57.             logger.error("操作数据库失败:"+e);   
  58.         }finally{   
  59.             try {   
  60.                 stmt.close();   
  61.                 conn.close();   
  62.             } catch (SQLException e) {   
  63.                 logger.error("关闭数据库失败:"+e);   
  64.             }   
  65.         }   
  66.         return false;   
  67.     }   
  68.        
  69.        
  70.     //获取记录数   
  71.     public int findCount(String sql,String jndiName){   
  72.         conn=InitDB.InitDB(jndiName);   
  73.         int rowCount=0;   
  74.         try {   
  75.             stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);   
  76.             rs=stmt.executeQuery(sql);   
  77.             rs.last();   
  78.             rowCount=rs.getRow();   
  79.         } catch (SQLException e) {   
  80.             e.printStackTrace();   
  81.         }finally{   
  82.             try {   
  83.                 rs.close();   
  84.                 stmt.close();   
  85.                 conn.close();   
  86.             } catch (SQLException e) {   
  87.                 e.printStackTrace();   
  88.             }   
  89.         }   
  90.         return rowCount;   
  91.     }   
  92.        
  93.     //通过sql语句获取记录数,返回List   
  94.     public ArrayList<Object> findBySql(String sql,String jndiName){   
  95.            
  96.         ArrayList<Object> list=new ArrayList<Object>();   
  97.            
  98.         try{   
  99.             conn=InitDB.InitDB(jndiName);   
  100.             stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   
  101.             rs=stmt.executeQuery(sql);   
  102.             rsmd=rs.getMetaData();   
  103.                
  104.             String columnName="";   
  105.             Object columnValue=null;   
  106.   
  107.             while(rs.next()){   
  108.                 String array="";   
  109.                 array+="{";   
  110.                 for(int j=1;j<=rsmd.getColumnCount();j++){   
  111.                     columnName=rsmd.getColumnName(j);   
  112.                     columnValue=rs.getObject(columnName);   
  113.                     array+="/""+columnName+"/"";   
  114.                     array+=":";   
  115.                     array+="/""+columnValue+"/"";   
  116.                     if(j<rsmd.getColumnCount()){   
  117.                         array+=",";   
  118.                     }else{   
  119.                         array+="";   
  120.                     }   
  121.                 }   
  122.                 array+="}";   
  123.                    
  124.                 list.add(array);   
  125.             }   
  126.                
  127.         }catch(Exception e){   
  128.             e.printStackTrace();   
  129.         }finally{   
  130.             try {   
  131.                 rs.close();   
  132.                 stmt.close();   
  133.                 conn.close();   
  134.             } catch (SQLException e) {   
  135.                 e.printStackTrace();   
  136.             }   
  137.         }   
  138.         return list;   
  139.     }   
  140.        
  141.     //把数据集处理成Ext可以接受的格式   
  142.     public String toExtJson(String table,String sqlContent,int start,int limit,Object cons,String jndiName){   
  143.         String sql=generateSql(table,sqlContent, cons);   
  144.         System.out.println(sql);   
  145.         int totalNum=findCount(sql, jndiName);   
  146.         ArrayList<Object> list=findBySql(sql+" LIMIT "+limit+" OFFSET "+start, jndiName);   
  147.         int resultNum=list.size();   
  148.         String str="";   
  149.         str+="";   
  150.         str+="{";   
  151.         str+="'totalCount':'"+totalNum+"',";   
  152.         str+="'rows':";   
  153.         str+="[";   
  154.         for(int i=0;i<resultNum;i++){   
  155.             str+="";   
  156.             str+=list.get(i);   
  157.             if(i<resultNum-1){   
  158.                 str+=",";                  
  159.             }else{   
  160.                 str+="";   
  161.             }   
  162.         }   
  163.         str+="]";   
  164.         str+="}";   
  165.         return str;   
  166.     }   
  167.        
  168.        
  169.     // 通过条件生存sql语句   
  170.     @SuppressWarnings("unchecked")   
  171.     public String generateSql(String table, String sqlContent,Object cons) {   
  172.         String sql = "";   
  173.         if(sqlContent==null){   
  174.             sqlContent="*";   
  175.         }   
  176.         sql = "select "+sqlContent+" from " + table;   
  177.         HashMap hashMap=null;   
  178.         if (cons != null) {   
  179.             if(cons instanceof HashMap){   
  180.                 hashMap=(HashMap) cons;   
  181.                 Set set=hashMap.entrySet();   
  182.                 Iterator it=set.iterator();   
  183.                 int k=0;   
  184.                 while(it.hasNext()){   
  185.                     Map.Entry me=(Map.Entry) it.next();   
  186.                     if(k==0){   
  187.                         sql+=" where ";   
  188.                         sql+=me.getKey();   
  189.                         sql+="=";   
  190.                         sql+=me.getValue();   
  191.                     }else{   
  192.                         sql+=" and ";   
  193.                         sql+=me.getKey();   
  194.                         sql+="=";   
  195.                         sql+=me.getValue();   
  196.                     }   
  197.                     k++;                       
  198.                 }   
  199.             }else{   
  200.                 sql+=" where "+cons;   
  201.             }   
  202.         }   
  203.   
  204.         return sql;   
  205.   
  206.     }   
  207. }  

 

  • SOA.rar (4.3 MB)
  • 下载次数: 3738
  • sql.rar (1.2 KB)
  • 下载次数: 1393