PreparedStatement不工作,即JSP中的ps.executeUpdate()

PreparedStatement不工作,即JSP中的ps.executeUpdate(),jsp,jdbc,Jsp,Jdbc,PreparedStatement不起作用。我甚至检查了打印值以确保不发送空值。ps.executeUpdate是它不起作用的主要区域。不知道这是否是语法错误。感谢您抽出时间 <%@page import="java.sql.*"%> <%! String fid,lid,gid,did,mn,yid,sid,cid,eid,aid,uid,pid,sttid,paswd; Connection con; Statement st; PreparedStatement ps,p

PreparedStatement不起作用。我甚至检查了打印值以确保不发送空值。ps.executeUpdate是它不起作用的主要区域。不知道这是否是语法错误。感谢您抽出时间

<%@page import="java.sql.*"%>
<%!
String fid,lid,gid,did,mn,yid,sid,cid,eid,aid,uid,pid,sttid,paswd;
Connection con;
Statement st;
PreparedStatement ps,ps1;
%>
<%
uid=(String)session.getAttribute("usr");
paswd=(String)session.getAttribute("pswd");
sttid=(String)session.getAttribute("stfid");
int staid=Integer.parseInt(sttid);

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","sms","vmhs");
String ins="insert into staff values (?,?,?,?)";
ps=con.prepareStatement(ins);
    ps.setString(1,uid);
    ps.setString(2,paswd);
    ps.setInt(3,staid);
    ps.setString(4,"active");
int i=ps.executeUpdate();
String upd="update staffinfo set pword=? where uname=?";
ps1=con.prepareStatement(upd);
ps1.setString(11,uid);
ps1.setString(12,paswd);
int j=ps1.executeUpdate();
if(j>0)
{
%>
<jsp:include page="averify.jsp"/>
<script>
alert("Registration verified successfully");
</script>
<%
}
else
{
%>
<jsp:include page="averify.jsp"/>
<script>
alert("Registration failed.Please check connection and try again");
</script>
<%
}
con.close();
}
catch(SQLException e)
{
e.getMessage();
}
%>

我不太确定您正在处理多少个表。您的insert语句插入员工价值观?,?,?,?在员工桌上。然后您正在更新更新Statffinfo set pword=?其中uname=?。请澄清它们是否相同

此外,我意识到你正在这样做:

"update staffinfo set pword=? where uname=?"
ps1.setString(11,uid); // This is username you are passing to password field
ps1.setString(12,paswd);// This is password being passed to username field
int j=ps1.executeUpdate();
是否要改为键入以下内容:

ps1.setString(1, paswd); 
ps1.setString(2, uid); 
int j=ps1.executeUpdate();
请试试这个。此外,在开发阶段,您应该始终打印错误消息,因为每个错误都会随其描述一起报告给您。您应将此行更改为:

catch (SQLException e) {
    System.out.println(e.toString());
}
以便您可以读取准确的错误消息。这将使您更容易调试错误。

使用con.commit更新后提交事务如何;或者也可以使用con.setAutoCommittrue;:

您还可以使用.toString方法,如下所示:

因此,不是:

uid=(String)session.getAttribute("usr");
paswd=(String)session.getAttribute("pswd");
sttid=(String)session.getAttribute("stfid");
int staid=Integer.parseInt(sttid);
String upd="update staffinfo set pword=? where uname=?";
ps1=con.prepareStatement(upd); 
ps1.setString(11,uid); 
ps1.setString(12,paswd); 
int j=ps1.executeUpdate();
您可以尝试:

uid = session.getAttribute("usr").toString();
paswd = session.getAttribute("pswd")toString();
sttid = session.getAttribute("stfid")toString();
int staid = Integer.parseInt(sttid);
为什么数字是11和12

我认为应该是1和2以及@Michael建议的更改

而不是:

uid=(String)session.getAttribute("usr");
paswd=(String)session.getAttribute("pswd");
sttid=(String)session.getAttribute("stfid");
int staid=Integer.parseInt(sttid);
String upd="update staffinfo set pword=? where uname=?";
ps1=con.prepareStatement(upd); 
ps1.setString(11,uid); 
ps1.setString(12,paswd); 
int j=ps1.executeUpdate();
它应该看起来像:

String upd="update staffinfo set pword=? where uname=?";
ps1=con.prepareStatement(upd);
ps1.setString(1,paswd);
ps1.setString(2,uid);
int j=ps1.executeUpdate();

第11列和第12列,因为它是dbn中的第11列和第12列,所以我认为DB中的列数应该不重要@Justin。正在传递的值的数量应与?查询中的占位符。谢谢您的更正。我以为它指的是DB中的列号。谢谢您Nitin。上帝保佑您。@NitinNair是对的。setString方法中给出的第一个参数是?它是PreparedStatement对象中的占位符。您还应该注意,索引从1开始。最好让那些花时间为您的问题提供解决方案的人知道他们的建议是否有帮助,以及在哪些方面有帮助。它还将帮助其他人了解这个问题是如何解决的。所以,一旦你开始工作,就不要离开。很抱歉我在做其他工作。谢谢。它成功了。上帝保佑你@MichaelWoyoI已经检查了错误,它说列索引无效。update语句没有执行。insert语句已经执行。