ntDB.java
--------------------------------------------------------------------------
package cint;
/* 存取数据库2002 注意:和dba.ntDB不能互换取代 ,尤其注意 */
/* 方法介绍: public static String sql_TRANS_ERROR public int rowCount ; public int colCount ; public String[] fieldName ; public int[] fieldType ; public int[] columnDisplaySize ; public String[][] data = null ;
public boolean isSaveField( String s ) public String noPrefixField( String s ) file://如果增加,会返回增加后的id号 public String toDB( Hashtable requestHt ) throws Exception public Hashtable copyRequestHt( Hashtable requestHt ) throws Exception public void createTable( String dbName , String globalTableName ) throws Exception public int executeQuery( String _connName, String SQL ) throws SQLException,Exception public int executeUpdate( String _connName, String SQL ) throws Exception public String data(int i, String _fieldName){ public String data( int i , String _fieldName , int _begin , int _end ) 从数据库中取值 public String getKV( String dbName , String tableName,int _pid , String _k ) throws Exception public String getKV( String dbName ,String tableName ,int _pid ,String _k ,int _begin // ,int _end // ) throws Exception public String getKV( String dbName, String tableName,String _pid , String _k ) throws Exception public String getKV( String dbName ,String tableName ,String _pid ,String _k ,int _begin // ,int _end // ) throws Exception 从data中取得值 public String getKV( int _pid , String _k ) throws Exception public String getKV( int _pid file://1 ,String _k file://2 ,int _begin file://3 ,int _end file://4 ) throws Exception public String getPublish( int _pid , String _k ) throws Exception{ public static boolean isNumeric( String s ){ public static boolean isInt( String s ){ public static boolean isDate( String s ){ public static boolean isDateTime( String s ){ public static String ntSubString( String _s , int _begin , int _end ){ public static String nt8859( String s ) throws Exception{ public static String ntFormatHour( String s ){ public static int ntGetMonthDayCount( int year ,int month ){ public static String ntEncodeHtml( String s ){ public static String ntEncodeSimple( String s ){ public static String ntEncodeDB( String s ){ public static String ntRemoveChar( String s, char cDel ){ public static int ntStringIndexOf( String[] ss , String s ) public static String toPinYin(String str, String flag) throws Exception { public static String toPinYin_IntToPY(Integer n){ public static void fileCopy(String from, String to, boolean overwrite) throws Exception{
*/
/* 调用范例 * nttest * <%@page import="java.util.*"%> * <%@page import="java.text.*"%> * <%@page import="dba.ntDB"%> * <% * ntDB nt = new ntDB(); * nt.executeQuery("yw","select * from msgboard_wjm"); * * out.println( (new SimpleDateFormat( * "yyyy-MM-dd HH:mm:ss.SSS")).format(new Date()) ); * file://out.println( (new Date()).toString() ); * * out.println("<br>"); * out.println( nt.rowCount ); * out.println("<table border=1 >"); * for(int i=0;i< nt.rowCount;i++ ){ * * * if(i==0){ * out.println("<tr>"); * for(int k=0;k< nt.colCount;k++ ){ * out.println("<td>"); * out.print( nt.fieldName[k] ); * out.print("</td>"); * } * out.println("</tr>"); * } * * out.println("<tr>"); * for(int j=0;j< nt.colCount;j++ ){ * out.println("<td>"); * out.println( nt.data[i][j] ); * out.println("</td>"); * } * out.println("</tr>"); * } * out.println("</table>"); * %> */
import java.util.*; import java.lang.*; import java.text.*; import java.sql.*; import java.sql.Connection; import javax.sql.*; import javax.naming.*; import javax.transaction.*; import java.io.*; import java.net.*;
public class ntDB { private Connection con; private String connName; private Statement statement ; private ResultSet rs = null ; private ResultSetMetaData rsmd;
public static String sql_TRANS_ERROR = " if @@error<>0 set @ntE=@ntE+1 \n";
file://额外方法, 汉字转化为拼音 private static Hashtable ht_PinYin=new Hashtable(10);
public int rowCount ; public int colCount ; public String[] fieldName ; public int[] fieldType ; public int[] columnDisplaySize ; public String[][] data = null ; ////额外方法 public ntDB() { rowCount=-1; colCount=-1; }
private void getConn(){ try{ Context env=(Context)new InitialContext().lookup("java:comp/env"); DataSource ds = (DataSource) env.lookup("jdbc/"+connName); con = ds.getConnection(); }catch(Exception e){ e.printStackTrace(); con = null; } }
private void closeConn() throws Exception { if( con != null ) con.close(); }
// GBDP技术 begin -------------------------------------- public boolean isSaveField( String s ) { if( s.equals("edit") || s.equals("add") || s.equals("delete") || s.equals("id") || s.equals("globalTableName") || s.equals("parentKey") || s.equals("parentValue") || s.equals("sql_trans") || s.equals("dbName") || s.toLowerCase().indexOf("submit") == 0 || s.toLowerCase().indexOf("nouse") == 0 || s.toLowerCase().indexOf("useless") == 0 || s.toLowerCase().indexOf("imagefield") == 0 ) return false; else return true; }
public String noPrefixField( String s ) { if( s.toLowerCase().indexOf("numeric") == 0 ) return s.substring(7,s.length()); else if( s.toLowerCase().indexOf("text") == 0 ) return s.substring(4,s.length()); else if( s.toLowerCase().indexOf("datetime") == 0 ) return s.substring(8,s.length()); else if( s.toLowerCase().indexOf("date") == 0 && s.toLowerCase().indexOf("datetime") < 0) return s.substring(4,s.length()); else if( s.toLowerCase().indexOf("int") == 0 ) return s.substring(3,s.length()); return s; } public String toDB( Hashtable requestHt ) throws Exception { String result = ""; String sql = ""; String globalTableName = null ; int id = 0 ; String sql_trans = null ; String dbName = null ; int masterId = 0; String parentKey = null ; String parentValue = null ;
// begin 检查 if( requestHt.get("id")==null && requestHt.get("edit")!=null ) throw new Exception("GBDP:编辑没有提供id号! ");
if( requestHt.get("id")==null && requestHt.get("delete")!=null ) throw new Exception("GBDP:删除没有提供id号! ");
globalTableName = (String)requestHt.get("globalTableName"); if( globalTableName == null || globalTableName.equals("") ) throw new Exception("GBDP:没有提供globalTableName! ");
if( requestHt.get("edit")!=null || requestHt.get("delete")!=null ) { try{ id = Integer.parseInt((String)requestHt.get("id")); }catch(Exception e){ throw new Exception("GBDP:没有提供正确的id号! "+ (String)requestHt.get("id")); } }
sql_trans = (String)requestHt.get("sql_trans"); // 因为不是必须,所以不用检查,用到时再检查 dbName = (String)requestHt.get("dbName"); if( dbName == null || dbName.equals("") ) throw new Exception("GBDP:没有提供dbName! "); file://主从表结构的主表id try{ masterId = Integer.parseInt((String)requestHt.get("masterId")); }catch(Exception e){ masterId = 0 ; } if( requestHt.get("add")!=null && requestHt.get("parentKey")==null ) throw new Exception("GBDP:增加没有提供parentKey! "); parentKey = (String)requestHt.get("parentKey"); if( requestHt.get("add")!=null && requestHt.get("parentValue")==null ) throw new Exception("GBDP:增加没有提供parentValue! "); parentValue = (String)requestHt.get("parentValue"); // end 检查 createTable( dbName , globalTableName ); file://修改 if( requestHt.get("edit")!=null && requestHt.get("delete")==null ) // isEdit && hasId { file://检查是否存在 executeQuery(dbName, "select * from "+ globalTableName +"_main "+ " where id = "+id ); if( rowCount <= 0 ) throw new Exception( "修改"+ globalTableName + "出错:数据库没有找到此id号'"+ id +"'"); // sql = ""+ " declare @i int \n"+ " declare @ntE int \n"+ " select @ntE=0 \n"+ " begin tran \n"+ " "; file://加入额外需要在事务内执行的语句,共有3处加入, add, edit , delete file://String sql_TRANS_ERROR = " if @@error<>0 set @ntE=@ntE+1 \n"; file://String sql_trans = "" ; file://用来插入后面的保存到数据库的sql语句中,使其中在事务之中间 if( sql_trans != null && !sql_trans.equals("") ) sql = sql + sql_trans + sql_TRANS_ERROR; Enumeration er37 = requestHt.keys(); while (er37.hasMoreElements()) { String s = ((String)er37.nextElement()).trim(); String sValue = ((String)requestHt.get(s)).trim(); if( sValue == null ) sValue = ""; sValue = ntEncodeDB( sValue ); String sTableName = globalTableName ; if( s.indexOf("numeric") == 0 || s.indexOf("n_") == 0 ) { sTableName = globalTableName + "_numeric" ; } else if( s.indexOf("int") == 0 || s.indexOf("i_") == 0 ) { sTableName = globalTableName + "_int" ; } else if( s.indexOf("date") == 0 || s.indexOf("d_") == 0 ) { sTableName = globalTableName + "_datetime"; sValue = "'"+sValue+"'"; } else if( s.indexOf("text") == 0 || s.indexOf("t_") == 0) { sTableName = globalTableName + "_text" ; sValue = "'"+sValue+"'"; } else sValue = "'"+sValue+"'"; file://必须放在这里,因为只有修改和增加数值型字段才满足sValue.equals("") if( sValue.equals("") ) continue; if( isSaveField(s) ) { sql = sql + " if exists ( select * from "+ sTableName +" where pid= "+ id +" and k='"+ noPrefixField(s) +"'" +" ) \n"+ " begin \n"+ " update "+ sTableName +" set v = "+ sValue +" \n"+ " where pid= "+ id +" and k='"+ noPrefixField(s) +"' \n" + " if @@error<>0 set @ntE=@ntE+1 \n"+ " end \n"+ " else \n"+ " begin \n"+ " select @i=@i "+ ""; if( !sValue.equals("") && !sValue.equals("''") ) sql = sql + " select @i = ( select isNull(min(id),0)-1 from "+ sTableName +" ) \n"+ " if @i >= 0 select @i = -1 \n"+ " insert into "+ sTableName +" (id,pid,k,v) \n"+ " values( @i, "+ id + " \n"+ " , '"+ noPrefixField(s) +"', "+ sValue +") \n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ ""; sql = sql + " "+ " end \n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " "; } }//end while sql = sql + " if @ntE = 0 \n"+ " commit tran \n"+ " else \n"+ " begin \n"+ " rollback tran \n"+ " raiserror ('数据库执行出错! ',16,1) \n"+ " end \n"+ " "; executeUpdate( dbName,sql ); } // end if edit
file://添加保存入数据库 if( requestHt.get("add")!=null && requestHt.get("edit")==null && requestHt.get("delete")==null ) { // 隔离局部变量 file://int rowNo = -9999; file://int formNo = -9999; file://nt.executeQuery(dbName,"select isNull(max(id),0)+1 from "+ globalTableName +""); file://formNo = Integer.parseInt( nt.data[0][0] ); file://nt.executeQuery(dbName,"select isNull(min(id),0)-1 from "+ globalTableName +""); file://rowNo = Integer.parseInt( nt.data[0][0] ); sql = ""+ " declare @ntE int \n"+ " declare @i int \n"+ " declare @formNo int \n"+ " select @ntE=0 \n"+ " begin tran \n"+ ""; file://加入额外需要在事务内执行的语句,共有3处加入, add, edit , delete file://String sql_TRANS_ERROR = " if @@error<>0 set @ntE=@ntE+1 \n"; file://String sql_trans = "" ; file://用来插入后面的保存到数据库的sql语句中,使其中在事务之中间 if( sql_trans != null && !sql_trans.equals("") ) sql = sql + sql_trans + sql_TRANS_ERROR; sql = sql + " select @i = ( select isNull(max(id),0)+1 from "+ globalTableName +"_main ) \n"+ " select @formNo = @i \n"+ " insert into "+ globalTableName +"_main (id,pid,k,v) \n"+ " values( @i, "+ masterId +", '"+ parentKey +"', '"+ parentValue +"') \n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " "; Enumeration er44 = requestHt.keys(); while (er44.hasMoreElements()) { String s = ((String)er44.nextElement()).trim(); String sValue = ((String)requestHt.get(s)); if( sValue == null ) sValue = ""; sValue = sValue.trim(); if( sValue.equals("") ) continue; sValue = ntEncodeDB( sValue ); String sTableName = globalTableName ; file://判断插入哪个类型(text,numeric,datetime...)的表 if( s.indexOf("numeric") == 0 || s.indexOf("n_") == 0 ) { sTableName = globalTableName + "_numeric" ; } else if( s.indexOf("int") == 0 || s.indexOf("i_") == 0 ) { sTableName = globalTableName + "_int" ; } else if( s.indexOf("date") == 0 || s.indexOf("d_") == 0 ) { sTableName = globalTableName + "_datetime"; sValue = "'"+sValue+"'"; } else if( s.indexOf("text") == 0 || s.indexOf("t_") == 0 ) { sTableName = globalTableName + "_text" ; sValue = "'"+sValue+"'"; } else sValue = "'"+sValue+"'"; if( isSaveField(s) ) { file://注意:保存到数据库的时候将"numeric","date"等前缀去除了 sql = sql + " select @i = ( select isNull(min(id),0)-1 from "+ sTableName +" ) \n"+ " if @i >= 0 select @i = -1 \n"+ " insert into "+ sTableName +" (id,pid,k,v) "+ " values( @i, @formNo , '"+ noPrefixField(s) +"', "+ sValue +") "+ " if @@error<>0 set @ntE=@ntE+1 "+ " "; } }//end while sql = sql + " if @ntE = 0 "+ " commit tran "+ " else "+ " begin "+ " rollback tran "+ " raiserror ('数据库执行出错!',16,1) "+ " end "+ " "; executeQuery( dbName,sql + " select @formNo as id " ); if( rowCount > 0 ) result = data[0][0] ; } // 增加
// 将pid 为 id 的删除 if( requestHt.get("delete")!=null ) file://因为一般都是edit和delete同时存在 { file://检查pid是否存在 executeQuery(dbName, "select * from "+ globalTableName +"_main "+ " where id = "+id ); if( rowCount <= 0 ) throw new Exception("删除"+ globalTableName + "出错:没有找到此id号'"+ id +"'"); sql = ""+ " declare @ntE int \n"+ " declare @i int \n"+ " select @ntE=0 \n"+ " begin tran \n"+ ""; file://加入额外需要在事务内执行的语句,共有3处加入, add, edit , delete file://String sql_TRANS_ERROR = " if @@error<>0 set @ntE=@ntE+1 \n"; file://String sql_trans = "" ; file://用来插入后面的保存到数据库的sql语句中,使其中在事务之中间 if( sql_trans != null && !sql_trans.equals("") ) sql = sql + sql_trans + sql_TRANS_ERROR; sql = sql + " delete from "+ globalTableName +" where pid = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " delete from "+ globalTableName +"_numeric where pid = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " delete from "+ globalTableName +"_int where pid = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " delete from "+ globalTableName +"_datetime where pid = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " delete from "+ globalTableName +"_text where pid = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " delete from "+ globalTableName +"_main where id = "+ id + "\n"+ " if @@error<>0 set @ntE=@ntE+1 \n"+ " if @ntE = 0 \n"+ " commit tran \n"+ " else \n"+ " begin \n"+ " rollback tran \n"+ " raiserror ('数据库执行出错! ',16,1) \n"+ " end \n"+ ""; executeUpdate(dbName, sql ); } // end 删除 return result; } // end toDB public Hashtable copyRequestHt( Hashtable requestHt ) throws Exception { if( requestHt == null ) return null; Hashtable ht = new Hashtable(); // Enumeration e = requestHt.keys(); while (e.hasMoreElements()) { String s = ((String)e.nextElement()); String sValue = ((String)requestHt.get(s)); ht.put( s , sValue ); } // end while // return ht; } public void createTable( String dbName , String globalTableName ) throws Exception { String sql = "";
file://检查表是否存在,如果没有,建6表, // t_main , t , t_numeric , t_datetime , t_text , t_int for(int i=0;i<6;i++){ String sTableName = globalTableName ; file://default String sType = "varchar(255)"; file://default if( i==1 ) { sTableName = globalTableName + "_numeric" ; sType = "numeric(38,8)" ;} else if( i==2 ) { sTableName = globalTableName + "_datetime"; sType = "datetime"; } else if( i==3 ) { sTableName = globalTableName + "_text" ; sType = "text"; } else if( i==4 ) { sTableName = globalTableName + "_main" ; } else if( i==5 ) { sTableName = globalTableName + "_int" ; sType = "int"; } sql = sql + " if not exists( select name from sysobjects \n"+ " where name ='"+ sTableName +"' and type='U' ) \n"+ " begin \n"+ " create table "+ sTableName +"( \n"+ " id int primary key \n"+ " , pid int not null \n"+ " , v "+ sType +" not null \n"+ " , k varchar(255) not null \n"+ " ) \n"+ " end \n"+ ""+ " if not exists( select name from sysindexes \n"+ " where name like '%idx_"+ sTableName +"_pid%' ) \n"+ " begin \n"+ " create index idx_"+sTableName+"_pid on "+sTableName+"(pid) \n"+ " end \n"+ ""+ " if not exists( select name from sysindexes \n"+ " where name like '%idx_"+ sTableName +"_k%' ) \n"+ " begin \n"+ " create index idx_"+sTableName+"_k on "+sTableName+"(k) \n"+ " end \n"+ ""; file://因为text子段不能建立索引 if( !sType.equals("text") ){ sql = sql + " if not exists( select name from sysindexes \n"+ " where name like '%idx_"+ sTableName +"_v%' ) \n"+ " begin \n"+ " create index idx_"+sTableName+"_v on "+sTableName+"(v) \n"+ " end \n"+ ""; } sql = sql + ""; } // end for create table executeUpdate(dbName,sql); } // end createTable() // GBDP技术 end -------------------------------------------
public int executeQuery( String _connName, String SQL ) throws SQLException,Exception { file://Hashtable ht = new Hashtable(); try{ connName = _connName; getConn();
statement = con.createStatement(); String sql = SQL ; file://sql = new String(sql.getBytes("GBK"),"8859_1"); try{ rs = statement.executeQuery( sql ); }catch(Exception e){ throw new Exception( e.getMessage() + "\n" + sql ); }
if( rs == null ) return -1 ; // getMetaData rsmd = rs.getMetaData(); file://ResultSetMetaData colCount = -1; colCount = rsmd.getColumnCount();
fieldName = new String[colCount]; file://String[] fieldType = new int[colCount]; file://int[] columnDisplaySize = new int[colCount]; file://int[]
for(int i=0;i<colCount;i++) { fieldName[i] = rsmd.getColumnName(i+1) ; columnDisplaySize[i] = rsmd.getColumnDisplaySize(i+1) ; fieldType[i] = rsmd.getColumnType(i+1) ; }
// if( colCount <= 0 ) return -1 ;
Vector v = new Vector(); while( rs.next() ) { String[] sRow = new String[colCount]; for(int i=0;i<colCount;i++){ sRow[i] = rs.getString(i+1); /* if( fieldType[i] == java.sql.Types.DATE || fieldType[i] == java.sql.Types.TIME || fieldType[i] == java.sql.Types.TIMESTAMP ) { sRow[i] = (new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS")).format(rs.getTimestamp(i+1)); } */ } v.add( sRow ); file://(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")).format(new Date()) // java.sql.Types.DATE }
rowCount = -1; file://int rowCount = v.size(); file://int data = new String[rowCount][colCount]; for(int i=0;i<rowCount;i++) { for(int j=0;j<colCount;j++){ data[i][j] = ((String[])v.elementAt(i))[j]; if( data[i][j] == null ) data[i][j] = ""; data[i][j] = data[i][j].trim(); } }
rs.close(); statement.close();
/* if( colCount >= 0 ){ ht.put("fieldCount",""+colCount); ht.put("colCount",""+colCount); } if( fieldName != null ) ht.put("fieldName",fieldName); if( fieldType != null ) ht.put("fieldType",fieldType); if( data != null ) ht.put("data",data); file://return data; ht.put("rowCount",""+rowCount); */ }finally{ closeConn(); } return rowCount; } // end executeQuery()
file://返回受影响的记录数量 public int executeUpdate( String _connName, String SQL ) throws Exception { String sql = SQL ; file://sql = new String(sql.getBytes("GBK"),"8859_1"); int result ;
connName = _connName; getConn();
statement = con.createStatement(); file://Statement
file://process try { result = statement.executeUpdate( sql ); } catch (Exception e33) { throw new Exception( "newtower:ntDB:executeUpdate:"+ e33.getMessage() + "\n" + sql ); } finally{ closeConn(); } return result; }
public String data(int i, String _fieldName){ if( i > rowCount-1 ) return ""; if( ntStringIndexOf(fieldName,_fieldName) < 0 ) return ""; return data[i][ntStringIndexOf(fieldName,_fieldName)]; }
public String data( int i , String _fieldName , int _begin , int _end ) { String s = data(i,_fieldName); s = ntSubString( s , _begin , _end ); return s; }
public String getKV( String dbName, String tableName,String _pid , String _k ) throws Exception { if( _pid == null || _pid.equals("") ) return ""; return getKV( dbName ,tableName , Integer.parseInt(_pid),_k ); }
public String getKV( String dbName, String tableName,int _pid , String _k ) throws Exception { String tableNameSuffix = ""; /* 因为text,int等前缀不再保存到数据库,所以不必以下的代码了 if( _k.indexOf("numeric")==0 && tableName.indexOf("_numeric")<0 ) tableNameSuffix = "_numeric"; else if( _k.indexOf("int")==0 && tableName.indexOf("_int")<0 ) tableNameSuffix = "_int"; else if( _k.indexOf("text")==0 && tableName.indexOf("_text")<0 ) tableNameSuffix = "_text"; else if( _k.indexOf("date")==0 && tableName.indexOf("_datetime")<0 ) tableNameSuffix = "_datetime"; */
this.executeQuery(dbName, " select v from "+ tableName + tableNameSuffix + " where pid = " + _pid + " and k ='"+ (_k) + "'" + ""); if( this.rowCount > 1 ) throw new Exception( tableName + tableNameSuffix + "发现多于一个值 pid=["+ _pid +"] k=[" + _k +"]" ); if( this.rowCount ==1 ) return data[0][0]; else return ""; }
public String getKV( String dbName ,String tableName ,String _pid ,String _k ,int _begin // ,int _end // ) throws Exception { if( _pid == null || _pid.equals("") ) return ""; return getKV( dbName, tableName, Integer.parseInt(_pid), _k, _begin, _end); }
public String getKV( String dbName ,String tableName ,int _pid ,String _k ,int _begin // ,int _end // ) throws Exception { file://begin String s = getKV( dbName ,tableName, _pid , _k ); s = ntSubString( s , _begin , _end ); return s; }
public String getKV( int _pid , String _k ) throws Exception { String s = ""; for(int i=0;i<rowCount;i++) { if( data(i,"pid").equals(""+_pid) && data(i,"k").equals(""+_k) ) { s = data(i,"v") ; break; } } return s; }
public String getKV( int _pid file://1 ,String _k file://2 ,int _begin file://3 ,int _end file://4 ) throws Exception { file://begin String s = getKV( _pid , _k ); s = ntSubString( s , _begin , _end ); return s; }
public String getPublish( int _pid , String _k ) throws Exception{ return getKV( "eweb","publish" , _pid, _k ); } ////////////////////////////////////////////////////////// public static boolean isNumeric( String s ){ try{ Double.parseDouble( s ); }catch(Exception e){ return false; } return true; }
public static boolean isInt( String s ){ try{ Integer.parseInt( s ); }catch(Exception e){ return false; } return true; }
public static boolean isDate( String s ){ java.util.Date d; try{ d = DateFormat.getDateInstance().parse( s ); }catch(Exception e){ return false; } return true; }
public static boolean isDateTime( String s ){ return isDate(s); } public static String ntSubString( String _s , int _begin , int _end ){ String r = _s ; try{ r = _s.substring(_begin,_end); }catch(Exception e238SubString){ r = _s ; } return r; }
public static String nt8859( String s ) throws Exception{ return new String(s.getBytes("GBK"),"8859_1"); }
public static String ntFormatHour( String s ){ if( s == null ) return null; String r = s; if( s.trim().length() == 1 ) r = "0" + r; return r; }
public static int ntGetMonthDayCount( int year ,int month ){ int r = 31 ; if( month == 4 || month == 6 || month == 9 || month == 11 ) r = 30 ; if( month == 2 ){ r = 28 ; if( (year%4)==0 && (year%400)==0 ) r = 29 ; if( (year%4)==0 && (year%100)!=0 ) r = 29 ; } return r; }
public static String ntEncodeHtml( String s ){ if( s == null ) return null; String r = ""; for( int i=0;i<s.length() ;i++ ){ char c = s.charAt(i); if( c == '&' ) r = r + "&" ; else if( c == '\"' ) r = r + """; else if( c == '<' ) r = r + "<"; else if( c == '>' ) r = r + ">"; else if( c == '\n' ) r = r + "<BR>"; else if( c == ' ' ) r = r + " "; else r = r + c; }//end for return r; } // end ntEncodeHtml
file://用来将普通字符串转化到""之间, 例如 asdf"\ 到 "asdf\"\\" public static String ntEncodeSimple( String s ){ if( s == null ) return null; String r = ""; for( int i=0;i<s.length() ;i++ ){ char c = s.charAt(i); if( c == '\n' ) r = r + "\\n" ; else if( c == '\\' ) r = r + "\\\\"; else if( c == (char)(13) ) r = r ; else if( c == (char)(10) ) r = r ; else if( c == '\"' ) r = r + "\\\""; else r = r + c; } return r; } // end ntEncodeSimple
public static String ntEncodeDB( String s ){ if( s == null ) return null; String r = ""; for( int i=0;i<s.length() ;i++ ){ char c = s.charAt(i); if( c == '\'' ) r = r + "''" ; else r = r + c; } return r; } // end ntEncodeDB
public static String ntRemoveChar( String s, char cDel ){ if( s == null ) return null; String r = ""; for( int i=0;i<s.length() ;i++ ){ char c = s.charAt(i); if( c == cDel ) ; else r = r + c; } return r; } // end ntRemoveChar
public static int ntStringIndexOf( String[] ss , String s ) { int pos = -1 ; for(int i=0;i<ss.length;i++) { if( ss[i].equalsIgnoreCase(s) ) { pos = i; break; } } return pos; } public static String toPinYin(String str, String flag) throws Exception { char[] hz= nt8859(str).toCharArray(); int len=nt8859(str).length(); int p,q; String ret=""; for(int i=0;i<len;i++){ p=(int)hz[i]; if(p>160){ q=(int)hz[++i]; p=p*256+q-65536; } if( flag.length()>0 ) if( flag.indexOf("C")==0 ){ String s = toPinYin_IntToPY(new Integer(p)); s = s.substring(0,1).toUpperCase() + s.substring(1,s.length()); ret+= s + flag.substring(1,flag.length()) ; } else ret+= toPinYin_IntToPY(new Integer(p)) + flag ; else ret+= toPinYin_IntToPY(new Integer(p)) ; } file://for if( flag.indexOf("C")==0 ) return ret.substring(0,ret.length()-flag.length()+1); else return ret.substring(0,ret.length()-flag.length()); }
public static String toPinYin_IntToPY(Integer n){ int num=n.intValue(); if(num>0&&num<160){ return String.valueOf((char)num); } else if(num<-20319||num>-10247){ return ""; } else{ if(ht_PinYin.size()==0){toPinYin_ht();} while(!ht_PinYin.containsKey(Integer.toString(num)))num--; return ht_PinYin.get(Integer.toString(num)).toString(); } }
private static void toPinYin_ht(){ ht_PinYin.put("-20319","a"); ht_PinYin.put("-20317","ai"); ht_PinYin.put("-20304","an"); ht_PinYin.put("-20295","ang"); ht_PinYin.put("-20292","ao"); ht_PinYin.put("-20283","ba"); ht_PinYin.put("-20265","bai"); ht_PinYin.put("-20257","ban"); ht_PinYin.put("-20242","bang"); ht_PinYin.put("-20230","bao"); ht_PinYin.put("-20051","bei"); ht_PinYin.put("-20036","ben"); ht_PinYin.put("-20032","beng"); ht_PinYin.put("-20026","bi"); ht_PinYin.put("-20002","bian"); ht_PinYin.put("-19990","biao"); ht_PinYin.put("-19986","bie"); ht_PinYin.put("-19982","bin"); ht_PinYin.put("-19976","bing"); ht_PinYin.put("-19805","bo"); ht_PinYin.put("-19784","bu"); ht_PinYin.put("-19775","ca"); ht_PinYin.put("-19774","cai"); ht_PinYin.put("-19763","can"); ht_PinYin.put("-19756","cang"); ht_PinYin.put("-19751","cao"); ht_PinYin.put("-19746","ce"); ht_PinYin.put("-19741","ceng"); ht_PinYin.put("-19739","cha"); ht_PinYin.put("-19728","chai"); ht_PinYin.put("-19725","chan"); ht_PinYin.put("-19715","chang"); ht_PinYin.put("-19540","chao"); ht_PinYin.put("-19531","che"); ht_PinYin.put("-19525","chen"); ht_PinYin.put("-19515","cheng"); ht_PinYin.put("-19500","chi"); ht_PinYin.put("-19484","chong"); ht_PinYin.put("-19479","chou"); ht_PinYin.put("-19467","chu"); ht_PinYin.put("-19289","chuai"); ht_PinYin.put("-19288","chuan"); ht_PinYin.put("-19281","chuang"); ht_PinYin.put("-19275","chui"); ht_PinYin.put("-19270","chun"); ht_PinYin.put("-19263","chuo"); ht_PinYin.put("-19261","ci"); ht_PinYin.put("-19249","cong"); ht_PinYin.put("-19243","cou"); ht_PinYin.put("-19242","cu"); ht_PinYin.put("-19238","cuan"); ht_PinYin.put("-19235","cui"); ht_PinYin.put("-19227","cun"); ht_PinYin.put("-19224","cuo"); ht_PinYin.put("-19218","da"); ht_PinYin.put("-19212","dai"); ht_PinYin.put("-19038","dan"); ht_PinYin.put("-19023","dang"); ht_PinYin.put("-19018","dao"); ht_PinYin.put("-19006","de"); ht_PinYin.put("-19003","deng"); ht_PinYin.put("-18996","di"); ht_PinYin.put("-18977","dian"); ht_PinYin.put("-18961","diao"); ht_PinYin.put("-18952","die"); ht_PinYin.put("-18783","ding"); ht_PinYin.put("-18774","diu"); ht_PinYin.put("-18773","dong"); ht_PinYin.put("-18763","dou"); ht_PinYin.put("-18756","du"); ht_PinYin.put("-18741","duan"); ht_PinYin.put("-18735","dui"); ht_PinYin.put("-18731","dun"); ht_PinYin.put("-18722","duo"); ht_PinYin.put("-18710","e"); ht_PinYin.put("-18697","en"); ht_PinYin.put("-18696","er"); ht_PinYin.put("-18526","fa"); ht_PinYin.put("-18518","fan"); ht_PinYin.put("-18501","fang"); ht_PinYin.put("-18490","fei"); ht_PinYin.put("-18478","fen"); ht_PinYin.put("-18463","feng"); ht_PinYin.put("-18448","fo"); ht_PinYin.put("-18447","fou"); ht_PinYin.put("-18446","fu"); ht_PinYin.put("-18239","ga"); ht_PinYin.put("-18237","gai"); ht_PinYin.put("-18231","gan"); ht_PinYin.put("-18220","gang"); ht_PinYin.put("-18211","gao"); ht_PinYin.put("-18201","ge"); ht_PinYin.put("-18184","gei"); ht_PinYin.put("-18183","gen"); ht_PinYin.put("-18181","geng"); ht_PinYin.put("-18012","gong"); ht_PinYin.put("-17997","gou"); ht_PinYin.put("-17988","gu"); ht_PinYin.put("-17970","gua"); ht_PinYin.put("-17964","guai"); ht_PinYin.put("-17961","guan"); ht_PinYin.put("-17950","guang"); ht_PinYin.put("-17947","gui"); ht_PinYin.put("-17931","gun"); ht_PinYin.put("-17928","guo"); ht_PinYin.put("-17922","ha"); ht_PinYin.put("-17759","hai"); ht_PinYin.put("-17752","han"); ht_PinYin.put("-17733","hang"); ht_PinYin.put("-17730","hao"); ht_PinYin.put("-17721","he"); ht_PinYin.put("-17703","hei"); ht_PinYin.put("-17701","hen"); ht_PinYin.put("-17697","heng"); ht_PinYin.put("-17692","hong"); ht_PinYin.put("-17683","hou"); ht_PinYin.put("-17676","hu"); ht_PinYin.put("-17496","hua"); ht_PinYin.put("-17487","huai"); ht_PinYin.put("-17482","huan"); ht_PinYin.put("-17468","huang"); ht_PinYin.put("-17454","hui"); ht_PinYin.put("-17433","hun"); ht_PinYin.put("-17427","huo"); ht_PinYin.put("-17417","ji"); ht_PinYin.put("-17202","jia"); ht_PinYin.put("-17185","jian"); ht_PinYin.put("-16983","jiang"); ht_PinYin.put("-16970","jiao"); ht_PinYin.put("-16942","jie"); ht_PinYin.put("-16915","jin"); ht_PinYin.put("-16733","jing"); ht_PinYin.put("-16708","jiong"); ht_PinYin.put("-16706","jiu"); ht_PinYin.put("-16689","ju"); ht_PinYin.put("-16664","juan"); ht_PinYin.put("-16657","jue"); ht_PinYin.put("-16647","jun"); ht_PinYin.put("-16474","ka"); ht_PinYin.put("-16470","kai"); ht_PinYin.put("-16465","kan"); ht_PinYin.put("-16459","kang"); ht_PinYin.put("-16452","kao"); ht_PinYin.put("-16448","ke"); ht_PinYin.put("-16433","ken"); ht_PinYin.put("-16429","keng"); ht_PinYin.put("-16427","kong"); ht_PinYin.put("-16423","kou"); ht_PinYin.put("-16419","ku"); ht_PinYin.put("-16412","kua"); ht_PinYin.put("-16407","kuai"); ht_PinYin.put("-16403","kuan"); ht_PinYin.put("-16401","kuang"); ht_PinYin.put("-16393","kui"); ht_PinYin.put("-16220","kun"); ht_PinYin.put("-16216","kuo"); ht_PinYin.put("-16212","la"); ht_PinYin.put("-16205","lai"); ht_PinYin.put("-16202","lan"); ht_PinYin.put("-16187","lang"); ht_PinYin.put("-16180","lao"); ht_PinYin.put("-16171","le"); ht_PinYin.put("-16169","lei"); ht_PinYin.put("-16158","leng"); ht_PinYin.put("-16155","li"); ht_PinYin.put("-15959","lia"); ht_PinYin.put("-15958","lian"); ht_PinYin.put("-15944","liang"); ht_PinYin.put("-15933","liao"); ht_PinYin.put("-15920","lie"); ht_PinYin.put("-15915","lin"); ht_PinYin.put("-15903","ling"); ht_PinYin.put("-15889","liu"); ht_PinYin.put("-15878","long"); ht_PinYin.put("-15707","lou"); ht_PinYin.put("-15701","lu"); ht_PinYin.put("-15681","lv"); ht_PinYin.put("-15667","luan"); ht_PinYin.put("-15661","lue"); ht_PinYin.put("-15659","lun"); ht_PinYin.put("-15652","luo"); ht_PinYin.put("-15640","ma"); ht_PinYin.put("-15631","mai"); ht_PinYin.put("-15625","man"); ht_PinYin.put("-15454","mang"); ht_PinYin.put("-15448","mao"); ht_PinYin.put("-15436","me"); ht_PinYin.put("-15435","mei"); ht_PinYin.put("-15419","men"); ht_PinYin.put("-15416","meng"); ht_PinYin.put("-15408","mi"); ht_PinYin.put("-15394","mian"); ht_PinYin.put("-15385","miao"); ht_PinYin.put("-15377","mie"); ht_PinYin.put("-15375","min"); ht_PinYin.put("-15369","ming"); ht_PinYin.put("-15363","miu"); ht_PinYin.put("-15362","mo"); ht_PinYin.put("-15183","mou"); ht_PinYin.put("-15180","mu"); ht_PinYin.put("-15165","na"); ht_PinYin.put("-15158","nai"); ht_PinYin.put("-15153","nan"); ht_PinYin.put("-15150","nang"); ht_PinYin.put("-15149","nao"); ht_PinYin.put("-15144","ne"); ht_PinYin.put("-15143","nei"); ht_PinYin.put("-15141","nen"); ht_PinYin.put("-15140","neng"); ht_PinYin.put("-15139","ni"); ht_PinYin.put("-15128","nian"); ht_PinYin.put("-15121","niang"); ht_PinYin.put("-15119","niao"); ht_PinYin.put("-15117","nie"); ht_PinYin.put("-15110","nin"); ht_PinYin.put("-15109","ning"); ht_PinYin.put("-14941","niu"); ht_PinYin.put("-14937","nong"); ht_PinYin.put("-14933","nu"); ht_PinYin.put("-14930","nv"); ht_PinYin.put("-14929","nuan"); ht_PinYin.put("-14928","nue"); ht_PinYin.put("-14926","nuo"); ht_PinYin.put("-14922","o"); ht_PinYin.put("-14921","ou"); ht_PinYin.put("-14914","pa"); ht_PinYin.put("-14908","pai"); ht_PinYin.put("-14902","pan"); ht_PinYin.put("-14894","pang"); ht_PinYin.put("-14889","pao"); ht_PinYin.put("-14882","pei"); ht_PinYin.put("-14873","pen"); ht_PinYin.put("-14871","peng"); ht_PinYin.put("-14857","pi"); ht_PinYin.put("-14678","pian"); ht_PinYin.put("-14674","piao"); ht_PinYin.put("-14670","pie"); ht_PinYin.put("-14668","pin"); ht_PinYin.put("-14663","ping"); ht_PinYin.put("-14654","po"); ht_PinYin.put("-14645","pu"); ht_PinYin.put("-14630","qi"); ht_PinYin.put("-14594","qia"); ht_PinYin.put("-14429","qian"); ht_PinYin.put("-14407","qiang"); ht_PinYin.put("-14399","qiao"); ht_PinYin.put("-14384","qie"); ht_PinYin.put("-14379","qin"); ht_PinYin.put("-14368","qing"); ht_PinYin.put("-14355","qiong"); ht_PinYin.put("-14353","qiu"); ht_PinYin.put("-14345","qu"); ht_PinYin.put("-14170","quan"); ht_PinYin.put("-14159","que"); ht_PinYin.put("-14151","qun"); ht_PinYin.put("-14149","ran"); ht_PinYin.put("-14145","rang"); ht_PinYin.put("-14140","rao"); ht_PinYin.put("-14137","re"); ht_PinYin.put("-14135","ren"); ht_PinYin.put("-14125","reng"); ht_PinYin.put("-14123","ri"); ht_PinYin.put("-14122","rong"); ht_PinYin.put("-14112","rou"); ht_PinYin.put("-14109","ru"); ht_PinYin.put("-14099","ruan"); ht_PinYin.put("-14097","rui"); ht_PinYin.put("-14094","run"); ht_PinYin.put("-14092","ruo"); ht_PinYin.put("-14090","sa"); ht_PinYin.put("-14087","sai"); ht_PinYin.put("-14083","san"); ht_PinYin.put("-13917","sang"); ht_PinYin.put("-13914","sao"); ht_PinYin.put("-13910","se"); ht_PinYin.put("-13907","sen"); ht_PinYin.put("-13906","seng"); ht_PinYin.put("-13905","sha"); ht_PinYin.put("-13896","shai"); ht_PinYin.put("-13894","shan"); ht_PinYin.put("-13878","shang"); ht_PinYin.put("-13870","shao"); ht_PinYin.put("-13859","she"); ht_PinYin.put("-13847","shen"); ht_PinYin.put("-13831","sheng"); ht_PinYin.put("-13658","shi"); ht_PinYin.put("-13611","shou"); ht_PinYin.put("-13601","shu"); ht_PinYin.put("-13406","shua"); ht_PinYin.put("-13404","shuai"); ht_PinYin.put("-13400","shuan"); ht_PinYin.put("-13398","shuang"); ht_PinYin.put("-13395","shui"); ht_PinYin.put("-13391","shun"); ht_PinYin.put("-13387","shuo"); ht_PinYin.put("-13383","si"); ht_PinYin.put("-13367","song"); ht_PinYin.put("-13359","sou"); ht_PinYin.put("-13356","su"); ht_PinYin.put("-13343","suan"); ht_PinYin.put("-13340","sui"); ht_PinYin.put("-13329","sun"); ht_PinYin.put("-13326","suo"); ht_PinYin.put("-13318","ta"); ht_PinYin.put("-13147","tai"); ht_PinYin.put("-13138","tan"); ht_PinYin.put("-13120","tang"); ht_PinYin.put("-13107","tao"); ht_PinYin.put("-13096","te"); ht_PinYin.put("-13095","teng"); ht_PinYin.put("-13091","ti"); ht_PinYin.put("-13076","tian"); ht_PinYin.put("-13068","tiao"); ht_PinYin.put("-13063","tie"); ht_PinYin.put("-13060","ting"); ht_PinYin.put("-12888","tong"); ht_PinYin.put("-12875","tou"); ht_PinYin.put("-12871","tu"); ht_PinYin.put("-12860","tuan"); ht_PinYin.put("-12858","tui"); ht_PinYin.put("-12852","tun"); ht_PinYin.put("-12849","tuo"); ht_PinYin.put("-12838","wa"); ht_PinYin.put("-12831","wai"); ht_PinYin.put("-12829","wan"); ht_PinYin.put("-12812","wang"); ht_PinYin.put("-12802","wei"); ht_PinYin.put("-12607","wen"); ht_PinYin.put("-12597","weng"); ht_PinYin.put("-12594","wo"); ht_PinYin.put("-12585","wu"); ht_PinYin.put("-12556","xi"); ht_PinYin.put("-12359","xia"); ht_PinYin.put("-12346","xian"); ht_PinYin.put("-12320","xiang"); ht_PinYin.put("-12300","xiao"); ht_PinYin.put("-12120","xie"); ht_PinYin.put("-12099","xin"); ht_PinYin.put("-12089","xing"); ht_PinYin.put("-12074","xiong"); ht_PinYin.put("-12067","xiu"); ht_PinYin.put("-12058","xu"); ht_PinYin.put("-12039","xuan"); ht_PinYin.put("-11867","xue"); ht_PinYin.put("-11861","xun"); ht_PinYin.put("-11847","ya"); ht_PinYin.put("-11831","yan"); ht_PinYin.put("-11798","yang"); ht_PinYin.put("-11781","yao"); ht_PinYin.put("-11604","ye"); ht_PinYin.put("-11589","yi"); ht_PinYin.put("-11536","yin"); ht_PinYin.put("-11358","ying"); ht_PinYin.put("-11340","yo"); ht_PinYin.put("-11339","yong"); ht_PinYin.put("-11324","you"); ht_PinYin.put("-11303","yu"); ht_PinYin.put("-11097","yuan"); ht_PinYin.put("-11077","yue"); ht_PinYin.put("-11067","yun"); ht_PinYin.put("-11055","za"); ht_PinYin.put("-11052","zai"); ht_PinYin.put("-11045","zan"); ht_PinYin.put("-11041","zang"); ht_PinYin.put("-11038","zao"); ht_PinYin.put("-11024","ze"); ht_PinYin.put("-11020","zei"); ht_PinYin.put("-11019","zen"); ht_PinYin.put("-11018","zeng"); ht_PinYin.put("-11014","zha"); ht_PinYin.put("-10838","zhai"); ht_PinYin.put("-10832","zhan"); ht_PinYin.put("-10815","zhang"); ht_PinYin.put("-10800","zhao"); ht_PinYin.put("-10790","zhe"); ht_PinYin.put("-10780","zhen"); ht_PinYin.put("-10764","zheng"); ht_PinYin.put("-10587","zhi"); ht_PinYin.put("-10544","zhong"); ht_PinYin.put("-10533","zhou"); ht_PinYin.put("-10519","zhu"); ht_PinYin.put("-10331","zhua"); ht_PinYin.put("-10329","zhuai"); ht_PinYin.put("-10328","zhuan"); ht_PinYin.put("-10322","zhuang"); ht_PinYin.put("-10315","zhui"); ht_PinYin.put("-10309","zhun"); ht_PinYin.put("-10307","zhuo"); ht_PinYin.put("-10296","zi"); ht_PinYin.put("-10281","zong"); ht_PinYin.put("-10274","zou"); ht_PinYin.put("-10270","zu"); ht_PinYin.put("-10262","zuan"); ht_PinYin.put("-10260","zui"); ht_PinYin.put("-10256","zun"); ht_PinYin.put("-10254","zuo"); ht_PinYin.put("-10247","zz"); } file://文件、目录复制 public static void fileCopy(String from, String to, boolean overwrite) throws Exception{ File src = new File(from); File dest = new File(to); try { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dest, !overwrite);
byte[] buf = new byte[512]; int size = fis.read(buf); while(size != -1) { fos.write(buf, 0, size); size = fis.read(buf); } fis.close(); fos.close(); }catch (FileNotFoundException fnfe){ if(src.isDirectory()) { /* 原文件是目录 复制目录*/ File[] files = src.listFiles(); Stack stack = new Stack(); for(int i = 0; i < files.length; i++) { dest.mkdirs(); System.out.println( files[i ]. getName() ); fileCopy(src + File.separator + files[ i].getName(), to + File.separator + files[ i].getName(), overwrite); } }else{ /* 原文件不存在或其它异常情况, 抛出异常*/ throw new Exception("原文件不存在或其它异常情况!"); } }catch (IOException e) { /* 未知的I/O异常 */ throw new Exception("未知的I/O异常!"); } } // end fileCopy()
// } // end class 
|