zoukankan      html  css  js  c++  java
  • ExtJS写的小系统,有源码,献给刚入门的朋友。


    http://www.javaeye.com/topic/308362
    接到任务,要写一个项目协作系统,任务不急,嘿嘿,用extjs试试看
    这个系统是部门间用来扯皮用的,md又被当枪头了
    自学了1个礼拜,就开始操刀了,代码很简单,模块化,希望对学习extjs的朋友有帮助,也帮俺的blog增加点人气(这样说话,不会扣分吧 ) 嘿嘿 。

    献上源码,供大家参考

    json格式化很方便,把取出来的数据,传给json格式化的类,就可以了。

    这个类是处理sql语句,返回数据,和格式化json

    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. }  
    package com.soa.userbean; import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import org.apache.log4j.Logger; /** * @author Julycn * */ public class InitDB { Connection conn=null; Statement stmt=null; ResultSet rs=null; ResultSetMetaData rsmd=null; static Logger logger=Logger.getLogger(InitDB.class); //初始化数据库连接 public static Connection InitDB(String jndiName) { Connection conn=null; try{ Context initContext=(Context) new InitialContext().lookup("java:/comp/env"); DataSource ds=(DataSource) initContext.lookup(jndiName); conn=ds.getConnection(); }catch(Exception e){ logger.error("链接数据库失败:"+e); } return conn; } //直接执行sql,用于更新,删除,添加 public boolean execute(String sql,String jndiName){ conn=InitDB.InitDB(jndiName); try{ if(conn!=null){ stmt=conn.createStatement(); stmt.execute(sql); return true; } }catch(Exception e){ logger.error("操作数据库失败:"+e); }finally{ try { stmt.close(); conn.close(); } catch (SQLException e) { logger.error("关闭数据库失败:"+e); } } return false; } //获取记录数 public int findCount(String sql,String jndiName){ conn=InitDB.InitDB(jndiName); int rowCount=0; try { stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=stmt.executeQuery(sql); rs.last(); rowCount=rs.getRow(); } catch (SQLException e) { e.printStackTrace(); }finally{ try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return rowCount; } //通过sql语句获取记录数,返回List public ArrayList<Object> findBySql(String sql,String jndiName){ ArrayList<Object> list=new ArrayList<Object>(); try{ conn=InitDB.InitDB(jndiName); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery(sql); rsmd=rs.getMetaData(); String columnName=""; Object columnValue=null; while(rs.next()){ String array=""; array+="{"; for(int j=1;j<=rsmd.getColumnCount();j++){ columnName=rsmd.getColumnName(j); columnValue=rs.getObject(columnName); array+="\""+columnName+"\""; array+=":"; array+="\""+columnValue+"\""; if(j<rsmd.getColumnCount()){ array+=","; }else{ array+=""; } } array+="}"; list.add(array); } }catch(Exception e){ e.printStackTrace(); }finally{ try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return list; } //把数据集处理成Ext可以接受的格式 public String toExtJson(String table,String sqlContent,int start,int limit,Object cons,String jndiName){ String sql=generateSql(table,sqlContent, cons); System.out.println(sql); int totalNum=findCount(sql, jndiName); ArrayList<Object> list=findBySql(sql+" LIMIT "+limit+" OFFSET "+start, jndiName); int resultNum=list.size(); String str=""; str+=""; str+="{"; str+="'totalCount':'"+totalNum+"',"; str+="'rows':"; str+="["; for(int i=0;i<resultNum;i++){ str+=""; str+=list.get(i); if(i<resultNum-1){ str+=","; }else{ str+=""; } } str+="]"; str+="}"; return str; } // 通过条件生存sql语句 @SuppressWarnings("unchecked") public String generateSql(String table, String sqlContent,Object cons) { String sql = ""; if(sqlContent==null){ sqlContent="*"; } sql = "select "+sqlContent+" from " + table; HashMap hashMap=null; if (cons != null) { if(cons instanceof HashMap){ hashMap=(HashMap) cons; Set set=hashMap.entrySet(); Iterator it=set.iterator(); int k=0; while(it.hasNext()){ Map.Entry me=(Map.Entry) it.next(); if(k==0){ sql+=" where "; sql+=me.getKey(); sql+="="; sql+=me.getValue(); }else{ sql+=" and "; sql+=me.getKey(); sql+="="; sql+=me.getValue(); } k++; } }else{ sql+=" where "+cons; } } return sql; } }
    • D0b9c019-13a1-38df-964b-4f93a32b7215-thumb
    • 大小: 55.7 KB
    • 084aae9f-49b4-3db1-ad2c-78475bc407e8-thumb
    • 大小: 31.2 KB
    • 3d472955-6f25-35a6-b603-e5e7c1d62138-thumb
    • 大小: 40.8 KB
    • 3d13e73d-379f-3296-8b06-24552abd3a6a-thumb
    • 大小: 35.2 KB
    • SOA.rar (4.3 MB)
    • 下载次数: 2161
    • sql.rar (1.2 KB)
    • 下载次数: 880
  • 相关阅读:
    Java 基础:继承中的执行顺序
    Java 基础:抽象类与接口
    ElasticSearch : 基础简介
    Java GUI : 实现排序算法的动态演示
    Java GUI:将JPanel添加进JScrollPane
    菜鸟的算法入门:java的链表操作
    Docker与Tomcat:去掉项目名称进行访问
    vue输入框的搜索返回信息高亮显示
    position: static 和 position: unset
    vue css /deep/ 用法
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400782.html
Copyright © 2011-2022 走看看