开发者社区> 问答> 正文

从Servlet发布到数据库会引发“列不存在错误”。我该如何解决并完成?

正在尝试使用servlet将appointments 添加到数据库的表中。我能够获取并显示信息,但无法弄清楚如何在表中正确插入新记录。当前,该错误表明名为“ first”的列不存在。但是,当我使用“ query”变量提取信息时,它似乎起作用。

package com.hospital;

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.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" " + "VALUES('first','last', 'day')"; // String sql = "INSERT INTO public."Reservations"(\r\n" + // " "Last", "First", "Day")\r\n" + // " VALUES ('first', 'last', 'day');"; // String query = "SELECT * FROM "Reservations"";

    String sql = "INSERT INTO \"Reservations\"(\r\n" + 
            "   \"Last\", \"First\", \"Day\")\r\n" + 
            "   VALUES (?, ?, ?);";

    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();

        PreparedStatement preparedStatement = c.prepareStatement(sql);
        preparedStatement.setString(1, last);
        preparedStatement.setString(2, first);
        preparedStatement.setString(3, day);
        preparedStatement.execute();

// stmt.executeUpdate(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>");

}

}

展开
收起
小六码奴 2019-10-12 17:47:18 3273 0
1 条回答
写回答
取消 提交回答
  • 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,...

    2019-10-12 17:48:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载