1. 程式人生 > >Java實現簡單的資料遷移,從單庫單表到單庫單表(2)

Java實現簡單的資料遷移,從單庫單表到單庫單表(2)

這個文章是對我上一篇文章Java實現簡單的資料遷移,從單庫單表到單庫單表的簡單擴充套件因為我上一篇文章那種方法只能把資料進行簡單的遷移,但是這裡我需要對資料庫的內容進行重新的編排,所以很顯然,我上一篇文章的寫法是沒法完成我的需求的。 重申一下需求: 從一張資料表:物品分類資料資源表和一張關係表:物品分類關係表將這兩張表的內容整理到一張表中:整理好的表,目的是為了把前兩張表裡面的第一層的物品第二層的物品,第三層的物品整理出來,並且寫好歸屬。 (姑且算是遷移吧) 不知道看管能不能有耐心看完他,總之原理很簡單,

  1. 從一個數據庫中把資料讀取出來。
  2. 然後在連結上另外一個數據庫的表進行insert寫入。

當然,我想使用MySQL的儲存過程應該也是可以完成這個需求的,但是我目前還不會,之後會整理之後放入這一系列的文章中。 (程式碼寫的很垃圾,將就著看吧。)

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;
import java.util.List;

public class DataCategorySinze {

	static ResultSet rsOne;
	private  static List<ResultSet> list = new ArrayList<ResultSet>();
	
	public static void main(String[] args) {
		int count = 0;
		try {
			//首先寫入第一層
//			DataCategorySinze.getDeepZeroS();
			//接著寫入第二層
//			DataCategorySinze.getDeepZero(count);
			//最後寫入第三層
			DataCategorySinze.getGc_Id_3();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 讀取,並寫入第一層的程式碼
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static void getDeepZeroS() throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select DISTINCT gc_id_1 from 33hao_goods_class_staple");
		while (rs.next()) {
			String gc_id_1 = rs.getString("gc_id_1");
			DataCategorySinze.getGc_Id_1S(gc_id_1);
//			DataCategorySinze.getGc_Id_1(gc_id_1,1,count);
			System.out.println(gc_id_1);
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	public static void getGc_Id_1S(String gc_id_1) throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		String sql = "SELECT gc_name as name,jx as catCode from 33hao_goods_class WHERE gc_id ="+gc_id_1;
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		while (rs.next()) {
			String name = rs.getString("name");
			String catCode = rs.getString("catCode");
			DataCategorySinze.inserDeepZero(name, 0, catCode, 0);
			System.out.println(name+" "+catCode+" ");
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	/**
	 * 讀取,並寫入第二層的程式碼
	 * @param count
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public static void getDeepZero(int count) throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select DISTINCT gc_id_1 from 33hao_goods_class_staple");
		while (rs.next()) {
			count++;
			String gc_id_1 = rs.getString("gc_id_1");
			DataCategorySinze.getGc_Id_1(gc_id_1,1,count);
			System.out.println(gc_id_1);
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	
	public static void getGc_Id_1(String gc_parent_id,int deep,int count) throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		String sql = "SELECT gc_name as name,jx as catCode from 33hao_goods_class WHERE gc_parent_id ="+gc_parent_id;
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(sql);
		while (rs.next()) {
			String name = rs.getString("name");
			String catCode = rs.getString("catCode");
			Integer parentId = Integer.valueOf(count);
			DataCategorySinze.inserDeepZero(name, parentId, catCode, deep);
//			DataCategorySinze.getGc_Id_1(String.valueOf(deep), 2, 0);
			System.out.println(name+" "+catCode+" "+parentId);
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	/**
	 * 讀取,並寫入第三層的程式碼
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void getGc_Id_3() throws ClassNotFoundException, SQLException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select DISTINCT gc_id_3 from 33hao_goods_class_staple");
		while (rs.next()) {
			String gc_id_3 = rs.getString("gc_id_3");
			DataCategorySinze.getGc_Id_3_GetParentInfo(gc_id_3);
			System.out.println(gc_id_3);
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	public static void getGc_Id_3_GetParentInfo(String gc_id_3) throws ClassNotFoundException, SQLException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select gc_name as name,gc_parent_id,jx as catCode   from 33hao_goods_class where gc_id ="+gc_id_3);
		while (rs.next()) {
			String gc_parent_id = rs.getString("gc_parent_id");
			String name = rs.getString("name");
			String catCode = rs.getString("catCode");
			DataCategorySinze.getGc_Id_3_GetParentInfoDeatil(gc_parent_id,name,catCode);
			System.out.println(gc_parent_id);
		}
	}
	
	public static void getGc_Id_3_GetParentInfoDeatil(String gc_parent_id,String name,String catCode) throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sakila?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select  *   from 33hao_goods_class where gc_id = "+gc_parent_id);
		while (rs.next()) {
			String gc_name = rs.getString("gc_name");
			ResultSet set = DataCategorySinze.getNewTablesInfo();
			while (set.next()) {
				String string = set.getString("name");
				String id = set.getString("id");
				Integer parentId = Integer.valueOf(id);
				if (gc_name.equals(string)) {
//					for (int i = 0; i < list.size(); i++) {
//						ResultSet resultSet = list.get(i);
							DataCategorySinze.inserDeepZero(name, parentId, catCode, 2);
//					}
				}
			}
		}
		rs.close();
		st.close();
		conn.close();
	}
	
	public static ResultSet getNewTablesInfo() throws SQLException, ClassNotFoundException{
		String URL = "jdbc:mysql://localhost:3306/sb_01?characterEncoding=UTF-8";
		String USER = "root";
		String PASSWORD = "root";
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		Statement st = conn.createStatement();
		ResultSet rs = st
				.executeQuery("select id,name,parentId,catCode,deep from merchandisecategory where deep =1");
		return rs;
	}
	
	public static void inserDeepZero( 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 ( name, parentId, catCode, deep) VALUES (  ?, ?, ?, ?)";
		int executeUpdate = 0;
		// 1.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setInt(2, parentId);
			pstmt.setString(3, catCode);
			pstmt.setInt(4, deep);
			executeUpdate = pstmt.executeUpdate();
			pstmt.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// 關閉資源
		conn.close();
	}
	
	
	/**
	 * 獲取被複制資料
	 * 
	 * @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.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		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.處理資料庫的返回結果(使用ResultSet類)
		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"));
		}
		// 關閉資源
		rs.close();
		st.close();
		conn.close();
	}

	/**
	 * 插入目標資料庫表中相應的欄位值
	 * 
	 * @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.載入驅動程式
		Class.forName("com.mysql.jdbc.Driver");
		// 2.獲得資料庫連結
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 3.通過資料庫的連線操作資料庫,實現增刪改查(使用Statement類)
		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();
		}
		// 關閉資源
		conn.close();
	}
}