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

Java 如何在带有bean的jdbc准备语句中使用INSERT INTO ALL语句,java,sql,jdbc,Java,Sql,Jdbc,请给出一些示例,说明如何在JSFbean中的jdbc准备语句中使用“INSERT INTO ALL语句” 实际上,我想使用单个jsf页面和每个员工id使用一个文本框来获取当前员工的员工id 如何使用INSERT INTO ALL语句来实现这一点 下面是我的代码片段 AttendanceBean.java: public class AttendanceBean { private int atteid; private String attdnam

请给出一些示例,说明如何在JSFbean中的jdbc准备语句中使用“INSERT INTO ALL语句”

实际上,我想使用单个jsf页面和每个员工id使用一个文本框来获取当前员工的员工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();
            }
        }
      }
    }    

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

插入到
tbl(第1列、第2列、第3列)
价值观
(val1a、val2a、val3a),
(val1b、val2b、val3b),
(val1c,val2c,val3c),
...
然而,在JDBC中,最好在循环中使用,然后使用a来执行多重插入。以下是一个启动示例:

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) {}
    }
}
private static final String SQL_INSERT=“插入tbl(col1、col2、col3)值(?,?)”;
公共作废保存(列出实体)引发SQLException{
连接=空;
PreparedStatement=null;
试一试{
connection=database.getConnection();
语句=connection.prepareStatement(SQL\U插入);
for(实体:实体){
语句.setObject(1,entity.getCol1());
语句.setObject(2,entity.getCol2());
语句.setObject(3,entity.getCol3());
语句addBatch();
}
语句。executeBatch();
}最后{
如果(statement!=null),请尝试{statement.close();}catch(SQLException ignore){}
如果(connection!=null),请尝试{connection.close();}catch(SQLException ignore){}
}
}

这个问题与JSF完全无关。在普通的独立Java类中执行此操作时,您会遇到完全相同的问题。请仔细标记。