java常用函数2

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/***将某个日期以固定格式转化成字符串**@paramdate*@returnString*/publicstaticStringdateToStr(java.util.Datedate){SimpleDateFormatsdf=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);Stringstr=sdf.format(date);returnstr;}/***判断任意一个整数是否素数**@paramn*@returnboolean*/publicstaticbooleanisPrimes(intn){for(inti=2;i=Math.sqrt(n);i++){if(n%i==0){returnfalse;}}returntrue;}/***获得任意一个整数的阶乘,递归**@paramn*@returnn!*/publicstaticintfactorial(intn){if(n==1){return1;}returnn*factorial(n-1);}/***将指定byte数组以16进制的形式打印到控制台**@paramhint*String*@paramb*byte[]*@returnvoid*/publicstaticvoidprintHexString(Stringhint,byte[]b){System.out.print(hint);for(inti=0;ib.length;i++){Stringhex=Integer.toHexString(b[i]&0xFF);if(hex.length()==1){hex='0'+hex;}System.out.print(hex.toUpperCase()+);}System.out.println();}wait();//线程等待notify();//激活一个线程/**Db.javaCreatedon2007年8月20日,上午8:37*/importjava.io.*;importjava.sql.*;importjava.util.Properties;publicclassDb{privateStringdriver;privateStringurl;privateStringuser;privateStringpassword;privateConnectionconn;privateStatementstm;privateResultSetrs;publicDb(){this(DBConf.properties);}publicDb(Stringconf){loadProperties(conf);setConn();}publicConnectiongetConn(){returnthis.conn;}//handlethepropertiesfiletogettheinformationsforconnectionprivatevoidloadProperties(Stringconf){Propertiesprops=newProperties();try{props.load(newFileInputStream(conf));}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}this.driver=props.getProperty(driver);this.url=props.getProperty(url);this.user=props.getProperty(user);this.password=props.getProperty(password);}//implementtheConnectionprivatevoidsetConn(){try{Class.forName(driver);this.conn=DriverManager.getConnection(url,user,password);}catch(ClassNotFoundExceptionclassnotfoundexception){classnotfoundexception.printStackTrace();System.err.println(db:+classnotfoundexception.getMessage());}catch(SQLExceptionsqlexception){System.err.println(db.getconn():+sqlexception.getMessage());}}publicvoiddoInsert(Stringsql){try{Statementstatement=conn.createStatement();inti=stm.executeUpdate(sql);}catch(SQLExceptionsqlexception){System.err.println(db.executeInset:+sqlexception.getMessage());}}publicvoiddoDelete(Stringsql){try{stm=conn.createStatement();inti=stm.executeUpdate(sql);}catch(SQLExceptionsqlexception){System.err.println(db.executeDelete:+sqlexception.getMessage());}}publicvoiddoUpdate(Stringsql){try{stm=conn.createStatement();inti=stm.executeUpdate(sql);}catch(SQLExceptionsqlexception){System.err.println(db.executeUpdate:+sqlexception.getMessage());}}publicResultSetdoSelect(Stringsql){try{stm=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);rs=stm.executeQuery(sql);}catch(SQLExceptionsqlexception){System.err.println(db.executeQuery:+sqlexception.getMessage());}returnrs;}publicstaticvoidmain(String[]args){try{Dbdb=newDb();Connectionconn=db.getConn();if(conn!=null&&!conn.isClosed()){System.out.println(連結成功);ResultSetrs=db.doSelect(select*fromcontent);while(rs.next()){System.out.println(rs.getString(1)+:+rs.getString(2)+:+rs.getString(3));}rs.close();conn.close();}}catch(SQLExceptione){e.printStackTrace();}}}DBConf.properties:driver=oracle.jdbc.driver.OracleDriverurl=jdbc:oracle:thin:@tdt151:1521:trainuser=XXpassword=XX/***人民币转成大写**@paramvalue*@returnString*/publicstaticStringhangeToBig(doublevalue){char[]hunit={'拾','佰','仟'};//段内位置表示char[]vunit={'万','亿'};//段名表示char[]digit={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};//数字表示longmidVal=(long)(value*100);//转化成整形StringvalStr=String.valueOf(midVal);//转化成字符串Stringhead=valStr.substring(0,valStr.length()-2);//取整数部分Stringrail=valStr.substring(valStr.length()-2);//取小数部分Stringprefix=;//整数部分转化的结果Stringsuffix=;//小数部分转化的结果//处理小数点后面的数if(rail.equals(00)){//如果小数部分为0suffix=整;}else{suffix=digit[rail.charAt(0)-'0']+角+digit[rail.charAt(1)-'0']+分;//否则把角分转化出来}//处理小数点前面的数char[]chDig=head.toCharArray();//把整数部分转化成字符数组charzero='0';//标志'0'表示出现过0bytezeroSerNum=0;//连续出现0的次数for(inti=0;ichDig.length;i++){//循环处理每个数字intidx=(chDig.length-i-1)%4;//取段内位置intvidx=(chDig.length-i-1)/4;//取段位置if(chDig[i]=='0'){//如果当前字符是0zeroSerNum++;//连续0次数递增if(zero=='0'){//标志zero=digit[0];}elseif(idx==0&&vidx0&&zeroSerNum4){prefix+=vunit[vidx-1];zero='0';}continue;}zeroSerNum=0;//连续0次数清零if(zero!='0'){//如果标志不为0,则加上,例如万,亿什么的prefix+=zero;zero='0';}prefix+=digit[chDig[i]-'0'];//转化该数字表示if(idx0)prefix+=hunit[idx-1];if(idx==0&&vidx0){prefix+=vunit[vidx-1];//段结束位置应该加上段名如万,亿}}if(prefix.length()0)prefix+='圆';//如果整数部分存在,则有圆的字样returnprefix+suffix;//返回正确表示}publicstaticStringgetURLByDBInfo(DBInfodbInfo){Stringurl=;if(dbInfo.getDbType()!=null)if(dbInfo.getDbType().equals(SQLSERVER))url=(newStringBuilder(jdbc:microsoft:sqlserver://)).append(dbInfo.getDbHost()).append(:).append(dbInfo.getDbPort()).append(;DatabaseName=).append(dbInfo.getDbSID()).toString();elseif(dbInfo.getDbType().equals(ORACLE))url=(newStringBuilder(jdbc:oracle:thin:@)).append(dbInfo.getDbHost()).append(:).append(dbInfo.getDbPort()).append(:).append(dbInfo.getDbSID()).to

1 / 76
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功