使用jsp與sql語句是jsp編程常見的方式,但是對于如何混合使用jsp與sql語句卻是有很多小伙伴們不是非常的清楚,那么下面錯新小編就為大家介紹混合使用jsp與sql語句的方法。
話不多說,直接上例子
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
?
復制代碼 代碼如下:
?
與delete,insert差不多
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="刪除" onclick="window.location.href='delete.jsp?orderid=<%=orderid %>'"></td></tr></table></td></tr>
<%
}
// 關閉連接
stmt.close();// 關閉命令對象連接
con.close();// 關閉數據庫連接
} catch (SQLException e) {
e.printStackTrace();
out.println("數據庫連接錯誤");
System.exit(0);
}
//數據庫連接完成
%>
</table>
看完后你知道如何混合使用jsp與sql語句了嗎??在學習jsp的內容后相信大家都會在實踐中不斷地成長并學會總結,感受每天的進步!