java⼯程师必备的20个java代码
1.String a = String.valueOf(2);
//integer to numeric string
int i = Integer.parseInt(a); //numeric
string to an int
String a = String.valueOf(2);
//integer to numeric string
int i = Integer.parseInt(a); //numeric string to an
int
2.向javajavajavajavajdbcoracleoraclejdbcoraclejdbcoracleoraclejdbcoraclejdbcoraclejdbcoraclejdbcoracleoraclejdbcoraclejdbcjavajavajav ⽂件中添加⽂本
Updated: Thanks Simone for pointing to exception. I have
changed the code.
BufferedWriter ut = null;
try
{
out = new BufferedWriter(new FileWriter(”
filename”, true));
out.write(”
aString”);
} catch (IOException e) {
/
/ error processing code
} finally
{
if (out != null) {
out.close();
}
}
BufferedWriter ut = 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.获取现在正调⽤的⽅法名
String methodName =
Thread.currentThread().getStackTrace()[1].getMethodName();
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName ();
4.在中将String型转换成Date型
javajavajavajava.util.Date =
.DateInstance().parse(date String);
.util.Date = .DateInstance().parse(date
String);or
SimpleDateFormat format = new SimpleDateFormat( "" );
Date date = format.parse( myString );
SimpleDateFormat format = new SimpleDateFormat( "" ); Date date
= format.parse( myString );
5.通过 链接数据库
public class Test
{
String driverClass =
"..driver.Driver";
Connection con;
public void init
(FileInputStream fs) throws ClassNotFoundException,
SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
props.load(fs);
java valueofString url = Property
("db.url");
String
userName = Property("db.user");
String password = Property
("db.password");
Class.forName(driverClass);
Connection(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)
{
Test test = new
Test();
test.init();
test.fetch
();
}
}
public class Test
{
String driverClass =
"..driver.Driver";
Connection con;
public void init(FileInputStream fs) throws ClassNotFoundException,
SQLException, FileNotFoundException, IOException
{
Properties props = new Properties();
props.load
(fs);
String url = Property
("db.url");
String userName = Property
("db.user");
String password = Property
("db.password");
Class.forName(driverClass);
Connection(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)
{
Test test = new Test
();
test.init();
test.fetch();
}
}
6.将中的util.Date转换成sql.Date
这⼀⽚段显⽰如何将⼀个 util Date转换成sql Date⽤于数据库