所以,这是我的一段代码,它将详细信息输入名为vendor_db的数据库中(使用java和mysql),即使代码看起来正确,我每次都会收到错误。 我已经添加了整个servlet代码以供参考。
@WebServlet("/DBHandler")
public class DBHandler extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DBHandler() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
FormBean f = (FormBean)request.getAttribute("formHandler");
boolean userExists = false;
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/ioc?autoReconnect=true&useSSL=false";
final String USER = "root";
final String PASS = "1234";
try( Connection conn=DriverManager.getConnection(DB_URL,USER,PASS)){
Class.forName(JDBC_DRIVER);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select vcode from vendor_db");
while(rs.next()) {
if(f.getCode().equals(rs.getString(1)))
{
userExists=true;
}
}
if(userExists) {
f.setErrors("code","Duplicate user: Try a different vendor code");
getServletConfig().getServletContext().getRequestDispatcher("/forms/retry.jsp").forward(request, response);
}
else {
String sql= "insert into vendor_db(vcode,vname,vmob,vemail,vpass,gst_no)values(?,?,?,?,?,?)";
try(PreparedStatement prep = conn.prepareStatement(sql)){
prep.setString(1,f.getCode());
prep.setString(2,f.getName());
prep.setString(3,f.getPhone());
prep.setString(4,f.getEmail());
prep.setString(5,f.getPass());
prep.setString(6,f.getGst_no());
int i = prep.executeUpdate();
prep.clearParameters();
if(i!=0) {
getServletConfig().getServletContext().getRequestDispatcher("/forms/login.jsp").forward(request, response);
}
else {
pw.println("Not entered in database");
}
prep.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
但是,我一直得到同样的错误:
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
at DBHandler.doPost(DBHandler.java:56)
它基本上在executeUpdate()行中显示错误。请帮助。谢谢!
答案 0 :(得分:0)
由于代码看起来很好,可能是多用户问题。
首先,我会检查DBHandler的第56行确实是executeUpdate。
DBHandler.doPost(DBHandler.java:56)
不知怎的,我对此表示怀疑。可能是部署出错或其他什么。
如果DBHandler或其他数据错误地成为servlet的字段,则servlet不是无状态的,并且两个doPosts / doGets可以"共享"相同的字段,覆盖它等。
当显示的代码在源中分割,并且控制流允许覆盖字段时,例如在doGet或服务中,也可能发生。
没有字段。
添加完整个代码后
不幸的是只有小点:
// Not needed nowadays: Class.forName(JDBC_DRIVER);
try (Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
PreparedStatement st = conn.prepareStatement(
"select vcode from vendor_db where vcode = ?")) {
st.setString(f.getCode());
try (ResultSet rs = st.executeQuery()) {
if (rs.next()) {
userExists = true;
}
}
if (userExists) {
f.setErrors("code",
"Duplicate user: Try a different vendor code");
getServletConfig().getServletContext()
.getRequestDispatcher("/forms/retry.jsp")
.forward(request, response);
}
else {
String sql= "insert into vendor_db"
+ "(vcode,vname,vmob,vemail,vpass,gst_no)"
+ "values(?,?,?,?,?,?)";
try (PreparedStatement prep = conn.prepareStatement(sql)) {
prep.setString(1, f.getCode());
prep.setString(2, f.getName());
prep.setString(3, f.getPhone());
prep.setString(4, f.getEmail());
prep.setString(5, f.getPass());
prep.setString(6, f.getGst_no());
int i = prep.executeUpdate();
// Not needed here: prep.clearParameters();
if (i != 0) {
getServletConfig().getServletContext()
.getRequestDispatcher("/forms/login.jsp")
.forward(request, response);
}
else {
pw.println("Not entered in database");
}
}
}
}
catch (Exception e)
{
log("An error happened", e);
e.printStackTrace();
}
请参阅java.sql.SQLException: Column count doesn't match value count at row 1了解相同的错误。
我的猜测是应用程序以某种方式未在Web服务器中重新部署。
at DBHandler.doPost(DBHandler.java:56)
这应该是int i = prep.executeUpdate();
指向DBHandler.java的第56行。添加几条空行以发布数字是否发生变化。
6个问号(没有引号 - 罚款),6个列名,6个参数设置。