熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

高靈活的SQL使用

2022-06-13   來源: Java核心技術 
 

  /** * @return執行sql語句(Insertpdatedelete) */ public void execMySQL(String inSQL) throws SQLException {   sql = inSQL;   util = new DBManager();   conn = utilgetConn();   connsetAutoCommit(false); // setAutoCommit(false);   try {    utilgetStmt()execute(sql);    mit();   } catch (SQLException e) {    // TODO Autogenerated catch block    connrollback();    eprintStackTrace();   }   utilclose();  }  /** * @return執行sql語句(select) */

  public MyTable execMySQLSelect(String inSQL) {   MyFunc func = new MyFunc();   sql = inSQL;   //list = new ArrayList();   util = new DBManager();   MyTable kfse = new MyTable();   String s;   int type;   int iValue;   List rows = new ArrayList();   Integer itg = new Integer();   BigDecimal ftg = new BigDecimal();   javasqlDate date;   try {    rs = utilgetStmt()executeQuery(sql);    ResultSetMetaData rsmd = rsgetMetaData();//可用於獲取關於 ResultSet 對象中列的類型和屬性信息的對象    String[] cols = new String[rsmdgetColumnCount()];//rsmdgetColumnCount()返回此 ResultSet 對象中的列數    String[] colsType = new String[rsmdgetColumnCount()];    for (int i = colslength; i > ; i) {     cols[i ] = rsmdgetColumnName(i);//rsmdgetColumnName(i)獲取指定列的名稱     colsType[i ] = StringvalueOf(rsmdgetColumnType(i));//rsmdgetColumnType(i)檢索指定列的 SQL 類型    }    if (rs != null) {     while (rsnext()) {      Object[] row = new Object[colslength];      rowsadd(row);      for (int i = colslength; i > ; i) {       //row[i ] = rsgetString(cols[i ]);       type = rsmdgetColumnType(i);       //row[i ] = StringvalueOf(type);       switch (type) {//type數值參照javasqltypes        case :         row[i ] = rsgetString(cols[i ]);         break;        case :         ftg = new BigDecimal();         if (rsgetBigDecimal(cols[i ]) != null)          ftg = rsgetBigDecimal(cols[i ]);

  row[i ] = ftg;         break;        case :         ftg = new BigDecimal();         if (rsgetBigDecimal(cols[i ]) != null)          ftg = rsgetBigDecimal(cols[i ]);

  row[i ] = ftg;         break;        case : //對u 數u 庫u 段NTEGER//ok         itg = new Integer(rsgetInt(cols[i ]));         row[i ] = itg;         break;        case :         itg = new Integer(rsgetInt(cols[i ]));         row[i ] = itg;         break;        case :         ftg = new BigDecimal();         if (rsgetBigDecimal(cols[i ]) != null)          ftg = rsgetBigDecimal(cols[i ]);

  row[i ] = ftg;         break;        case :         row[i ] =          new Float(rsgetFloat(cols[i ]));         break;        case :         ftg = new BigDecimal();         if (rsgetBigDecimal(cols[i ]) != null)          ftg = rsgetBigDecimal(cols[i ]);

  row[i ] = ftg;         break;        case : //對u 數u 庫u 段TRING//ok         row[i ] =          funcCL_String(rsgetString(cols[i ]));

  break;        case : //對u 數u 庫u 段ATE//ok         date = (javasqlDate) rsgetDate(cols[i ]);         row[i ] = date;         break;        case :         javasqlTime time =          (javasqlTime) rsgetTime(cols[i ]);         row[i ] = time;         break;        case :         javasqlTimestamp timestamp =          (javasqlTimestamp) rsgetTimestamp(           cols[i ]);         row[i ] = timestamp;         break;

  default :

  row[i ] =          funcCL_String(rsgetString(cols[i ]));

  break;

  }      }     }     kfsesetCols(cols);     kfsesetColstype(colsType);     kfsesetRows(rows);    }    utilclose();   } catch (SQLException e) {    eprintStackTrace();   }   return kfse;  } //頁面讀取 javautilList rows;  String str = ;  int ii =;  Iterator iter = null;    int j = ;    try {     int i;     ExecMySql bean = new ExecMySql();     String tmps;     j = ;     str =      select DJCJD_MCDJCJD_KSRQ from EM_DJCJD_TB where DJCJD_ID=;

  MyTable mytb = beanexecMySQLSelect(str);     j=;     rows = new javautilArrayList();     rows = mytbgetRows();     ii =rowssize();     if (rowsisEmpty()) {      return;     }     String mys;                          j=;     for (iter = erator(); iterhasNext();) {      Object[] sss = (Object[]) iternext();

  j = ;      wdContextcurrentContextElement()setString((String) sss[]);      wdContextcurrentContextElement()setDate((Date) sss[]);           }

  } catch (Exception e) {     MessageManager wdMsgMgr =(MessageManager) wdThiswdGetAPI()getComponent()getMessageManager();     wdMsgMgrreportException(egetMessage() true);     loggererrorT(egetMessage());    }

  //插入日期 insert into em_djcqk_tb t (tDJCQK_IDtDJCQK_JCRQ) values(to_date( ::yyyymmdd hh:mi:ss)) //插入帶 的字符串 String a = aaaaa ; String b = areplaceAll() insert into em_djcqk_tb (DJCQK_IDDJCQK_JCR) values (b)


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26555.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.