JDBC实现增删改查(附代码详解)

本文详细介绍了如何使用Java JDBC实现对宠物信息的增删改查操作,包括加载数据库驱动,添加新宠物,删除宠物记录,更新宠物信息以及查询所有宠物的步骤。

JDBC实现宠物信息的增效删改查

主方法:

public class Demo {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
		Driver();
        Pet pet = new Pet(55,"小黑","公");
        //添加信息
        add(pet);
        //删除信息
        delet();
        //修改信息
        updata();
        //普通查询
        query();
 
	}

}

宠物类:

public class Pet {
		
	private int id;
	private String name;
	private String sex;
	public Pet(int id, String name, String sex) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
	}
	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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

加载驱动:

public static void Driver() throws SQLException {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
		String root = "root";
		String password = "password";
		Connection connection = DriverManager.getConnection(url,"root","password");
		Statement statement = connection.createStatement();

	}

添加信息:

private static void add(Pet pet) throws SQLException {
		// TODO Auto-generated method stub
		//定义SQL语句
		String sql="insert into pet  values("+pet.getId()+",'"
				+pet.getName()+"','"+pet.getSex()+"')";
		System.out.println(sql);
		String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
		String root = "root";
		String password = "password";
		Connection connection = DriverManager.getConnection(url,"root","password");
		Statement statement = connection.createStatement();
		int rows = statement.executeUpdate(sql);

		if(rows>0) {
			System.out.println("----操作成功----");
		}else {
			System.out.println("----操作失败----");
		}

	}

删除信息:

private static void delet() throws SQLException {
		// TODO Auto-generated method stub
		String sql="delete  from pet where id = 1";
		System.out.println(sql);
		String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
		String root = "root";
		String password = "password";
		Connection connection = DriverManager.getConnection(url,"root","password");
		Statement statement = connection.createStatement();

		int rows = statement.executeUpdate(sql);

		if(rows>0) {
			System.out.println("----操作成功----");
		}else {
			System.out.println("----操作失败----");
		}
	}

修改信息:

private static void delet() throws SQLException {
		// TODO Auto-generated method stub
		String sql="delete  from pet where id = 1";
		System.out.println(sql);
		String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
		String root = "root";
		String password = "password";
		Connection connection = DriverManager.getConnection(url,"root","password");
		Statement statement = connection.createStatement();

		int rows = statement.executeUpdate(sql);

		if(rows>0) {
			System.out.println("----操作成功----");
		}else {
			System.out.println("----操作失败----");
		}
	}

查询信息:

private static void query() throws SQLException {
		// TODO Auto-generated method stub
		String sql="select *from pet ";
		System.out.println(sql);
		String url = "jdbc:mysql://localhost:3306/dep ?serverTimezone=Asia/Shanghai";
		String root = "root";
		String password = "password";
		Connection connection = DriverManager.getConnection(url,"root","password");
		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery(sql);
		while(result.next())
		{
			int id = result.getInt("id");
			String name = result.getString("name");
			String sex = result.getString("sex");
			System.out.println(id+"\t"+name+"\t"+sex+"\t");
		}

	}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值