資源描述:
《非常有用的java代碼段》由會員上傳分享,免費在線閱讀,更多相關內容在行業(yè)資料-天天文庫。
1、20非常有用的Java程序片段(1)2013-06-1414:57陳皓酷殼網?我要評論(0)?字號:T?
2、?T下面是20個非常有用的Java程序片段,希望能對你有用。AD:2013大數(shù)據全球技術峰會課程PPT下載下面是20個非常有用的Java程序片段,希望能對你有用。1.字符串有整型的相互轉換1.String?a?=?String.valueOf(2);???//integer?to?numeric?string???2.int?i?=?Integer.parseInt(a);?//numeric?string?to?an?int??2.向文件末尾添加內容1.Bu
3、fferedWriter?out?=?null;???2.try?{???3.????out?=?new?BufferedWriter(new?FileWriter(”filename”,?true));???4.????out.write(”aString”);???5.}?catch?(IOException?e)?{???6.????//?error?processing?code???7.}?finally?{???8.????if?(out?!=?null)?{???9.????????out.close();???10.????}???11.}??3.
4、得到當前方法的名字1.String?methodName?=?Thread.currentThread().getStackTrace()[1].getMethodName();??4.轉字符串到日期1.java.util.Date?=?java.text.DateFormat.getDateInstance().parse(date?String);??或者是:1.SimpleDateFormat?format?=?new?SimpleDateFormat(?"dd.MM.yyyy"?);???2.Date?date?=?format.parse(?myStri
5、ng?);??5.使用JDBC鏈接Oracle1.public?class?OracleJdbcTest???2.{???3.????String?driverClass?=?"oracle.jdbc.driver.OracleDriver";???4.???5.????Connection?con;???6.???7.????public?void?init(FileInputStream?fs)?throws?ClassNotFoundException,?SQLException,?FileNotFoundException,?IOException???8
6、.????{???9.????????Properties?props?=?new?Properties();???10.????????props.load(fs);???11.????????String?url?=?props.getProperty("db.url");???12.????????String?userName?=?props.getProperty("db.user");???13.????????String?password?=?props.getProperty("db.password");???14.????????Class.
7、forName(driverClass);???15.???16.????????con=DriverManager.getConnection(url,?userName,?password);???17.????}???18.???19.????public?void?fetch()?throws?SQLException,?IOException???20.????{???21.????????PreparedStatement?ps?=?con.prepareStatement("select?SYSDATE?from?dual");???22.?????
8、???ResultSet?rs?=?ps.executeQuery();???23.???24.????????while?(rs.next())???25.????????{???26.????????????//?do?the?thing?you?do???27.????????}???28.????????rs.close();???29.????????ps.close();???30.????}???1.???2.????public?static?void?main(String[]?args)???3.????{???4.????????Oracle
9、JdbcT