创建JAVABean
package com;
public class user {private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<form action="X.jsp" method="post">
姓名:<input type="text" id="name" name="name"><br>
性别:<input type="text" id="sex" name="sex"><br>
年龄:<input type="text" id="age" name="age"><br>
<input type="submit" value="提交">
<jsp:useBean id="user" class="com.user" scope="session"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<jsp:getProperty name="user" property="name"/>
<jsp:getProperty name="user" property="sex"/>
<jsp:getProperty name="user" property="age"/>
创建跳转表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="bean.User"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection= DriverManager.getConnection(url);
String sql="insert into user(username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
<jsp:getProperty name="user" property="username"/>
实现数据库的增删改查的操作
1.使用Statement增加数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="bean.User"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection= DriverManager.getConnection(url);
String sql="insert into user(username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
Statement stmt = connection.createStatement();
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("注册失败");
}else{
out.print("注册成功");
}
%>
<jsp:getProperty name="user" property="username"/>
使用PreparedStatement增加数据
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql = "insert into user(username,password,gender,age) values (?,?,?,?)";
PreparedStatement Ws = connection.prepareStatement(sql);
Ws.setString(1,request.getParameter("username"));
Ws.setString(2,request.getParameter("password"));
Ws.setInt(3,request.getParameter("gender"));
Ws.setInt(4,request.getParameter("age"));
ResultSet rs = Ws.executeQuery();
%>
2.使用Statement删除数据
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://10.3.22.15:3306/book?user=root&password=root";
Connection connection =DriverManager.getConnection(url);
Statement stmt =connection.createStatement();
String sql = "delete from user where id = 5";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("删除失败");
}else{
out.print("删除成功");
}
%>
使用PreparedStatement删除数据
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "delete from user where id = ? ";
PreparedStatement pstm = connection.prepareStatement(sql);
pstm.setInt(1,5);
%>
3.使用Statement修改数据
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="bean.User"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
Connection connection =DriverManager.getConnection(url);
Statement stmt =connection.createStatement();
String sql ="update user set age = 25 where id = 3";
int count = stmt.executeUpdate(sql);
if(count == 0){
out.print("修改失败");
}else{
out.print("修改成功");
}
%>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://127.0.0.1:3306/book";
Connection connection = DriverManager.getConnection(url,"book","book");
String sql="update user set age=? where age=?";
PreparedStatement pstm=connection.prepareStatement(sql);
pstm.setInt(25,3);
%>
4.使用Statement查询数据
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
Statement stmt = connection.createStatement();
String sql = "select * from user where username = '"+request.getParameter("username")+"'";
ResultSet rs = stmt.executeQuery(sql);*/
while (rs.next()) {
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
%>
使用PreparedStatement查询数据
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/book";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user where username = ?";
PreparedStatement Ws = connection.prepareStatement(sql);
Ws.setString(1,request.getParameter("username"));
ResultSet rs = Ws.executeQuery();
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
%>