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();
}
}
}
}
}