如何在带有bean的jdbc准备好的语句中使用INSERT INTO ALL语句

发布于 2024-11-08 04:28:14 字数 1773 浏览 0 评论 0原文

请给我一些例子,说明如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2024-11-15 04:28:14

多重插入的正确 SQL 语法是:

INSERT INTO 
    tbl (col1, col2, col3) 
VALUES
    (val1a, val2a, val3a),
    (val1b, val2b, val3b),
    (val1c, val2c, val3c),
    ...

但是,在 JDBC 中,您最好使用 PreparedStatement#addBatch() 在循环中,后跟 executeBatch() 进行多重插入。这是一个启动示例:

private static final String SQL_INSERT = "INSERT INTO tbl (col1, col2, col3) VALUES (?, ?, ?)";

public void save(List<Entity> entities) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_INSERT);

        for (Entity entity : entities) {
            statement.setObject(1, entity.getCol1());
            statement.setObject(2, entity.getCol2());
            statement.setObject(3, entity.getCol3());
            statement.addBatch();
        }

        statement.executeBatch();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
}

The correct SQL syntax for a multi-insert is:

INSERT INTO 
    tbl (col1, col2, col3) 
VALUES
    (val1a, val2a, val3a),
    (val1b, val2b, val3b),
    (val1c, val2c, val3c),
    ...

However, in JDBC, you'd better use PreparedStatement#addBatch() in a loop, followed by a executeBatch() to do a multi-insert. Here's a kickoff example:

private static final String SQL_INSERT = "INSERT INTO tbl (col1, col2, col3) VALUES (?, ?, ?)";

public void save(List<Entity> entities) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_INSERT);

        for (Entity entity : entities) {
            statement.setObject(1, entity.getCol1());
            statement.setObject(2, entity.getCol2());
            statement.setObject(3, entity.getCol3());
            statement.addBatch();
        }

        statement.executeBatch();
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文