连接查询删除

為{幸葍}努か 提交于 2020-03-05 07:40:16

<%@ 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>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!