JDBC_增删改查

本文详细介绍了一个使用Java实现的学生信息管理系统,包括学生类(Student)的定义,以及如何通过StuDao类进行数据库的增删改查操作。系统连接SQLServer数据库,演示了如何在Java中执行SQL语句,如插入(insert)、删除(delete)、更新(update)和查询(query)学生信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一共三个类Student.java、StuDao.java、test.java

Student.java

public class Student {
	private int id;
	private String name;
	private int age;
	
	public Student() {}
	public Student(int id,String name,int age) {
		this.id = id;
		this.name = name;
		this.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;
	}
	
}

增删改查写在StuDao.java里

连接SQLServer数据库

	Connection con;
	StuDao() throws SQLException{
		String name = "账户";
		String pw="密码";
		con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=数据库名",name,pw);
	}

public void insert(Student s){
		PreparedStatement pstmt = null;
		if (con != null) {
			try {
					//t_student为数据库中的表名
					pstmt = con.prepareStatement("insert into t_student values (?,?,?)");
					pstmt.setInt(1,s.getId());
					pstmt.setString(2,s.getName());
					pstmt.setInt(3,s.getAge());
					int i = pstmt.executeUpdate();
					if(i!=0) System.out.println("插入成功");
					else System.out.println("插入失败");
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				//释放资源
				try {			
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}	
	}

	public void delete() {
		int ID = 0;
		Scanner sc = new Scanner(System.in);
		PreparedStatement pstmt = null;
		if (con != null) {
			try {
				System.out.print("想要删除的学生ID:");
				ID = sc.nextInt();
				pstmt = con.prepareStatement("delete from t_student where ID=?");
				pstmt.setInt(1, ID);				
				int i=pstmt.executeUpdate();
				if(i!=0) System.out.println("删除成功");
				else System.out.println("删除失败");
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				//释放资源
				try {
				
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		this.query();
	}

public void update(Student s){
		PreparedStatement pstmt = null;
		if (con != null) {
			try {
				pstmt = con.prepareStatement("update t_student set NAME=?,age=? where id=?");
				pstmt.setInt(3, s.getId());
				pstmt.setString(1, s.getName());
				pstmt.setInt(2, s.getAge());
				int i=pstmt.executeUpdate();
				if(i!=0) System.out.println("修改成功");
				else System.out.println("修改失败");				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				//释放资源
				try {
				
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}	
	}

public void query() {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if (con != null) {
			try {
				pstmt = con.prepareStatement("select * from t_student");
				rs = pstmt.executeQuery();
				while (rs.next()) {
					System.out.println(rs.getInt("ID")+"\t"+rs.getString("name")+
							"\t"+rs.getInt("age"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 释放资源
				try {
					if (rs != null) {
						rs.close();
						rs = null;
					}
					if (pstmt != null) {
						pstmt.close();
						pstmt = null;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

关闭连接

public void close() {
		try {
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}	

test.java

public class Test {

	public static void main(String[] args) {
		StuDao sd = new StuDao();
//		//插入
//		sd.insert(new Student(1,"李四",30));
//		//删
//		sd.delete();
//		//改
//		sd.update(new Student(1,"TOM",20));
		//查
		sd.query();
		sd.close();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值