如何在带有bean的jdbc准备好的语句中使用INSERT INTO ALL语句
请给我一些例子,说明如何在 jsf bean 内的 jdbc 准备好的语句中使用“INSERT INTO ALL STATEMENT”?
实际上,我想使用单个 jsf 页面并为每个员工 ID 使用一个文本框来获取当前员工的员工 ID。
我如何使用 INSERT INTO ALL 语句来实现此目的?
以下是我的代码片段。
出勤Bean.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();
}
}
}
}
Kindly give me some example that how we can use "INSERT INTO ALL STATEMENT" in jdbc prepared statement inside a jsf bean?
Actually i want to take employee id's of present employees using single jsf page and using one textbox for each employee id.
How can i use INSERT INTO ALL statement to achieve this?
Following is my code snippet.
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();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多重插入的正确 SQL 语法是:
但是,在 JDBC 中,您最好使用
PreparedStatement#addBatch()
在循环中,后跟executeBatch()
进行多重插入。这是一个启动示例:The correct SQL syntax for a multi-insert is:
However, in JDBC, you'd better use
PreparedStatement#addBatch()
in a loop, followed by aexecuteBatch()
to do a multi-insert. Here's a kickoff example: