我是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,第二个参数为2,...

07-27 13:33