JSP+JDBC实现增删改查

需求:创建一张老师表,使用jdbc+jsp实现增删改查

1、创建数据库表

JSP+JDBC实现增删改查_第1张图片JSP+JDBC实现增删改查_第2张图片

2、创建一个web项目

JSP+JDBC实现增删改查_第3张图片

3、代码编写

实体类:

public class Teacher {


private Integer id;//
private String name;
private String age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
public Teacher(Integer id, String name, String age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Teacher(String name, String age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public Teacher(String name, String age) {
super();
this.name = name;
this.age = age;
}

Dao:

/**
* 查询全部

*/
public List find(){
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接信息
Connection con = DriverManager.getConnection("jdbc:mysql:///1604k", "root", "123456");
//执行sql
String sql="select * from t_teacher";
PreparedStatement ps = con.prepareStatement(sql);
//返回查询的结果集
ResultSet rs = ps.executeQuery();
List list=new ArrayList();
//遍历结果集
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String age = rs.getString("age");
String sex = rs.getString("sex");
//封装结果到对象实体
Teacher t=new Teacher(id, name, age, sex);
//将结果放入list集合
list.add(t);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 增加数据
*/
public int add(Teacher t){
int i=0;
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection con = DriverManager.getConnection("jdbc:mysql:///1604k", "root", "123456");
String sql="insert into t_teacher(name,age,sex) values(?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
//给数据复制
ps.setString(1, t.getName());
ps.setString(2, t.getAge());
ps.setString(3, t.getSex());
//执行sql
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
/**
* 删除一条数据
*/
public int delete(int id){
int i=0;
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接信息
Connection con = DriverManager.getConnection("jdbc:mysql:///1604k", "root", "123456");
//执行sql
String sql="DELETE from t_teacher where id=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, id);
i = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
/**
* 登录
*/
public int login(Teacher t){
int a=0;
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接信息
Connection con = DriverManager.getConnection("jdbc:mysql:///1604k", "root", "123456");
//执行sql
String sql="SELECT * from t_teacher where name=? and age=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, t.getName());
ps.setString(2, t.getAge());
ResultSet rs = ps.executeQuery();
//根据返回的值判断是否登录成功
if(rs.next()){
a=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return a;

}
/**
* 修改
*/
public int update(Teacher t){
int a=0;

try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接信息
Connection con = DriverManager.getConnection("jdbc:mysql:///1604k", "root", "123456");
//执行sql
String sql="UPDATE t_teacher set name=?,age=?,sex=? WHERE id=?";
PreparedStatement ps = con.prepareStatement(sql);
//赋值
ps.setString(1, t.getName());
ps.setString(2, t.getAge());
ps.setString(3, t.getSex());
ps.setInt(4, t.getId());
a = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();

return a;

}
JSP页面:

JSP+JDBC实现增删改查_第4张图片
JSP+JDBC实现增删改查_第5张图片
JSP+JDBC实现增删改查_第6张图片
JSP+JDBC实现增删改查_第7张图片
JSP+JDBC实现增删改查_第8张图片
JSP+JDBC实现增删改查_第9张图片
JSP+JDBC实现增删改查_第10张图片
JSP+JDBC实现增删改查_第11张图片
效果图:省略。。。

你可能感兴趣的:(JSP+JDBC实现增删改查)