【问题标题】:Parameter index out of range (1 > number of parameters, which is 0) in update更新中的参数索引超出范围(1 > 参数个数,即 0)
【发布时间】:2019-08-07 08:19:05
【问题描述】:

我的私人项目有问题,它的功能之一(日记修改)。问题是,java.sql.SQLException: UPDATE 中的参数索引超出范围(1 > 参数数量,即 0)。

我以前在编辑后会转到列表的第一页,但我将其设置为直接转到查看页面,以便检查更正后的文本。我找到了google,但是关于INSERT INTO有错误,但是我在INSERT INTO中没有问题。

代码中唯一改变的是try语法里面的script标签,因为变量,之前就报错了,但和改正的不同的是,根本就没有修复。

<%
String dbUrl = "jdbc:mysql://localhost:3306/diary?serverTimezone=UTC";
request.setCharacterEncoding("UTF-8"); 

try {       
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection conn = DriverManager.getConnection(dbUrl, "root","@Kanamycin1");
    int idx = Integer.parseInt(request.getParameter("idx"));
    int pg = Integer.parseInt(request.getParameter("pg"));
    String title = request.getParameter("title"); 
    String contents = request.getParameter("contents"); 
    contents = contents.replace("\r\n","<br>");

    String sql = "SELECT NUM FROM diary WHERE NUM=" + idx;
    PreparedStatement pstmt = conn.prepareStatement(sql);
    Statement stmt = conn.createStatement();
    sql = "UPDATE diary SET TITLE='" + title+ "' ,CONTENTS='"+ contents +"' WHERE NUM=" + idx; 
    stmt.executeUpdate(sql);

    pstmt.setString(1, title);
    pstmt.setString(2, contents);

    pstmt.execute();
    pstmt.close();

    conn.close();
%>
  <script language=javascript>
   self.window.alert("Modified");
   location.href="view.jsp?idx=<%=idx%>&pg=<%=pg%>"; 
   </script>
<%  
} catch(SQLException e) {
out.println( e.toString() );
} 
%>

这里是 SQL 代码,用于修改文字。 (使用UPDATE,出现错误)

<%
String dbUrl = "jdbc:mysql://localhost:3306/diary?serverTimezone=UTC";
request.setCharacterEncoding("UTF-8"); 

try {       
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection conn = DriverManager.getConnection(dbUrl, "root","@Kanamycin1");

    String sql = "INSERT INTO diary(TIME,TITLE,CONTENTS) VALUES(?,?,?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String time = (new SimpleDateFormat("yyyy-MM-dd")).format( new Date());
    String title = request.getParameter("title"); 
    String cont = request.getParameter("contents"); 
    cont = cont.replace("\r\n","<br>");

    pstmt.setString(1, time);
    pstmt.setString(2, title);
    pstmt.setString(3, cont);

    pstmt.execute();
    pstmt.close();

    conn.close();
} catch(SQLException e) {
out.println( e.toString() );
} 
%>
  <script language=javascript>
   self.window.alert("Saved");
   location.href="index.jsp"; 
   </script>

这是编写代码,使用 INSERT INTO。 (没有错误)

因为这个:

error(java.sql.SQLException: 参数索引超出范围 (1 > number 参数个数,即 0))

我根本无法使用纠正句子的能力。

我想知道为什么会出现这个错误,并在UPDATE中解决这个问题。

【问题讨论】:

  • 在 JSP 页面中使用硬编码的连接参数创建物理连接是一个真的,非常糟糕的主意。而且您不应该使用 root 连接到数据库
  • sql = "UPDATE diary SET TITLE='" + title+ "' ,CONTENTS='"+ contents +"' WHERE NUM=" + idx; 应该是 sql = "UPDATE diary SET TITLE=? ,CONTENTS=? WHERE NUM=?" - 无论如何,如上所述,这样的代码确实 NOT 属于 JSP。学习和使用Model-View-Controller 模式。
  • 我不知道我是否会专业地做这个,但我这样做是参考别人的写作,所以我不知道我是否可以连接到根或任何东西。是否可以从数据库中检索它,将其添加到数据库中并对其进行修改,而无需以 root 身份连接它?我不知道javascript或java。我连JSP,MYSQL都不知道。

标签: mysql sql jsp jdbc


【解决方案1】:

您在更新子句中使用了错误的语法。代码应该是这样的。

    String sql = "SELECT NUM FROM diary WHERE NUM=" + idx;
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.executeQuery();

    pstmt.close();
    sql = "UPDATE diary SET TITLE=? ,CONTENTS=? WHERE NUM=?"; 
    pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, title);
    pstmt.setString(2, contents);
    pstmt.setString(3, idx);
    pstmt.executeUpdate();

【讨论】:

  • 最好也参数化idx,而不是将其连接到查询字符串中。
  • idx的列是NUM,类型是INT但是String可以吗?
  • @Koreanraichu 如果 idx 是int,则使用setInt
猜你喜欢
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
相关资源
最近更新 更多