📄 transferbooksdatatomysql.java
字号:
package table;import java.sql.*;public class TransferBooksDataToMySQL { public static void main(String[] args) throws Exception { //注册SQLServer的驱动程序 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //连接SQLServer的books数据库 Connection connSQLServer = DriverManager.getConnection ("jdbc:microsoft:sqlserver://bemyfriend:1433;DatabaseName=books;" + "User=sa;Password="); //定义MySQL数据库的连接地址 String url = "jdbc:mysql://localhost:3306/books?user=root;password="; //注册MySQL的驱动程序 Class.forName("org.gjt.mm.mysql.Driver"); //连接MySQL的books数据库 Connection connMySQL = DriverManager.getConnection(url, "", ""); //为SQLServer的books数据库创建Statement Statement stmtSQLServer = connSQLServer.createStatement(); //为MySQL的books数据库创建PreparedStatement PreparedStatement preStmtMySQL; ResultSet rsSQLServer; /** 交换bookCategory数据表的数据 */ rsSQLServer = stmtSQLServer.executeQuery("select * from bookCategory"); preStmtMySQL = connMySQL.prepareStatement("insert into bookCategory " + " values(?, ?, ?)"); while (rsSQLServer.next()) { //使用PreparedStatement添加新数据表的记录 for (int i = 1; i < 4; ++i) { preStmtMySQL.setString(i , rsSQLServer.getString(i)); } //向数据库提交记录 preStmtMySQL.executeUpdate(); } System.out.println("成功向MySQL的books数据库的bookCategory数据表写入数据."); /** 交换books数据表的数据 */ rsSQLServer = stmtSQLServer.executeQuery("select * from books"); preStmtMySQL = connMySQL.prepareStatement("insert into books values(?,?,?,?," + "?,?,?)"); while (rsSQLServer.next()) { //使用PreparedStatement添加新数据表的记录 for (int i = 1; i < 8; ++i) { preStmtMySQL.setString(i, rsSQLServer.getString(i)); } //向数据库提交记录 preStmtMySQL.executeUpdate(); } System.out.println("成功向MySQL的books数据库的books数据表写入数据."); //关闭数据库连接和SQL语句执行类 stmtSQLServer.close(); preStmtMySQL.close(); connSQLServer.close(); connMySQL.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -