1
请给我举一个例子,说明如何在jsf bean中的jdbc prepared语句中使用“INSERT INTO ALL STATEMENT”?如何在jdbc prepared语句中使用INSERT INTO ALL语句
其实我想使用单个jsf页面和每个员工ID使用一个文本框的员工编号的当前员工。
如何使用INSERT INTO ALL语句实现此目的?
以下是我的代码片段。
AttendanceBean.java:
public class AttendanceBean {
private int atteid;
private String attdname;
private int attday;
private int attmonth;
private int attyear;
public static Connection getAttConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:globldb3";
String username = "scott";
String password = "tiger";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public String addAttendance(){
Connection conn = null;
PreparedStatement pstmt = null;
boolean committed = false;
try {
conn = getAttConnection();
conn.setAutoCommit(false);
String query = "INSERT ALL INTO attendance VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1,this.atteid);
pstmt.setString(2,this.attdname);
pstmt.setInt(3,this.attday);
pstmt.setInt(4,this.attmonth);
pstmt.setInt(5,this.attyear);
pstmt.executeUpdate();
conn.commit();
conn.setAutoCommit(true);
committed = true;
return "home.xhtml";
} catch (Exception e) {
e.printStackTrace();
return "CRM.xhtml";
} finally {
try{
if (!committed) conn.rollback();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
这个问题与JSF完全无关。在正常的独立Java类中这样做会导致完全相同的问题。请仔细标记。 – BalusC 2011-05-20 12:16:47