jdbc mysql update语句_MYSQL 之 JDBC(三): 增删改查(一)通过Statement执行更新操作...

Statement测试

3261cce84d531bb17b20112813d07e17.gif

/**

* 通过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();

}

}

}

3261cce84d531bb17b20112813d07e17.gif

insert/update/delete封装

3261cce84d531bb17b20112813d07e17.gif

/**

* 通用的更新的方法: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();

}

}

}

3261cce84d531bb17b20112813d07e17.gif

创建JDBC的工具类,封装方法

工具类

3261cce84d531bb17b20112813d07e17.gif

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

}

}

3261cce84d531bb17b20112813d07e17.gif

修改后的insert/update/delete封装

3261cce84d531bb17b20112813d07e17.gif

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值