1.Statement用于执行Sql语句的接口
1》.通过Connection 下的createStatement()方法获取对象
2》通过executeUpdate or executeQuary执行sql语句
3》传入的语句可以使 数据库数据的增删改查
2.Connection Statement 都是应用程序和数据库服务器的链接资源,使用后不要浪费资源,使用后一定要关闭。
3.关闭的顺序是先关闭Statement,再关闭connection
4.代码
public void update() {
/* 1. Statement: 用于执行 SQL 语句的对象
* 1). 通过Connection下的createStatement()方法来获取
* 2).通过excuteUpdate(sql)方法来执行sql语句
* 3). 传入的 SQL 可以是 INSRET, UPDATE 或 DELETE.SELECT
*
* 2. Connection、Statement 都是应用程序和数据库服务器的连接资源. 使用后一定要关闭.
* 需要在 finally 中关闭 Connection 和 Statement 对象.
*
* 3. 关闭的顺序是: 先关闭后获取的. 即先关闭 Statement 后关闭 Connection
*/
Connection con=null;
Statement statement=null;
try {
//1.获取数据库链接
try {
con=JdbcUtil.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.执行sql语句
//1.写好sql语句
String sql="UPDATE students\r\n" +
"SET NAME = '老公'\r\n" +
"WHERE id=1;";
//2.获取Statement对象
statement=(Statement)con.createStatement();
//3.调用exciteUpdate方法执行sql语句
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//3.关闭连接
JdbcUtil.release(con, statement);
}
}