正在尝试使用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>");
}
}
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,...
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。