Java JDBC入门之三: 通过 Statement执行更新操作

  Java JDBC入门之三: 通过 Statement执行更新操作


public class TestJDBC {
	
	
	/**
	 * 通过 JDBC 向指定的数据库插入数据
	 * 1. Statement:用于执行SQL 语句的对象
	 * 1). 通过Connection 的 createStatement()方法来获取
	 * 2). 通过executeUpdate(sql) 可以执行SQL语句。
	 * 3). 传入的 SQL 可以是 INSERT,UPDATE 或 DELETE, 但不能是SELECT
	 * 
	 * 2.Connection、Statement 都是应用程序和数据库服务器的连接资源,使用用必须关闭
	 * 需要在finally中关闭Connection、Statement的对象
	 * 
	 * 3. 关闭的顺序是: 先关闭后获取的,即先关闭Statement 后关闭 Connection
	 */
	@Test
	public void testStatement() throws Exception {
		//1.获取数据库连接
		Connection conn = null; 
		Statement statement = null;
		try {
			conn = getConnection2();
			//3. 准备插入的SQL 的语句
			String sql = null; 
//			sql = "INSERT INTO customers(NAME,email,birth) values('AA','AA','2019-01-31')";
//			sql = "DELETE FROM customers WHERE id = '1'";
			sql = "UPDATE customers SET name = 'Tom' WHERE id = '4'";
			//4. 执行插入
			//1). 获取操作SQL 语句的Statement 对象:调用Connection 的 createStatement()方法获取
			 statement = conn.createStatement();
			//2). 调用Statement 对象 executeUpdate(sql)执行SQL 语句进行插入
			statement.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				//5. 关闭Statement 对象
				if(statement != null) {
					statement.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				//2.关闭连接
				if(conn != null) {					
					conn.close();
				}
			}
		}
	
	}
	
	public Connection getConnection2() throws Exception{
		
		Properties properties = new Properties();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
		properties.load(in);
		String user = properties.getProperty("user");
		String driverClass = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String password = properties.getProperty("password");
		
		Class.forName(driverClass);
		
		return DriverManager.getConnection(url, user, password);
	}
}

封装成工具类,虽然这个类还有很多的不完善和不足,但其实是一种封装思想的体现。


/**
 * 操作JDBC 的工具类,其中封装了一些工具方法
 *version 1.0
 */
public class JDBCTools {
	/**
	 * 关闭Statement 和 Connection
	 * @param statement
	 * @param conn
	 */
	public static void release(Statement statement,Connection conn) {
		if(statement != null) {
			try {
				statement.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		if(conn != null) {
			try {
				conn.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}
	/**
	 * 1. 获取连接发方法
	 * 通过读取配置文件从数据库服务器获取一个连接.
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection2() throws Exception{
		
		Properties properties = new Properties();
		InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
		properties.load(in);
		String user = properties.getProperty("user");
		String driverClass = properties.getProperty("driver");
		String url = properties.getProperty("url");
		String password = properties.getProperty("password");
		
		Class.forName(driverClass);
		
		return DriverManager.getConnection(url, user, password);
	}
    

    /**
	 * 通用的更新的方法:包括INSERT,UPDATE,DELETE
	 * 版本1.
	 */
	public void update(String sql) {
		Connection conn = null;
		Statement statement = null;
		
		try {
			conn = getConnection2();
			statement = conn.createStatement();
			statement.executeUpdate(sql);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			if(conn != null) {
				try {
					conn.close();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			
		}
		
		
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值