连接查询删除

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
   
   
    <%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>

<form name=form1 method=post action="index.jsp">

<table>

<tr>
id:<input type=text name=id>
</tr>
<tr>
price:<input type=text name=price>
</tr>
<tr>
publisher:<input type=text name=publisher>
</tr>
<tr>
<input type=submit name=submit value=确定>
</tr>

 

</table>

</form>

<form method=post action=index.jsp>
<table>

<tr>
delete:<input type=text name=delete>
</tr>
<tr>
<input type=submit name=submit2 value=确定>
</tr>


</table>

</form>

<%


String userName="root";
String userPassword="523223";

String url="jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=gb2312";


 Class.forName("com.mysql.jdbc.Driver").newInstance();
 
 Connection conn=DriverManager.getConnection(url,userName,userPassword);

 
 String id=request.getParameter("id");
 String price=request.getParameter("price");
 String publisher=request.getParameter("publisher");
 String delete=request.getParameter("delete");
 
 try{
 
 Statement stmt=conn.createStatement();
  


 String sql="insert into book_info values(?,?,?)";
  
 
 
  PreparedStatement pstmt=conn.prepareStatement(sql);
  
  pstmt.setString(1,id);
  pstmt.setString(2,price);
  pstmt.setString(3,publisher);

  if(id!=null)
    pstmt.executeUpdate();
  
 
  
  if(delete!=null)
  {
   String sql3="delete from book_info where price=?";
   PreparedStatement pstmt3=conn.prepareStatement(sql3);
   
   pstmt3.setString(1,delete);
  
   pstmt3.executeUpdate();
  }
  
  
    String sql2="select * from book_info";
 
 ResultSet rs=stmt.executeQuery(sql2);
 
 
 
 
 
 out.print("<table border='1'>");
 
 
 
 
 while(rs.next())
 {
 %>
 
 <tr>
 <td><%=rs.getString(1) %></td>
 <td><%=rs.getString(2) %></td>
 <td><%=rs.getString(3) %></td>
 </tr>
 
 
 <%
 }
 
 out.print("</table>");
 

 
 
 
 
 }catch(Exception e)
 {
  out.print(e.toString());
 }
 
 
 
 
 
 

 

%>

 

</body>
</html>

转载于:https://www.cnblogs.com/lengye/archive/2011/10/06/2200203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值