在AIX Version 5.3中使用Java和PHP技术进行开发(3)
http://tech.ddvip.com 2007年08月27日 社区交流
内容摘要:在 AIX® 中可以开发使用 Java™ 和 PHP 技术的应用程序。在获得使用 PHP 作为基于 Web 的接口平台的好处的同时,您可以使用 Java 编程语言编写核心逻辑(或者重新部署现有的基于 Java 的应用程序)。在本文(系列文章的第 3 部分)中,介绍了如何将第 2 部分中创建的核心应用程序连接到 DB2® 数据库,以便对问卷调查的问题和回答进行存储。
然后,您可以使用 getGeneratedKeys() 方法来接受这个自动生成的 ID。这将返回一个 resultset 对象,您需要这个过程所返回的第一个值,以获得新的唯一的 ID。清单 8 显示了这个过程的代码。
清单 8. 为每个问卷调查获得唯一的 IDtry {
s = conn.createStatement();
s.executeUpdate(
"INSERT INTO survey_response (responseid) "
+ "values (0)",
Statement.RETURN_GENERATED_KEYS);
rs = s.getGeneratedKeys();
if (rs.next()) {
responseid = rs.getInt(1);
} else {
System.out.println("Can't get auto incremement data");
out.println("Sorry, we couldn't write your responses into the DB");
}
rs.close();
s.close ();
} catch (Exception ex) {
System.out.println("SQLException (getting responseid): " +
ex.getMessage());
}
最后,您必须通过确定合适的 INSERT 语句来插入信息,并将数据写入到数据库中。必须对每个问卷调查问题完成这项操作,所以,您可以在相同的循环(以前是 Servlet 的一部分,用于输出问卷调查结果)中执行这项操作(请参见清单 9)。
清单 9. 插入问卷调查数据for(Iterator<SurveyQuestion> i = this.survey.iterator(); i.hasNext(); ) {
SurveyQuestion question = (SurveyQuestion) i.next();
question.showquestion(out,false);
out.println("<p>" + request.getParameter("field" + fieldid));
try {
ps = conn.prepareStatement (
"INSERT INTO survey_response_detail " +
"(responseid, question, responsestring) VALUES(?,?,?)");
ps.setString (1,responseid.toString());
ps.setString (2,fieldid.toString());
ps.setString (3,request.getParameter("field" + fieldid));
int count = ps.executeUpdate ();
ps.close ();
} catch (Exception ex) {
System.out.println("SQLException (adding question result): " +
ex.getMessage());
}
fieldid++;
}
我们在上面的代码中使用了一个预编译语句,以使得能够更容易地在查询中填充相关的数据。我们本可以使用一个预编译语句并且每次重新设置数据,但是为了保持完整性,我们决定在每次循环的时候重新创建它。
来源:ibm 作者:Doug Monroe 责编:豆豆技术应用