JDBC 通过Statement 执行更新操作 2

	@Test
	public void testStatement() throws Exception {
		// 1. 获取数据库连接
		Connection conn = null;
		Statement statement = null;
		
		try {
			conn = getConnection2();
			// 3. 准备插入的 SQL 语句
			String sql = "insert into customers (name,email,birth) " + 
					"values('XYZ','1069941886@qq.com','1990-12-12');";
			
			// 4. 执行插入
			// 1)获取操作 SQL 语句的 Statement 对象。
			// 调用 Connection 的 createStatement() 方法来获取
			 statement = conn.createStatement();
			
			// 2)调用 Statement 对象的executeUpdate(sql) 执行 SQL 语句进行插入
			statement.executeUpdate(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				// 5. 关闭 Statement 对象
				if(statement != null) {
					statement.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {	
				// 2. 关闭连接
				if(conn != null) {
					conn.close();
				}
			}
			
		}
		
	}
posted @ 2019-07-07 01:45  烂科人  阅读(292)  评论(0)    收藏  举报