import java.util.*;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
StudentDao dao=new StudentDao();
int result=dao.delStudent(1);
if (result==1){
System.out.println("删除成功");
}
Student stu1=new Student(3,"suyu",32);
int result1=dao.updateStudent(stu1);
if (result1==1){
System.out.println("修改成功");
}
List<Student> stuList=dao.queryAll();
for (Student stu:stuList){
System.out.println(stu);
}
}
}
public class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
Connection con;
PreparedStatement pst;
public Connection getConn() throws ClassNotFoundException, SQLException
{
// 1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 2 获得数据库的连接
Connection con =
DriverManager.getConnection
("jdbc:mysql://localhost:3306/suyu?useUnicode=true&characterEncoding=utf-8", "root", "");
return con;
}
//增加
public int addStudent(Student student) throws ClassNotFoundException, SQLException
{
con=getConn();
String insertSQL="insert into student values(null,?,?)";
pst = con.prepareStatement(insertSQL);
pst.setString(1,student.getName());
pst.setInt(2, student.getAge());
int result=pst.executeUpdate();
pst.close();
con.close();
return result;
}
//删除
public int delStudent(int id) throws ClassNotFoundException, SQLException
{
con=getConn();
String sql="delete from student where id=?";
pst = con.prepareStatement(sql);
pst.setInt(1,id);
int result=pst.executeUpdate();
pst.close();
con.close();
return result;
}
//修改
public int updateStudent(Student stu) throws ClassNotFoundException, SQLException
{
con=getConn();
String sql="update student set name=?,age=? where id=?";
pst=con.prepareStatement(sql);
pst.setString(1,stu.getName());
pst.setInt(2,stu.getAge());
pst.setInt(3,stu.getId());
int result=pst.executeUpdate();
pst.close();
con.close();
return result;
}
private Object getName() {
// TODO Auto-generated method stub
return null;
}
//查询所有记录
public List<Student> queryAll() throws ClassNotFoundException, SQLException
{
con=getConn();
String sql="select * from student";
pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
List<Student> stuList=new ArrayList<Student>();
while(rs.next()){
Student stu=new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setAge(rs.getInt(3));
stuList.add(stu);
}
return stuList;
}
//根据主键查询记录
public Student queryById(int id) throws ClassNotFoundException, SQLException
{
con=getConn();
String sql="select * from student where id=?";
pst=con.prepareStatement(sql);
pst.setInt(1,id);
ResultSet rs=pst.executeQuery();
Student stu=null;
if (rs.next()){
stu=new Student(rs.getInt(1),rs.getString(2),rs.getInt(3));
}
pst.close();
con.close();
return stu;
}
}
JAVA
最新推荐文章于 2022-06-21 19:13:44 发布