jsp与sql语句的混合使用示例
程序员文章站
2023-11-13 19:01:22
话不多说,直接上例子 insert 复制代码 代码如下: string sql="insert into allorder values('" + orderid + "'...
话不多说,直接上例子
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>
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>