我创建了一个表单,使用JSP将数据填充到MySQL表中。在更新部分,当我输入具有预期主键值的数据并点击提交按钮时,它什么也没做,只是得到一个空白页面。
我对java编程还很陌生,请帮忙。
<%
//jsp prog for database connection
Connection conn = null;
PreparedStatement pstatement = null;
try {
String candidate = request.getParameter("candidate");
// So on....
Connection conn=ConnectionProvider.getConn();
int updateQuery = 0;
if(candidate!=null && phone!=null && recname!=null && ofrdate!=null && ofrstat!=null && ofrdctc!=null && DOJ!=null && month!=null && jngstat!=null)
{
if(candidate!="" && phone!="" && recname!="" && ofrdate!="" && ofrstat!="" && ofrdctc!="" && DOJ!="" && month!="" && jngstat!="")
{
String queryString =("UPDATE mytable SET recname=?,ofrdate=?,ofrstat=?,ofrdctc=?,DOJ=?,month=?,jngstat=? WHERE phone=?");
pstatement = conn.prepareStatement(queryString);
pstatement.setString(1, phone);
pstatement.setString(2, recname);
pstatement.setString(3, ofrdate);
pstatement.setString(4, ofrstat);
pstatement.setString(5, ofrdctc);
pstatement.setString(6, DOJ);
pstatement.setString(7, month);
pstatement.setString(8, jngstat);
updateQuery = pstatement.executeUpdate();
} // end
} // end of try
// catch exception block
// finally block
%>
我认为错误在这里:
String queryString =("UPDATE mytable SET recname=?,ofrdate=?,ofrstat=?,ofrdctc=?,DOJ=?,month=?,jngstat=? WHERE phone=?");
pstatement = conn.prepareStatement(queryString);
pstatement.setString(1, phone);
pstatement.setString(2, recname);
pstatement.setString(3, ofrdate);
pstatement.setString(4, ofrstat);
pstatement.setString(5, ofrdctc);
pstatement.setString(6, DOJ);
pstatement.setString(7, month);
pstatement.setString(8, jngstat);
您的通配符和pstatement.setString(int, String);
之间没有对应关系,您应该按照它们在查询字符串中出现的顺序设置它们。
例如,第一个通配符指的是recname
字段,但您将其设置为:pstatement.setString(1, phone);
也许这就是更新失败的原因。
尝试从字符串recname
开始按顺序设置通配符,第8张设置为字符串phone
:
pstatement.setString(1, recname);
pstatement.setString(2, ofrdate);
pstatement.setString(3, ofrstat);
pstatement.setString(4, ofrdctc);
pstatement.setString(5, DOJ);
pstatement.setString(6, month);
pstatement.setString(7, jngstat);
pstatement.setString(8, phone);