user
package dbc;
public class user {
private int id;
private int pwd;
private String name;
private double weight;
private String tel;
public user() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPwd() {
return pwd;
}
public void setPwd(int pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
jdbc
package dbc;
import java.sql.*;public class jdbc {
Connection conn=null;
public Connection getConnection() throws ClassNotFoundException, SQLException {
String user="root";
String drivername="com.mysql.jdbc.Driver";
String usepwd="test";
String dbname="sys";
String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+user+"&password="+usepwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
try{conn=DriverManager.getConnection(url);
return conn;}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
web3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ page import="dbc.user" import="java.util.ArrayList" import="java.util.List"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="web3-1.jsp"method="post">
<div align="center">
<br><br><br><br><br><br><br>
请输入sql语句:<input type="text" name="sql"><br><br>
<input type="submit" value="提交">
</div>
</form><br><br>
<table border="1"align="center" valign="center">
<% List<user>list=(ArrayList<user>)request.getAttribute("list");
if(list!=null){%>
<tr><td>账号</td><td>密码</td><td>姓名</td><td>体重</td><td>电话</td></tr>
<% user o=new user();
for(int i=0;i<list.size();i++){
o=(user)list.get(i);%>
<tr>
<td><%=o.getId() %></td>
<td><%=o.getPwd() %></td>
<td><%=o.getName() %></td>
<td><%=o.getWeight() %></td>
<td><%=o.getTel() %></td>
</tr>
<% }}%>
</table>
</body>
</html>
web3-1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" import="dbc.user" import="java.util.List" import="java.util.ArrayList"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br><br><br><br><br><br>
<%
String drivername="com.mysql.jdbc.Driver";
String username="root";
String userpwd="test";
String dbname="sys";String url1="jdbc:mysql://localhost:3306/"+dbname;
String url2="?user="+username+"&password="+userpwd;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName(drivername);
Connection conn=DriverManager.getConnection(url);
response.setContentType("text/html;charset=UTF-8");
String sql=request.getParameter("sql");
String sq=sql;
PreparedStatement pstmt=conn.prepareStatement(sql);%>
<% String part=sql.substring(0, 6);
if(part.equals("select")){
ResultSet rs=pstmt.executeQuery();
if(!rs.next()){
out.print("sql语句输入错误!");
}
else{
List<user>list=new ArrayList<user>();
rs.beforeFirst();
while(rs.next()){
user u=new user();
u.setId(rs.getInt(1));
u.setPwd(rs.getInt(2));
u.setName(rs.getString(3));
u.setWeight(rs.getDouble(4));
u.setTel(rs.getString(5));
list.add(u);
}
request.setAttribute("list", list);
/* user u=new user();
for(int i=0;i<list.size();i++){
u=(user)list.get(i);
out.print(u.getId());
}*/
%>
<jsp:forward page="web3.jsp"></jsp:forward>
</div>
<%
}
}
else if(part.equals("insert")){
String sql3="select id from java";
PreparedStatement pstmt3=conn.prepareStatement(sql3);
ResultSet rs3=pstmt3.executeQuery();
String b[]=sql.split(" ");
String c[]=b[3].split(",");
String d=c[0].substring(7);
int d1=Integer.parseInt(d);
int flag=0;
while(rs3.next()){
int s=rs3.getInt("id");
if(d1==s){
out.print("用户已被注册,请重新输入!");
flag=1;break;
}
}
if(flag==0)
{
int n=pstmt.executeUpdate();
if(n==0){
out.print("sql语句输入错误!");
}
else{
out.print("插入成功,操作记录:1条");
}
}
}
else if(part.equals("delete")){
String sql1="select * "+sql.substring(7);
PreparedStatement pstmt1=conn.prepareStatement(sql1);
ResultSet rs1=pstmt1.executeQuery();
rs1.last();
if(rs1.getRow()==0){
out.print("sql语句输入错误!");
}
else{
out.print("删除成功,操作记录:"+rs1.getRow()+"条");
int n=pstmt.executeUpdate();
}
}
else{
String p[]=sql.split(" ");
String sql2="select * from java where "+p[5];
PreparedStatement pstmt2=conn.prepareStatement(sql2);
ResultSet rs2=pstmt2.executeQuery();
rs2.last();
if(rs2.getRow()==0){
out.print("sql语句输入错误!");
}
else{
out.print("更新成功,操作记录:"+rs2.getRow()+"条");
int n=pstmt.executeUpdate();
}
}
%>
</body>
</html>
ps:插入操作目前不能插入汉字,否则会报错。不知道为什么上课问老师。