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);
    
    }
  • 相关阅读:
    自制DEV皮肤
    网格系统,菜单、按钮及导航
    表单
    排版
    Bootstrap的HTML标准模板
    ecshop常用的修改内容
    删除ECSHOP后台左侧导航菜单
    dede 内容页文章标题显示不全的更改方法
    dede 鼠标移到标题处显示完整标题
    Artlist标签去掉table
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/15043711.html
Copyright © 2011-2022 走看看