MYSQL 之 JDBC(三): 增删改查(一)通过Statement执行更新操作
2024-10-21 06:14:09
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);
}
}
————————————————
版权声明:本文为CSDN博主「李英俊小朋友」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21579045/article/details/105386353
最新文章
- IOC的理解
- 室内定位系列(二)——仿真获取RSS数据
- html5 选择元素
- Spring3.0 与 MyBatis框架 整合小实例
- ASP.NET网页生成EXCEL并下载(利用DataGrid或GridView等)
- C#_控件——CheckBox,TextBox,RequiredFieldValidator
- 利用开源项目使discus论坛与java应用同步登录和注册
- 绫致时装讲述O2O细节:野心在“私人定制” - 移动购物 - 亿邦动力网
- Unity SurfaceShader 开始编程
- lumen框架
- HTTP协议报文、工作原理
- 使用vue-cli脚手架搭建简单项目框架
- Java面试题—初级(2)
- JavaScript进阶(八)JS实现图片预览并导入服务器功能
- rest framework 认证 权限 频率
- mysql访问视图提示:找不到视图
- 【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】
- python删除数组元素导致跳过元素
- Leetcode 268.缺失数字 By Python
- pandas学习(创建数据,基本操作)