Java implements simple data migration from single database and single table to single database and single table

Posted by phpjaco on Tue, 24 Dec 2019 19:20:26 +0100

For simple requirements, I found a complete project github: data can be migrated through configuration
https://github.com/Lifedance/DataTransfer
Thank you very much.
But it's a pity that I can't use it in my project. It's estimated that the project is not perfect, or maybe it's for safety
My table can never be written.
But I can only write a simple to get some fields of the old table, and then insert them into the new table.
The code is as follows: in fact, it is a very simple jdbc operation database.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class DataCategory {

	public static void main(String[] args) {
		try {
			DataCategory.getSqlQuery();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Get replicated data
	 * 
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static void getSqlQuery() throws SQLException,
			ClassNotFoundException {
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1. Load driver
		Class.forName("com.mysql.jdbc.Driver");
		// 2. Get database link
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3. Operate the database through the connection of the database to realize the addition, deletion, modification and query (using the Statement class)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select gc_id as id,gc_name as name,jx as catCode , 
				gc_parent_id as parentId,gc_sort as deep from 33hao_goods_class");
		// 4. Process the returned results of the database (using the ResultSet class)
		while (rs.next()) {
			Integer valueOf = Integer.valueOf(rs.getString("deep"));
			Integer id = Integer.valueOf(rs.getString("id"));
			Integer parentId = Integer.valueOf(rs.getString("parentId"));
			DataCategory.insertIntoCategory(id, rs.getString("name"), parentId,
					rs.getString("catCode"), valueOf);
			System.out.println(id + " " + " " + parentId + valueOf + " "
					+ rs.getString("deep") + " " + rs.getString("name"));
		}
		// close resource
		rs.close();
		st.close();
		conn.close();
	}

	/**
	 * Insert the corresponding field value in the target database table
	 * 
	 * @param id
	 * @param name
	 * @param parentId
	 * @param catCode
	 * @param deep
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void insertIntoCategory(int id, String name, int parentId,
			String catCode, int deep) throws ClassNotFoundException,
			SQLException {
		String URL = "jdbc:mysql://localhost:3306/sb_01?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		String sql = "INSERT INTO merchandisecategory ( id, name, parentId, catCode, deep) 
									VALUES ( ?, ?, ?, ?, ?)";
		int executeUpdate = 0;
		// 1. Load driver
		Class.forName("com.mysql.jdbc.Driver");
		// 2. Get database link
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3. Operate the database through the connection of the database to realize the addition, deletion, modification and query (using the Statement class)
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.setString(2, name);
			pstmt.setInt(3, parentId);
			pstmt.setString(4, catCode);
			pstmt.setInt(5, deep);
			executeUpdate = pstmt.executeUpdate();
			pstmt.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// close resource
		conn.close();
	}
}

This allows you to insert data into the table.
The effect is as follows:

Topics: Database SQL Java JDBC