JAVA


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;
 }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值