求助
servlet吧
全部回复
仅看楼主
level 1
Aginn😘 楼主
在servlet中执行sql语句update,程序没有报错,但为什么数据库没有更新呢?![不高兴]
2018年08月24日 13点08分 1
level 1
Aginn😘 楼主
也试过加commit了,不过报错说,已经有autocommit了
2018年08月24日 13点08分 2
level 1
Aginn😘 楼主
2018年08月24日 13点08分 3
level 1
Aginn😘 楼主
代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String indate=request.getParameter("checkindate");
String outdate=request.getParameter("checkoutdate" );
String floor=request.getParameter("floor" );
String number=request.getParameter("number" );
String sql="select * from room where floor='"+floor+"'and number='"+number+"'";
String sql2="update room set is_checkin ='1'and time_out ='"+outdate+"' and time_in ='"+indate+"' where floor='"+floor+"'and number='"+number+"'";
System.out.println("Connecting to database...");
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/room","root","root");
System.out.println("creating statement...");
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
System.out.println(rs);
System.out.println(rs.next());
rs.previous();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs.next())
{
stmt.executeUpdate(sql2);
System.out.println("success");
}
else
{
System.out.println("error");
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if(conn!=null)
conn.close();
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
response.setHeader("reflesh","0.1;url:/welcom.jsp");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
System.out.println("bye");
}
}
2018年08月24日 13点08分 4
1