对学⽣表的增删改查操作
⼤三暑假在实习,第⼆周已经结束了,现在可以⾃⼰动⼿写⼀些⽐较⼩的⼯程。就是写出来调试真的好费⼈。简单介绍⼀下,有⼀个简单的学⽣信息表,数据库设计如下:
然后,根据MVC进⾏分层处理:
程序运⾏结果:
1、查询数据库的结果。
2、点击新增
3、点击编辑,会获取到Id,进⾏对应的编辑
4、点击删除,直接删除。
public class Student {
int id;//学号
String name;//姓名
int age;//年龄
String classes;//班级
public Student(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {mysql面试题 增删改查
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {  this.classes = classes;
}
}
import java.util.List;
bean.Student;
public interface IStuDao {
/**
网页开发的新趋势* 新增⼀个学⽣
* @param stu
*/
public void addstu(Student stu);
/**
* 修改⼀个学⽣
* @param stu
*/
public void updatestu(Student stu);
/
**
* 通过ID删除⼀个学⽣
* @param id
*/
public void delstu(int id);
/**
* 到所有的学⽣
* @return 返回⼀个集合
*/
public List<Student> findall();
/**
* 通过id到⼀个学⽣
* @param id
* @return
*/
public Student findStubyId(int id);
}
Dao.impl;
import java.sql.Connection;
java算法视频教程import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Dao.IStuDao;
bean.Student;
db.JDBCUtils;
public class StuDaoImpl implements IStuDao {
public void addstu(Student stu) {
// TODO Auto-generated method stub
String s1 = "INSERT INTO student (id,name,age,class) VALUES(?,?,?,?)";        try {
Connection con = nection();
PreparedStatement prep = con.prepareStatement(s1);
数据库innerjoin
prep.setInt(1, Id());
prep.setString(2, Name());
prep.setInt(3, Age());
prep.setString(4, Classes());
prep.setString(4, Classes());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
public void updatestu(Student stu) {
String s1 = "update student set name=?,age=?,class=? where id=?";          try {
Connection con = nection();
PreparedStatement prep = con.prepareStatement(s1);
prep.setInt(4, Id()); //第4个问号的值
prep.setString(1, Name());
prep.setInt(2, Age());
prep.setString(3, Classes());
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
}
public void delstu(int id){
String s5 = "delete from student where id =?" ;
Connection con = null;
try {
con = nection();
PreparedStatement prep = con.prepareStatement(s5);
prep.setInt(1,id);
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
}
public List<Student> findall() {
String s2 = "select * from student";
Connection con = null;
List<Student> list = new ArrayList();
try {
con = nection();
PreparedStatement ps = con.prepareStatement(s2);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
list.add(toStu(rs));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
return list;
}
//将⼀⾏数据封装成⼀个对象
private Student toStu(ResultSet rs) throws SQLException {
复制链接到迅雷下载文件变成phpStudent s = new Student();
s.Int("id"));
s.String("name"));
s.Int("age"));
s.String("class"));服务器下载安装
return s;
}
public Student findStubyId(int id) {
String s3 = "select * from student where id =" + id;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Student> li = new ArrayList();
try {
con = nection();
ps = con.prepareStatement(s3);
rs = ps.executeQuery();
while (rs.next()) {
return toStu(rs);
}  } catch (SQLException e) { // TODO Auto-generated catch block        e.printStackTrace();
}finally{
}
return null;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。