- 创建数据库:在 MySQL 中创建 RUNOOB 数据库,创建website表,并插入内容
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');
说明:本次插入了3条内容
- 创建数据库:在 MySQL 中创建 RUNOOBTEST 数据库,并创建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;
说明:只是创建了一个空表
- Java 连接 MySQL 需要驱动包,下载地址为:http://dev.mysql.com/downloads/connector/j/,解压后得到jar库文件,然后在对应的项目中导入该库文件。
- 功能实现代码:
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!"); } }
说明:当输入完成后,发现不是按顺序插入的,因此将test数据库中的数据清空,加入两行代码,实现id指定开始自加
//清空id数据(不清空表本身) TRUNCATE TABLE tablename //设置id初始值为1 ALTER TABLE tablename AUTO INCREMENT=1