Statement测试
/**
* 通过JDBC向指定的数据表中插入一条记录
* 1. Statement:用于执行sql语句的对象
* 1.1 通过Connection的createStatement()方法来获取
* 1.2 通过executeUpdate(sql)可以执行SQL语句
* 1.3 传入的sql可以是insert, update或delete,但不能是select
* 2. Connection、Statement都是应用程序和数据库服务器的连接资源。使用后一定要关闭。
* 2.1 需要再finally中关闭
* 3. 关闭顺序:先获取的后关,后获取的先关
*/
public void testStatement() {
Connection conn = null;
Statement statement = null;
try {
// 1. 获取数据库连接
conn = getConnection2();
// 2. 准备插入的SQL语句
String sql = "insert into t_user (username, pwd) values('测试', 3352)";
String sql2 = "update t_user set username='傻瓜' where id = 20017";
// 3. 执行插入
// 3.1 获取操作sql语句的Statement对象
statement = conn.createStatement();
// 3.2 调用Statement对象的executeUpdate(sql)执行SQL语句
statement.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
// 4. 关闭Statement对象
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
// 5. 关闭Connection对象
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
insert/update/delete封装
/**
* 通用的更新的方法: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 {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
创建JDBC的工具类,封装方法
工具类
package com.litian.jdbc;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
/**
* @author: Li Tian
* @contact: litian_cup@163.com
* @software: IntelliJ IDEA
* @file: JDBCUtils.java
* @time: 2020/3/21 15:23
* @desc: |操作JDBC的工具类,其中封装了一些工具方法
* Version1
*/
public class JDBCTools {
/**
* 关闭Statement和Connection的方法
*/
public static void release(Statement statement, Connection conn) {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void release(ResultSet rs, Statement statement, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 1. 获取连接的方法
* 通过读取配置文件从数据库服务器获取一个连接。
*
* @return
*/
public static Connection getConnection() throws Exception {
// 1. 准备连接数据库的4个字符串。
// 1.1 创建Properties对象
Properties properties = new Properties();
// 1.2 获取jdbc.properties对应的输入流
InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
// 1.3 加载1.2对应的输入流
properties.load(in);
// 1.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. 加载数据库驱动程序
Class.forName(driver);
// 3. 通过DriverManager的getConnection()方法获取数据库连接。
return DriverManager.getConnection(jdbcUrl, user, password);
}
}
修改后的insert/update/delete封装
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 {
JDBCTools.release(statement, conn);
}
}