1. 字符串有整型的相互轉換
1 2 3 |
String a = String.valueOf( 2 ); //integer to numeric string int i = Integer.parseInt(a); //numeric string to an int |
2. 向文件末尾添加內容
1 2 3 4 5 6 7 8 9 10 11 12 |
BufferedWriter out = null ; try {
out = new BufferedWriter( new FileWriter(”filename”, true ));
out.write(”aString”); } catch (IOException e) {
// error processing code } finally {
if (out != null ) {
out.close();
} } |
3. 得到當前方法的名字
1 | String methodName = Thread.currentThread().getStackTrace()[ 1 ].getMethodName(); |
4. 轉字符串到日期
1 2 |
java.util.Date = java.text.DateFormat.getDateInstance().parse(date String); |
或者是:
1 2 3 |
SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" ); Date date = format.parse( myString ); |
5. 使用JDBC鏈接Oracle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public class OracleJdbcTest {
String driverClass = "oracle.jdbc.driver.OracleDriver" ;
Connection con;
public void init(FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty( "db.url" );
String userName = props.getProperty( "db.user" );
String password = props.getProperty( "db.password" );
Class.forName(driverClass);
con=DriverManager.getConnection(url, userName, password);
}
public void fetch() throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement( "select SYSDATE from dual" );
ResultSet rs = ps.executeQuery();
while (rs.next())
{
// do the thing you do
}
rs.close();
ps.close();
}
public static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest();
test.init();
test.fetch();
} } |