/*
* Statemen:
* 1.用于执行SQL语句对象
* 1.1 通过Connection的createStatement()方法来获取
* 1.2 通过executeUpdate(sql)可以执行SQL语句
* 1.3 传入的SQL可以是INSERT UODATE DELETE 但不能是SELECT
*
* 2.Connection,Statemen得关闭。需要在finally中关闭。
*
* 3.关闭顺序。Statemen Connection
*
*/
@Test
public void testStatement() throws Exception{
Connection connection = null;
Statement statement = null;
try {
//1.获取数据库连接
connection = getConnection();
//3.准备插入的SQL
String sql = "INSERT INTO CUSTOMERSS VALUES(2,'22','222','1990-12-12')";
// 4.执行插入操作
//4.1 获取操作SQL语句的Statement对象:调用Connection的createStatement()获得
statement= connection.createStatement();
// 4.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{
if(connection!= null){
//2.关闭数据库连接
connection.close();
}
}
}
}