|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

 

 

posted on   yanqi_vip  阅读(11)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示