【问题标题】:Pass on getGeneratedKeys to next page将 getGeneratedKeys 传递到下一页
【发布时间】:2014-09-18 04:06:53
【问题描述】:

我想将getGneratedKeys(也称为last_generated_id)从当前Java 类传递到下一个类。但是,我对如何存储收集 ID 到下一个 Java 类以供此类使用。

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

Save.javainsert.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.javaUserImage.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;

}

【问题讨论】:

  • 什么是用户旅程?页面是如何导航的?
  • @Braj /Student/UserImage.jsp/Student/error.jsp 用户提交insert.jsp后,如果成功,会跳转到UserImage.jsp

标签: java mysql jsp jdbc struts2


【解决方案1】:

算法:

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?

【讨论】:

  • 您好,感谢您的快速回复。但是,我在哪里放置这些代码?我是初学者,所以我还没有学会在哪里插入这样的代码。它在sql命令中吗?谢谢
【解决方案2】:

在动作类中,您应该创建一个属性(使用 getter 和 setter)来保存来自getGeneratedKeys 的值。让它成为您通过第一个操作初始化的last_generated_id

该操作返回UserImage.jsp,您可以在其中使用绑定到包含值的操作属性的hidden 字段。

当您将表单提交到第二个操作时,此值将填充到第二个操作属性。因此,当您需要在执行第二个操作时使用它时,您可以轻松使用它。

因为您对操作使用不同的类,所以您应该在两个类中创建此属性。注意通过提供适当的操作配置,OGNL 正确绑定和填充隐藏字段。

【讨论】:

  • 有帮助吗?您想发布一个 JSP 页面来告诉您该字段的放置位置吗?
【解决方案3】:

只需get last inserted idwhile 更新时间。

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

还有你的更新帖子

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多