gpt4 book ai didi

java - 从 servlet 发送到数据库会抛出 "column does not exist error"。我该如何解决并完成这个问题?

转载 作者:行者123 更新时间:2023-12-02 09:25:53 24 4
gpt4 key购买 nike

我是 java 新手,正在尝试使用 servlet 将约会添加到数据库中的表中。我能够获取信息并显示它,但我不知道如何正确地将新记录插入到我的表中。目前,错误表明名为“first”的列不存在。但是,当我使用“query”变量提取信息时,它似乎有效。

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AppointmentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public AppointmentServlet() {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
System.out.println("doPost method running");

String first = request.getParameter("firstname");
String last = request.getParameter("lastname");
String day = request.getParameter("day");
String sql = "INSERT INTO \"Reservations\"(\r\n" +
" \"Last\", \"First\", \"Day\")\r\n" +
" VALUES (first, last, day);";
// String query = "SELECT * FROM \"Reservations\"";


Connection c = null;
Statement stmt = null;

try {

Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/HospitalReservations", "postgres", "coffeeCup");
c.setAutoCommit(false);
System.out.println("Opened database connection successfully.");
System.out.println("C: " + c);

stmt = c.createStatement();
c.commit();


ResultSet rs = stmt.executeQuery(sql);
while ( rs.next() ) {
String firstname = rs.getString("first");
String lastname = rs.getString("last");
String daytime = rs.getString("day");

System.out.println("NAME = " + firstname + " " + lastname);
System.out.println("DAY = " + daytime);
}


} catch( Exception e ) {
System.err.print(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Operation done successfully.");


response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Appointment Response</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3> Reservation Made </h3>");
out.println("<p>" + first + " " + last + " has been added for " + day + "</p>");
out.println("</body>");
out.println("</html>");

}

}

最佳答案

需要传递参数时使用PreparedStatement。例如,在您的情况下:

    //use ? for parameter placeholder
String sql = "INSERT INTO \"Reservations\"(\r\n" +
" \"Last\", \"First\", \"Day\")\r\n" +
" VALUES (?, ?, ?);";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, last);
preparedStatement.setString(2, first);
preparedStatement.setString(3, day);
preparedStatement.execute();

注意:preparedStatement.setXXX方法的第一个参数称为parameterIndex,它从1开始。第一个参数是1,第二个参数是1是 2,...

关于java - 从 servlet 发送到数据库会抛出 "column does not exist error"。我该如何解决并完成这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58351090/

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