• Java数据库增删改查


    数据库为MySQL数据库,Oracle数据库类似:

    表结构参照

    插入数据

    package com.jef.sql;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    /**
     * //演示如何使用com.mysql.jdbc连接桥连接Mysql,插入内容,共三种方式:静态的两种executeUpdate(),executeBatch(),动态的PrepareStatement,
     * 实时动态时可以采用PrepareStatement()和executeBatch()的结合,删除、修改都有这三种方法
     */
    public class JavaCtMysqlInsert {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                ct.setAutoCommit(false);
                PreparedStatement ps = ct.prepareStatement("insert into user values(?, ?)");
                ps.setString(1, "ranye");
                ps.setString(2, "100");
                ps.executeUpdate();
                ps.clearParameters();
                ct.commit();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package com.jef.sql;
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    public class JavaCtMysqlInsertTwo {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                ct.setAutoCommit(false);
                Statement sm = ct.createStatement();
                sm.addBatch("insert into user values('dage', 100)");
                sm.addBatch("insert into user values('haonan', 100)");
                sm.executeBatch();
                ct.commit();
                sm.close();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package com.jef.sql;
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    public class JavaCtMysqlInsertThree {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                ct.setAutoCommit(false);
                Statement sm = ct.createStatement();
                sm.executeUpdate("insert into user values('yuanyuan', '100')");
                ct.commit();
                sm.close();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    删除数据

    package com.jef.sql;
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    public class JavaCtMysqlDelete {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                Statement sm = ct.createStatement();
                sm.executeUpdate("delete from user where name='tufujie'");
                sm.close();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package com.jef.sql;
    
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class JavaCtMysqlDeleteTwo {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                Statement sm = ct.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = sm.executeQuery("select * from user where name='dage'");// 从查询到的内容中进行修改、插入和删除
                rs.last();
                rs.deleteRow();
                rs.close();
                sm.close();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    修改数据

    package com.jef.sql;
    
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    public class JavaCtMysqlUpdate {
        public static void main(String[] args) {
            try {
                Connection ct = ConnectionMySQL.getMySQLConnection();
                Statement sm = ct.createStatement();
                sm.executeUpdate("update user set name='ran' where name='ranye'");
                sm.close();
                ct.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    查询数据

    package com.jef.sql;
    
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    /**
     * 当前情况下至少需要4+2条数据存在,特殊场景需捕获或者抛出
     */
    public class JavaCtMysqlSelect {
        public static void main(String[] args) {
            try {
                Connection conn = ConnectionMySQL.getMySQLConnection();
                Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                ResultSet result = stmt.executeQuery("select * from user");
                System.out.println("从末尾数据开始输出");
                result.last();
                System.out.println(result.getString("name") + "	" + result.getString("password"));
                while (result.previous())
                    System.out.println(result.getString("name") + "	" + result.getString("password"));
                System.out.println("从起始数据开始输出");
                result.first();
                System.out.println(result.getString("name") + "	" + result.getString("password"));
                while(result.next())
                    System.out.println(result.getString("name") + "	" + result.getString("password"));
    
                // 指定第几笔数据
                System.out.println("指定第几笔数据,这里指定第4笔数据");
                result.absolute(4);
                System.out.println(result.getString("name") + "	" + result.getString("password"));
    
                // 从目前游标处指定游标下移数
                System.out.println("从目前游标处指定游标位置,这里向下移动2笔数据");
                result.relative(2);
                System.out.println(result.getString("name") + "	" + result.getString("password"));
                result.close();
                stmt.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    藏!Java编程技巧之单元测试用例编写流程 原创 常意 阿里技术 2021-05-12
    时间,遵循rfc3339标准格式 unix时间戳
    微软面试题: LeetCode 240. 搜索二维矩阵 II 出现次数:3
    微软面试题: LeetCode 69. x 的平方根 出现次数:3
    微软面试题: LeetCode 138. 复制带随机指针的链表 出现次数:3
    微软面试题: LeetCode 384. 打乱数组 出现次数:3
    微软面试题: LeetCode 207. 课程表 出现次数:3
    微软面试题: LeetCode 98. 验证二叉搜索树 出现次数:3
    CF1537E2 Erase and Extend (Hard Version) 题解
    洛谷 P4332 [SHOI2014]三叉神经树 题解
  • 原文地址:https://www.cnblogs.com/tufujie/p/5140737.html
Copyright © 2020-2023  润新知