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

最新文章

  1. IOC的理解
  2. 室内定位系列(二)——仿真获取RSS数据
  3. html5 选择元素
  4. Spring3.0 与 MyBatis框架 整合小实例
  5. ASP.NET网页生成EXCEL并下载(利用DataGrid或GridView等)
  6. C#_控件——CheckBox,TextBox,RequiredFieldValidator
  7. 利用开源项目使discus论坛与java应用同步登录和注册
  8. 绫致时装讲述O2O细节:野心在“私人定制” - 移动购物 - 亿邦动力网
  9. Unity SurfaceShader 开始编程
  10. lumen框架
  11. HTTP协议报文、工作原理
  12. 使用vue-cli脚手架搭建简单项目框架
  13. Java面试题—初级(2)
  14. JavaScript进阶(八)JS实现图片预览并导入服务器功能
  15. rest framework 认证 权限 频率
  16. mysql访问视图提示:找不到视图
  17. 【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】
  18. python删除数组元素导致跳过元素
  19. Leetcode 268.缺失数字 By Python
  20. pandas学习(创建数据,基本操作)

热门文章

  1. Pyinstaller 打包python 到exe 在windows下免python环境运行python
  2. HBase 中加盐(Salting)之后的表如何读取:Spark 篇
  3. Windows服务监控工具Perfmon
  4. Android学习笔记触摸事件
  5. laravel查询常用的方式含义.
  6. Springboot项目整合Swagger2报错
  7. SpringBoot从入门到放弃之配置Spring-Data-JPA自动建表
  8. Python3-随笔目录
  9. 深入理解JVM(③)虚拟机的类加载过程
  10. HTML&CSS面试高频考点(二)