gpt4 book ai didi

java.sql.SQLException : Parameter index out of range (1 > number of parameters, 即 0)。为什么会发生这种异常以及如何解决

转载 作者:行者123 更新时间:2023-12-01 11:55:27 25 4
gpt4 key购买 nike

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author hp
*/
@WebServlet(name = "Getdetails", urlPatterns = {"/Getdetails"})
public class Getdetails extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
Connection con = null;
PreparedStatement ps = null;
Statement st = null;
ResultSet rs = null;
String debitcard = new String();
String debitcardno = new String();
String accountno = new String();
String account = new String();
String cvv = new String();
String pin = new String();

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bussinesssolutions?zeroDateTimeBehavior=convertToNull", "root", "root");
ps = con.prepareStatement("select card.debitcardno, card.accountnno, card.cvv, card.pin from card inner join regsiter on card.edebitcardno = regsiter.debitcardno");
ps.setString(1, debitcard);
ps.setString(2, account);
ps.setString(3, cvv);
ps.setString(4, pin);
rs = ps.executeQuery();
debitcardno = rs.getString("debitcardno");
accountno = rs.getString("accountno");
cvv = rs.getString("cvv");
pin = rs.getString("pin");

request.getSession().setAttribute("debitcardno", debitcardno);
request.getSession().setAttribute("accountno", accountno);
request.getSession().setAttribute("cvv", cvv);
request.getSession().setAttribute("pin", pin);
RequestDispatcher rd = request.getRequestDispatcher("Getdetails");
rd.forward(request, response);
} catch (ClassNotFoundException | SQLException | ServletException | IOException e) {
out.print(e);
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(Bankdetails.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
ps.close();
st.close();
} catch (SQLException ex) {
Logger.getLogger(Bankdetails.class.getName()).log(Level.SEVERE, null, ex);
}
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(Bankdetails.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
`return` "Short description";
}// </editor-fold>

}

我收到上述异常。不知道为什么会出现这种异常以及如何解决。我想从上述详细信息的数据库中获取详细信息,但代码中可能存在一些错误,导致它没有发生

最佳答案

ps = con.prepareStatement("select card.debitcardno, card.accountnno, card.cvv, " + 
"card.pin from card inner join regsiter on card.edebitcardno = regsiter.debitcardno");

此语句中没有 ? 参数。以下任何一个都不起作用。

ps.setString(1, debitcard);
ps.setString(2, account);
ps.setString(3, cvv);
ps.setString(4, pin);

您需要实际提供要分配的参数。

ps = con.prepareStatement("select card.debitcardno, card.accountnno, card.cvv, " + 
"card.pin from card inner join regsiter on " +
"card.edebitcardno = regsiter.debitcardno " +
"where card.debitcardno=? AND card.accountno=? AND card.cvv=? AND card.pin=?");

关于java.sql.SQLException : Parameter index out of range (1 > number of parameters, 即 0)。为什么会发生这种异常以及如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com