下午好。我尝试从 eclipse 的 java 代码连接到数据库。我需要发出请求并检查表单中输入的用户名和密码是否相互匹配。用户名及其密码列表位于名为 stud_test 的数据库中。我需要运行 gradle 和 tomcat 来检查 servlet 是否工作。当我这样做并打开所需的页面时,我看到了 PSQLExceptions。我的代码示例如下。我不明白有什么问题。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
Connection con;
ResultSet rs;
String URL = "jdbc:postgresql://localhost:5432/stud_test";
String username = request.getParameter("useruser");
String passwrd = request.getParameter("pass");
response.setContentType("text/html");
try {
con = DriverManager.getConnection(URL, "postgres", "postgres");
Statement st = con.createStatement();
st.executeQuery ("SELECT password FROM stud WHERE user = " + username);
rs = st.getResultSet();
if (passwrd.equals(rs)){
request.getServletContext().getRequestDispatcher(
"/jsp/hello.jsp").forward(request, response);
}
else {
request.getServletContext().getRequestDispatcher("/jsp/fail.jsp").forward(request, response);
}
rs.close ();
st.close ();
}
catch(Exception e) {
System.out.println("Exception is :" + e);
}
}