• [Java] JDBC 02 写得比较完美 TestJDBC/TestDML.java(这很关键,看完整性),还引申以后必备 log4j 很 nice


    import java.sql.*;
    
    public class TestJDBC {
    
        public static void main(String[] args) {
            ResultSet rs = null;
            Statement stmt = null;
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
    
                conn = DriverManager
                        .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
                stmt = conn.createStatement();
                rs = stmt.executeQuery("select * from dept");
                while (rs.next()) {
                    System.out.println(rs.getString("deptno"));
                    System.out.println(rs.getInt("deptno"));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace(); // log4j 啊!你赶紧过来吧,阿里需要你!。
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                        rs = null; // 置为空吧,垃圾回收器会回收的。
                    }
                    if (stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                    if (conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    向数据库中插入一条语句
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class TestDML {
    
        public static void main(String[] args) {
            Statement stmt = null;
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
    
                conn = DriverManager
                        .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
                stmt = conn.createStatement();
                String sql = "insert into dept2 values (98, 'GAME', 'BJ')";
                stmt.executeUpdate(sql);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                    if (conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    

    DML(Data Manipulation Language)数据操纵语言命令使用户能够查询数据库以及操作已有数据库中的数据。基本的数据操作分成两类四种:检索(查询)和更新(插入、删除、修改)。DML分成交互型DML和嵌入型DML两类。依据语言的级别,DML又可分成过程性DML和非过程性DML两种。如insert,delete,update,select(插入、删除、修改、检索)等都是DML.

    命令行传入参数,执行 DML

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class TestDML2 {
    
        public static void main(String[] args) {
            if(args.length != 3) {
                System.out.println("Parameter Error! Please Input Again!");
                System.exit(-1);
            }
            
            int deptno = 0;
            
            try {
                deptno = Integer.parseInt(args[0]);
            } catch (NumberFormatException e) {
                System.out.println("Parameter Error! Deptno should be Number Format!");
                System.exit(-1);
            }
            
            String dname = args[1];
            String loc = args[2];
            
            Statement stmt = null;
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
    
                conn = DriverManager
                        .getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
                stmt = conn.createStatement();
                String sql = "insert into dept2 values (" + deptno + ",'" + dname + "','" + loc + "')";
    System.out.println(sql);
                stmt.executeUpdate(sql);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                    if(conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    

  • 相关阅读:
    您认为在测试人员同开发人员的沟通过程中,如何提高沟通的效率和改善沟通的效果?维持测试人员同开发团队中其他成员良好的人际关系的关键是什么?
    redis和jedis的用法,区别
    Jedis实现多种功能总结
    Druid简单介绍
    Svn与Git的区别
    SVN的一些基本概念(学前了解)
    Redis-cli 的功能
    postman的使用方法
    Spring Boot 有哪些优点?
    Redis中的常用命令哪些?
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786874.html
Copyright © 2020-2023  润新知