从Servlet发布到数据库会引发“列不存在错误”。我该如何解决并完成? -问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

小六码奴 2019-10-12 17:47:18 330

正在尝试使用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>");

}

}

SQL 关系型数据库 Java 数据库连接 数据库 PostgreSQL
分享到
取消 提交回答
全部回答(1)
  • 一码平川MACHEL
    2019-10-12 17:48:53

    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,...

    0 0
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题