Example of mixing jsp and sql statements

  • 2020-08-22 22:37:31
  • OfStack

Without further ado, let's go to the example
insert
 
String sql="insert into allorder values('" + orderid + "','" + bookid +"','" + amount + "','" + username + "')"; 
int result=stmt.executeUpdate(sql); 
if(result==1) 
response.sendRedirect("index.jsp"); 
else 
response.sendRedirect("detail.jsp"); 


delete
 
String condition="delete from allorder where id='"+ orderid+ "'"; 
int result=stmt.executeUpdate(condition); 

if(result==1) 
response.sendRedirect("index.jsp"); 
else 
out.println("shopCar.jsp"); 


update
 
 with delete . insert almost  

select 
[code] 
<% 
while (rs.next()){ 
String amount=rs.getString("amount"); 
String orderid=rs.getString("id"); 
String bookname=rs.getString("name"); 
String price=rs.getString("price"); 
String picture=rs.getString("pic"); 

//out.println("\tshuliang"+amount); 
%> 
<tr><td><%=orderid %></td><td><%=picture %></td><td><%=bookname %></td><td><%=price %></td><td><%=amount %></td><td><table><tr><td><input type="button" value=" delete " onclick="window.location.href='delete.jsp?orderid=<%=orderid %>'"></td></tr></table></td></tr> 
<% 
} 
//  Close the connection  
stmt.close();//  Close the command object connection  
con.close();//  Close the database connection  
} catch (SQLException e) { 
e.printStackTrace(); 
out.println(" Database connection error "); 
System.exit(0); 
} 
// Database connection complete  
%> 
</table>