|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 2022-04-06 15:22  yanqi_vip  阅读(18)  评论(0)    收藏  举报

导航