JDBC操作数据库(一) —— 更新操作(增、删、改)

在数据库中建立一个user_infor表,并插入数据
数据库的部分数据
使用JDBC对数据库进行更新操作普通的做法:

public static void main(String[] args) {
		Connection connection = null;
		Statement statement = null;
		try {
//			1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
//			2.连接数据库
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","root");
//			3.创建Statement对象
			statement = connection.createStatement();
//			4.书写SQL语句
			String sql = "delete from user_infor where user_name = 1 ";
//			5.执行SQL语句
			int i = statement.executeUpdate(sql);
//			删除操作影响的行数
			System.out.println(i);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
//			6.释放资源(注意顺序)
			try {
				if (statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

结果

1	//删除操作影响了一行数据

由于通过JDBC对数据进行,插入,删除,更改,都是用的Statement的executeUpdate方法,其他代码相同,且只有SQL语句不同,如果每次进行更新操作,都要重新写一遍代码则代码复用性太差。
所以这里我们可以把数据库执行更新的操作抽取到一个update方法中,
该方法方法传入SQL语句,返回值为boolean类型,表示该方法是否成功的更新(增删改)了数据库中的数据。

具体代码如下

//传入SQL语句,返回值为boolean类型,表示更新是否成功
public static boolean update(String sql) {
		Connection connection = null;
		Statement statement= null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.连接数据库
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
			//3.创建Statement对象
			statement= connection.createStatement();
			//4.执行SQL语句,并返回更新操作是否成功,成功返回true,否则返回false
			return statement.executeUpdate(sql)>0;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			//5.释放资源(注意顺序)
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (connection !=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

如果想要对数据库进行更新(增、删、改),只需要传入对应的SQL语句即可

  • 删除
//测试方法
public static void main(String[] args) {
		//书写SQL语句
		String sql = "delete from user_infor where user_name = '1'";
		if(update(sql)) {
			//控制台打印Yes表示删除成功
			System.out.println("Yes");
		}else {
			System.out.println("NO");
		}
	}

结果

Yes

查看数据库user_name=1的数据已经被删除
成功删除user_name=1的数据

  • 插入(增加数据)
    和删除方法类似
	public static void main(String[] args) {
		String sql = "insert into user_infor (id,user_name,password) values ('11111','00000','123456')";
		if(update(sql)) {
			System.out.println("Yes");
		}else {
			System.out.println("NO");
		}
	}

结果

Yes

成功向数据库插入一条数据

  • 更改操作
public static void main(String[] args) {
		String sql = "update user_infor set id='aaa',user_name='bbb',password='ccc' where user_name='0'";
		if(update(sql)) {
			System.out.println("Yes");
		}else {
			System.out.println("NO");
		}

结果

Yes

数据库中
更改数据库中数据成功

总结,数据库更新(增、删、改)操作,对于每种操作由于只有SQL语句不同,所以可以抽取出一个方法,这样增加了代码的复用性,不用每次都重新把所以的步骤写一遍。

猜你喜欢

转载自blog.csdn.net/qq_35302939/article/details/89842560
今日推荐