Mysql connection with JAVA, to achieve the specified field contents read from a database, from growing into another database

  • Create a database: Create RUNOOB MySQL database, create a website tables, and insert content
    CREATE TABLE `websites` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
      `url` varchar(255) NOT NULL DEFAULT '',
      `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
      `country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
    INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', '菜鸟教程', 'http://www.runoob.com', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');

    Description: The contents inserted 3

  • Create a database: Create RUNOOBTEST in MySQL database, and create table website_test
    CREATE TABLE `websites_test` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
    `url` varchar(255) NOT NULL DEFAULT '',
    `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
    `country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

    Description: just create an empty table

  •  Java needs to connect to the MySQL driver package, download address is: http://dev.mysql.com/downloads/connector/j/ , get unpacked jar library file, and then import the library file in the corresponding project.
  • Function implementation code:
    import java.sql.*;
    
    public class MySQLDemo {
        static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
        static final String DB_URL1 = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
        static final String DB_URL2 = "jdbc:mysql://localhost:3306/RUNOOBTEST?useSSL=false&serverTimezone=UTC";
    
    
        // 数据库的用户名与密码,需要根据自己的设置
        static final String USER = "root";
        static final String PASS = "asdA456-+!";
    
        public static void main(String[] args) {
            Connection conn1 = null;
            Statement stmt1 = null;
            Connection conn2 = null;
            Statement stmt2 = null;
            try{
                // 注册 JDBC 驱动
                Class.forName(JDBC_DRIVER);
    
                // 打开链接
                System.out.println("连接数据库1...");
                conn1 = DriverManager.getConnection(DB_URL1,USER,PASS);
    
                // 打开链接
                System.out.println("连接数据库2...");
                conn2 = DriverManager.getConnection(DB_URL2,USER,PASS);
    
                // 执行查询
                System.out.println(" 实例化Statement对象1...");
                stmt1 = conn1.createStatement();
                String sql1;
                sql1 = "SELECT id, name, url FROM websites";
                ResultSet rs1 = stmt1.executeQuery(sql1);
    
                // 展开结果集数据库
                while(rs1.next()){
                    // 通过字段检索
                    int id  = rs1.getInt("id");
                    String name = rs1.getString("name");
                    String url = rs1.getString("url");
    
                    // 输出数据
                    System.out.print("ID: " + id);
                    System.out.print(", 站点名称: " + name);
                    System.out.print(", 站点 URL: " + url);
                    System.out.print("\n");
                }
    
                //执行插入
                System.out.println("实例化Statement对象2...");
                stmt2 = conn2.createStatement();
                String sql2;
                sql2 =  "insert into RUNOOBTEST.websites_test(name) select name from RUNOOB.websites where id=3";
                stmt2.executeUpdate(sql2);
    
                // 完成后关闭
                rs1.close();
                stmt1.close();
                conn1.close();
    
                // 完成后关闭
                //rs2.close();
                stmt2.close();
                conn2.close();
    
            }catch(SQLException se){
                // 处理 JDBC 错误
                se.printStackTrace();
            }catch(Exception e){
                // 处理 Class.forName 错误
                e.printStackTrace();
            }finally{
                // 关闭资源
                try{
                    if(stmt1!=null) stmt1.close();
                    if(stmt2!=null) stmt2.close();
                }catch(SQLException se2){
                }// 什么都不做
                try{
                    if(conn1!=null) conn1.close();
                    if(conn2!=null) conn2.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }
            }
            System.out.println("Goodbye!");
        }
    }

    Description: When the input is completed, the insertion sequence is not found, the test data in the database so emptied, two lines added, from the start to achieve the specified id added

    //清空id数据(不清空表本身)
    TRUNCATE TABLE tablename
    
    //设置id初始值为1
    ALTER TABLE tablename AUTO INCREMENT=1
    

     

Published 111 original articles · won praise 57 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_38358499/article/details/100737514