将getGeneratedKeys传递给下一页

时间:2014-07-26 11:06:37

标签: java mysql jsp struts2 ognl

我想将当前java类中的 generatedkeys (也称为 last_generated_id )传递给下一个java类。但是,我对如何存储收集 ID到下一个要使用的java类的java类非常不知所措。

方案是insert.jsp将将所有字符串和int 插入到我的mysql数据库中。第二页,UserImage.jsp将将图像上传到与insert.jsp相同的表和相同的记录。这意味着UserImage查询将是更新查询而不是插入查询。

Save.java - > insert.jsp

的关联类
public String execute()
{ 
   int i = 0; //SELECT LAST_INSERT_ID();
try{
 Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull","root","8899");

String s = "insert into usertable(userNRIC, userName, userEmail, userPW, userContact, userAddress, catID, catTypeID, sectionID, status"
                        + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
PreparedStatement ps=con.prepareStatement(s, Statement.RETURN_GENERATED_KEYS);

ps.setString(1, mb.getUserNRIC());
ps.setString(2, mb.getUserName());
ps.setString(3, mb.getUserEmail());
ps.setString(4, mb.getUserPW());
ps.setInt(5, mb.getUserContact());
ps.setString(6, mb.getUserAddress());
ps.setInt(7, mb.getCatID());
ps.setInt(8, mb.getCatTypeID());
ps.setInt(9, mb.getSectionID());
ps.setString(10, mb.getStatus());       
ps.executeUpdate();
con.commit();
ResultSet rs = ps.getGeneratedKeys();
if (rs != null && rs.next()) {
    i = rs.getInt(1);
    //ps.setInt(i, mb.getUserID("UserID"));
}
    ps.close();
    con.close();

    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

    }
    return SUCCESS;

}

FileUploadAction.java - > UserImage.jsp的关联类

public String execute(){
    int i = 0;
    connection = getConnection();
    String query = "update usertable set userPhoto=? where userID=?";
    try {
        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
        System.out.println("Server path:" + filePath);
        File fileToCreate = new File(filePath, this.userImageFileName);

        FileUtils.copyFile(this.userImage, fileToCreate);
         //String uniqueFileName = fileToCreate.getPath();
        pst = connection.prepareStatement(query);
        pst.setString(1, this.userImageFileName);

        i = pst.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }
    return SUCCESS;

}

提前致谢。顺便说一下,我正在使用struts2框架。

3 个答案:

答案 0 :(得分:0)

ALGO:

Record record = Fire the select query based on userID ordered by last insertion
If record is found then
    get the generated value from first record
    update the record
Else
    insert new record

根据你的评论

用户旅程:Student.jsp - > UserImage.jsp

只需将自动生成的随机密钥存储为请求属性,然后将客户端传递给UserImage.jsp,并在单击上传按钮时发送回服务器。

详细了解Pass data from Java Servlet to JSP?

答案 1 :(得分:0)

在动作类中,您应该创建一个属性(使用getter和setter)来保存generatedkeys的值。让它为last_generated_id,您可以通过第一个操作进行初始化。该操作返回UserImage.jsp,您可以使用绑定到包含值的action属性的hidden字段。当您将表单提交到第二个操作时,此值将填充到第二个操作属性。因此,当您需要在执行第二个操作时使用它时,可以轻松实现。因为您为操作使用不同的类,所以您应该在两个类中创建此属性。通过提供适当的操作配置,注意OGNL正确绑定和填充隐藏字段。

答案 2 :(得分:0)

更新时间,只需get last inserted id

Statement stmt = con.createStatement();
String query = "SELECT max(userID) FROM usertable";
int my_last_generated_id =  stmt.executeQuery(query).uniqueResult(); 

和你的UpdatePost

 pst = connection.prepareStatement(query);
 pst.setString(1, this.userImageFileName);
 pst.setInt(2, my_last_generated_id );
 i = pst.executeUpdate();