JDBC 通过Statement 执行更新操作 1

	/*
	 *  通过 JDBC 向指定的数据表中插入一条记录。
	 *  
	 */
	@Test
	public void testStatement() throws Exception {
		// 1. 获取数据库连接
		Connection 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 statement = conn.createStatement();
		
		// 2)调用 Statement 对象的executeUpdate(sql) 执行 SQL 语句进行插入
		statement.executeUpdate(sql);
		
		// 5. 关闭 Statement 对象
		statement.close();
		
		// 2. 关闭连接
		conn.close();
		
	}
	
	
	
	@Test
	public void testGetConnection2() throws Exception {
		System.out.println(getConnection2());
	}

posted @ 2019-07-07 01:40  烂科人  阅读(521)  评论(0)    收藏  举报