|NO.Z.00084|——————————|BigDataEnd|——|Java&MySQL.JDBC.V09|——|MySQL.v09|Jdbc开发_使用JdbcUtils完成DML操作|
一、[使用JdbcUtils完成DML操作]:DML插入数据
### --- 插入记录
——> 解决插入中文乱码问题.
jdbc:mysql://localhost:3306/db4?characterEncoding=UTF-8
characterEncoding=UTF-8 指定字符的编码、解码格式。
二、代码示例
### --- 代码示例
/**
* 插入数据
* @throws SQLException
*/
@Test
public void testInsert() throws SQLException {
//1.通过工具类获取连接
Connection connection = JDBCUtils.getConnection();
//2.获取Statement
Statement statement = connection.createStatement();
//2.1 编写Sql
String sql = "insert into jdbc_user values(null,'张百万','123','2020/1/1')";
//2.2 执行Sql
int i = statement.executeUpdate(sql);
System.out.println(i);
//3.关闭流
JDBCUtils.close(connection,statement);
}
三、DML更新记录
### --- 根据ID 需改用户名称
/**
* 修改 id 为1 的用户名为 广坤
*/
@Test
public void testUpdate() throws SQLException {
Connection connection = JDBCUtils.getConnection();
Statement statement = connection.createStatement();
String sql = "update jdbc_user set username = '广坤' where id = 1";
statement.executeUpdate(sql);
JDBCUtils.close(connection,statement);
}
四、DML 删除记录
### --- 删除id为 3 和 4 的记录
/**
* 删除id 为 3 和 4的记录
* @throws SQLException
*/
@Test
public void testDelete() throws SQLException {
Connection connection = JDBCUtils.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate("delete from jdbc_user where id in(3,4)");
JDBCUtils.close(connection,statement);
}
五、sql语句
package com.yanqi.jdbc05;
import com.yanqi.jdbc05.JdbcUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDml {
/*
* 插入数据
* */
@Test
public void testInsert() throws SQLException {
//1.通过JDBCUtils工具类 获取连接
Connection con = JdbcUtils.getConnection();
//2.获取Statement对象
Statement statement = con.createStatement();
//2.1 编写SQL
String sql = "insert into jdbc_user values(null,'张百万','123','2020/11/11')";
//2.2 执行SQL
int i = statement.executeUpdate(sql);
System.out.println(i);
//3.关闭流
JdbcUtils.close(con,statement);
}
/*
* 更新操作 根据id修改用户名
* */
@Test
public void testUpdate() throws SQLException {
Connection connection = JdbcUtils.getConnection();
Statement statement = connection.createStatement();
String sql = "update jdbc_user set username = '刘能' where id = 1";
statement.executeUpdate(sql);
JdbcUtils.close(connection,statement);
}
/*
* 删除操作
* 删除 id为 1 和 2 的数据
*
* */
@Test
public void testDelete() throws SQLException {
Connection connection = JdbcUtils.getConnection();
Statement statement = connection.createStatement();
String sql = "delete from jdbc_user where id in(1,2)";
statement.executeUpdate(sql);
JdbcUtils.close(connection,statement);
}
}
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv005-mysql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通