packagecom.hy;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.text.MessageFormat;public classBatchInserter {//连接到数据库的四大属性
private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";private static final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";private static final String USER = "system";private static final String PSWD = "XXXXX";public static voidbatchInsert() {
Connection conn= null;
Statement stmt= null;try{
Class.forName(DRIVER).newInstance();
conn=DriverManager.getConnection(DBURL, USER, PSWD);
stmt=conn.createStatement();
StringBuilder sb=newStringBuilder();
sb.append("INSERT ALL ");
sb.append(" INTO firsttb(NAME, age,createdtime) values('E1','22',sysdate)");
sb.append(" INTO firsttb(NAME, age,createdtime) values('E2','32',sysdate)");
sb.append(" INTO firsttb(NAME, age,createdtime) values('E3','42',sysdate)");
sb.append("select * from dual");
String sql=sb.toString();
stmt.executeUpdate(sql);
sql= "select id,name,age,createdtime from firsttb";
ResultSet rs=stmt.executeQuery(sql);int index = 0;while(rs.next()) {
index++;
String id= rs.getString("id");
String name= rs.getString("name");
String age= rs.getString("age");
String cdate= rs.getString("createdtime");
String raw= "#{0},{1},{2},{3},{4}";
Object[] arr={ index, id, name, age, cdate };
String outStr=MessageFormat.format(raw, arr);
System.out.println(outStr);
}
}catch(Exception e) {
System.out.print(e.getMessage());
}finally{try{
stmt.close();
conn.close();
}catch(SQLException e) {
System.out.print("Can't close stmt/conn because of " +e.getMessage());
}
}
}/*** 执行点
*@paramargs*/
public static voidmain(String[] args) {
batchInsert();
}
}