zoukankan      html  css  js  c++  java
  • JDBC: DML操作

    DML操作

    1.  插入记录

    解决插入中文乱码问题

    jdbc:mysql://localhost:3306/db4?characterEncoding=UTF-8 
    characterEncoding=UTF-8 指定字符的编码、解码格式。
     

    代码示例(在TestDML.java中)

     /**
         * 插入数据
         * @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);
    }

    2.  更新记录

    根据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);
        }
     

    3.  删除记录

    删除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);
    
    }
  • 相关阅读:
    sqlserver 智能提示插件
    支付相关-算法知识2
    支付相关-算法知识1
    支付相关-证书知识学习4
    支付相关-证书知识学习3
    支付相关-证书知识学习2
    getID3类的学习使用
    ThinkPHP 的 Vender的简单实用
    postman的简单使用
    js 实现点击复制文本内容
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/15043711.html
Copyright © 2011-2022 走看看