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