1. Statement:用于执行SQL语句的对象
1)通过Connection的createStatement()方法来获取
2)通过executeUpdate(sql)可以执行SQL语句
3)传入的SQL可以是INSERT, UPDATE或者DELET,但不能是SELECT.
2. Connection、Statement都是应用程序和数据库服务器的连接资源,使用后一定要关闭,当在其他语句执行过程中,万一发生错误报错,不往下执行关闭操作,所以需要在finally中关闭Connection和Statement对象,确保他们一定关闭
3.关闭的顺序:先关闭后获取,即先关闭Statement后关闭Connection
具体的增删改sql语句:
a. 插入
sql = "INSERT INTO customers(ID, NAME, EMAIL, BIRTH) VALUES ('2018','王小二', '163@erpang.com', '1999-04-24')";
b. 删除
sql = "DELETE FROM customers WhERE ID = '2018'";
c. 更新
sql = "UPDATE customers SET EMAIL = '163@二胖.com' WHERE ID = '2017'";
截取成功插入的记录:
完整的代码:
package com.atguigu.jdbc;
import java.beans.Statement;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
public class JDBCTest {
/*
* 通过JDBC向指定的数据表中增删改记录
* 1. Statement:用于执行SQL语句的对象
* 1)通过Connection的createStatement()方法来获取
* 2)通过executeUpdate(sql)可以执行SQL语句
* 3)传入的SQL可以是INSERT, UPDATE或者DELET,但不能是SELECT.
*
* 2. Connection、Statement都是应用程序和数据库服务器的连接资源,使用后一定要关闭
* 需要在finally中关闭Connection和Statement对象
*
* 3.关闭的顺序:先关闭后获取,即先关闭Statement后关闭Connection
* @throws Exception
*/
@Test
public void testStatement() throws ClassNotFoundException, IOException, SQLException {
//1. 获取数据库连接
Connection conn = null;
java.sql.Statement statement = null;
try {
conn = getConnection2();
String sql = null;
//2. 准备插入的SQL语句语句
//a. 插入
// sql = "INSERT INTO customers(ID, NAME, EMAIL, BIRTH) "
// + "VALUES ('2018','王小二', '163@erpang.com', '1999-04-24')";
//b. 删除
// sql = "DELETE FROM customers WhERE ID = '2018'";
//c. 更新
sql = "UPDATE customers SET EMAIL = '163@二胖.com' WHERE ID = '2017'";
System.out.println(sql);
//3. 执行插入
//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 {
//4. 关闭Statement对象
try {
if(statement != null)
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//5. 关闭连接
if(statement != null)
conn.close();
}
}
}
}
但是介于每次执行更行操作时候都要执行连接数据库和关闭Statement和Connection操作,所以我对上面进行了优化,手写了JDBCToos工具类,可以在原来的class直接调用方法
conn = JDBCTools.getConnection();
JDBCTools.release((Statement) statement, conn);
下面是完整代码:
package com.atguigu.jdbc;
import java.beans.Statement;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
public class JDBCTest {
/*
* 通过JDBC向指定的数据表中增删改记录
* 1. Statement:用于执行SQL语句的对象
* 1)通过Connection的createStatement()方法来获取
* 2)通过executeUpdate(sql)可以执行SQL语句
* 3)传入的SQL可以是INSERT, UPDATE或者DELET,但不能是SELECT.
*
* 2. Connection、Statement都是应用程序和数据库服务器的连接资源,使用后一定要关闭
* 需要在finally中关闭Connection和Statement对象
*
* 3.关闭的顺序:先关闭后获取,即先关闭Statement后关闭Connection
* @throws Exception
*/
@Test
public void testStatement() throws ClassNotFoundException, IOException, SQLException {
//1. 获取数据库连接
Connection conn = null;
java.sql.Statement statement = null;
try {
conn = getConnection2();
String sql = null;
//2. 准备插入的SQL语句语句
//a. 插入
// sql = "INSERT INTO customers(ID, NAME, EMAIL, BIRTH) "
// + "VALUES ('2018','王小二', '163@erpang.com', '1999-04-24')";
//b. 删除
// sql = "DELETE FROM customers WhERE ID = '2018'";
//c. 更新
sql = "UPDATE customers SET EMAIL = '163@二胖.com' WHERE ID = '2017'";
System.out.println(sql);
//3. 执行插入
//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 {
//4. 关闭Statement对象
try {
if(statement != null)
statement.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//5. 关闭连接
if(statement != null)
conn.close();
}
}
}
/*
* 通用的更新方法:包括INSERT、UPDATE、DELECT
* 版本1.
*/
public void update(String sql) throws ClassNotFoundException, IOException, SQLException {
Connection conn = null;
java.sql.Statement statement = null;
try {
conn = JDBCTools.getConnection();
statement = conn.createStatement();
statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.release((Statement) statement, conn);
}
}
}
JDBCTools工具类:
package com.atguigu.jdbc;
import java.beans.Statement;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
/*
* 手写操作JDBC的工具类,封装了一些工具方法
* Version 1
*/
public class JDBCTools {
/*
* 1. 获取连接的方法
* 通过读取配置文件从数据库服务器获取一个连接
* @return
* @throws Exception
*/
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
//1.准备连接数据库的4个字符串
//1).创建Properties对象
Properties properties = new Properties();
//2).获取jdbc.properties对应的输入流
InputStream in = JDBCTools.class.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
//3).加载2)对应的输入流
properties.load(in);
//4)具体决定user,password等4个字符串
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String jdbcUrl = properties.getProperty("jdbcUrl");
String driver = properties.getProperty("driver");
//2.加载数据库驱动程序(对应Driver 实现类中有注册驱动的静态代码块)
Class.forName(driver);
//3.通过DriverManager的getConnection()方法获取数据库连接
return (Connection) DriverManager.getConnection(jdbcUrl, user, password);
}
/*
* 2. 关闭Statement和Connection
* @param statement
* @param conn
*/
public static void release(Statement statement, Connection conn) {
if(statement != null) {
try {
((Closeable) statement).close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行结果: