mysql和postgresql数据库间的数据迁移(注释迁移)

本文介绍了一种使用Java程序跨数据库(从MySQL到PostgreSQL)批量更新表注释的方法,涉及连接数据库、获取表及字段信息并进行注释更新等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;


public class Test {
//---------------------------------------读取数据-------------------------------------
public static void main(String[] args) throws Exception {
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
PreparedStatement pre1 = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
ResultSet result1 = null;// 创建一个表名结果集对象


try {
Class.forName("com.mysql.jdbc.Driver");// 加载mysql驱动程序
System.out.println("开始尝试连接mydatabase数据库!");
String url = "jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8";
String user = "root";// 用户名,系统默认的账户名
String password = "123456";// 你安装时选设置的密码

con = DriverManager.getConnection(url, user, password);
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","root","123456");// 获取连接
System.out.println("输入数据库--mydatabase--连接成功!");


//--------------------查看表名-----------------------------------------****1改库名*****---------
String sql1 = "select table_name from information_schema.tables where table_schema='"
+ "pdl_warehouse1"                 //***********************1改输入库名***************************
+ "'";
pre1 = con.prepareStatement(sql1);
result1 = pre1.executeQuery();// 执行查询,注意括号中不需要再加参数

//--------------------查看列名/类型/注释--------------------------------------------------
while(result1.next()) {
System.out.println("------------进入库中表:"+result1.getString("table_name"));

//---------------------------mysql输入所需字段------------------------------
/*String sql = "select column_name,column_comment,column_type from information_schema.columns where table_schema='mydatabase' and table_name='"
+ result1.getString("table_name")
+ "'";// 预编译语句
*/

//---------------------------postgresql输入所需字段------------------------------
String sql = "select column_name,column_comment from information_schema.columns where table_schema='"
+ "pdl_warehouse1"              //**********************2改输入库名************************
+ "' and table_name='"
+ result1.getString("table_name")
+ "'";// 预编译语句
pre = con.prepareStatement(sql);// 实例化预编译语句
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数 
System.out.println("结果集result="+result+"结果集result="+result1);

   importData(result1,result);//进入更新
   
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result1 != null)
result1.close();
if (result != null)
result.close();
if (pre1 != null)
pre1.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("输入数据库--mydatabase--数据库连接已关闭!");
} catch (Exception e) {
e.printStackTrace();
}
}


}
//-------------------------------------------更新数据-------------------------------------


public static String importData(ResultSet result1,ResultSet result) {


Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
PreparedStatement pre1 = null;// 创建预编译语句对象,一般都是用这个而不用Statement

try {
//--------------------mysql-------------------
/*Class.forName("com.mysql.jdbc.Driver");// 加载mysql驱动程序
System.out.println("开始尝试连接testdb数据库!");
String url = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf8";
String user = "root";// 用户名,系统默认的账户名
String password = "123456";// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("testdb连接成功!");*/


//-------------------postgresql---------------
Class.forName("org.postgresql.Driver");// 加载postgresql驱动程序
System.out.println("开始尝试连接test数据库!");
String url = "jdbc:postgresql://101.201.144.119:8765/dw?useUnicode=true&characterEncoding=utf8";
String user = "etl_user";// 用户名,系统默认的账户名
String password = "temp4you";// 你安装时选设置的密码
con = DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("dw连接成功!");


int num = 0;//统计数据个数

while(result.next()) {
// System.out.println("表名:"+result1.getString("table_name")+"字段名:"+result.getString("column_name")+",字段类型:"+result.getString("column_type")+",字段注释:"+result.getString("column_comment"));
System.out.println("表名:"+result1.getString("table_name")+"字段名:"+result.getString("column_name")+",字段注释:"+result.getString("column_comment"));


//-------------------------更新mysql库中表注释-------------------------
/* String sql = "ALTER TABLE "
+ result1.getString("table_name")
+ " MODIFY COLUMN "
+result.getString("column_name")
+" "
+result.getString("column_type")
+" COMMENT "
+"'"
       +result.getString("column_comment")
       +"'";    // 预编译语句
       
               */
  //--------------------更新postgresql库中表注释------------------
 //comment on column test.test.name is 'hahaname';
       ResultSet rs = con.getMetaData().getTables(null, null, result1.getString("table_name"), null);  
                if(rs.next() ) {                                          //判断库中是否存在某表
String sql = "comment on column "
+"dw1."        //**************************************3改输出库名*****************************
   +result1.getString("table_name")
   +"."
   +result.getString("column_name")
+" is '"
+result.getString("column_comment")
+"'"; 
pre = con.prepareStatement(sql);// 实例化预编译语句
pre.execute();
   pre.close();

num = num + 1;
System.out.println("更新成功第" + (num) + "条数据");

//==========================日志管理==============================
Timestamp currentTime= new Timestamp(System.currentTimeMillis()); 
System.out.println("=========================="+currentTime);
String sql1 = "insert into test.message values('"
+result1.getString("table_name")
+"','"
   +result.getString("column_name")
+"','"
+result.getString("column_comment")
+"','"
+currentTime
                                 + "')";

pre1 = con.prepareStatement(sql1);// 实例化预编译语句
pre1.execute();
   pre1.close();
}
}


} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("dw数据库连接已关闭!");
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值