Oracle数据库连接生成DataX的job-Json

时间:2024-10-13 00:06:31
package com.bbkj.main;

import com.bbkj.DbUtils.ConnectionPoolManager;
import com.bbkj.DbUtils.DbUtil;
import com.bbkj.DbUtils.IConnectionPool; import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set; /**
* Created by Administrator on 2016/12/7.
*/
public class Json { public static void main(String [] args) throws InterruptedException {
testOracle();
}
/**
* 一个非常标准的连接Oracle数据库的示例代码
*/
public static void testOracle()
{
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
try
{
con = DbUtil.pool.getConnection();
String sql = "select * from user_tables";// 预编译语句,“?”代表参数
pre = con.prepareStatement(sql);// 实例化预编译语句
//pre.setString(1, "");// 设置参数,前面的1表示参数的索引,而不是表中列名的索引
result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
StringBuilder sbstr =new StringBuilder(); //fos.write(rs.getInt(1));
InputStreamReader fis = new InputStreamReader(
new FileInputStream(System.getProperty("user.dir")+"/template/template.json"));
BufferedReader bis = new BufferedReader(fis);
String data = null;
while ((data =bis.readLine())!=null){
sbstr.append(data);
}
data = sbstr.toString(); while (result.next()) {
StringBuilder sb =new StringBuilder();
String subsql = "select * from user_tab_columns where table_name = '" + result.getString("table_name") + "'";
PreparedStatement subpre =con.prepareStatement(subsql);
ResultSet subresult = subpre.executeQuery();
int i=0;
FileOutputStream fos = new FileOutputStream("E:\\bobo\\dbfile\\"+result.getString("table_name")+".json",false);
PrintStream p = new PrintStream(fos);
while (subresult.next()) {
if(i>0){
sb.append(",");
}
sb.append(" ").append(subresult.getString("COLUMN_NAME"))
.append(" ");
i++;
}
System.out.println(sb.toString());
data = data.replaceAll("\\$\\{table\\}",result.getString("table_name"));
data = data.replaceAll("\\$\\{fields\\}",sb.toString());
p.print(data.toString());
p.close();
fos.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null) {
DbUtil.pool.releaseConn(con);
con.close();
}
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}

这里用到了一个别人写的数据库连接池:我的另外一篇文章有转载http://www.cnblogs.com/HendSame-JMZ/articles/6145456.html

//取得该用户下所有的表
select * from user_tables;
//取得表名为Sysuser的注释信息
select * from user_tab_comments where table_name = 'SYSUSER';
//取得该用户下表名为Sysuser表的结构
select * from user_tab_columns where table_name='SYSUSER';
//取得该用户下表名为sysuser表中字段的注释信息
select * from user_col_comments where table_name = 'SYSUSER';
//取得该用户下所有表的中文名称和英文名称
select t2.table_name,t2.comments from user_tables t1,user_tab_comments t2 where t1.table_name=t2.table_name;
TABLE_NAME COMMENTS
------------------------------ -----------------------------------------------
SYSUSER 用户表
//取得表sysuser中的英文字段名,中文字段名,字段类型,字段长度
select t1.TABLE_NAME,t1.COLUMN_NAME,t2.COMMENTS,t1.DATA_TYPE,t1.DATA_LENGTH from user_tab_columns t1, user_col_comments t2 where t1.TABLE_NAME='SYSUSER' and t1.TABLE_NAME=t2.TABLE_NAME and t1.COLUMN_NAME=t2.COLUMN_NAME;
TABLE_NAME COLUMN_NAME COMMENTS DATA_TYPE DATA_LENGTH
----------- ----------------------------------------------------------------------
SYSUSER SYSUSERID 用户ID VARCHAR2 50
SYSUSER SYSUSERNAME 用户名 VARCHAR2 50
SYSUSER SYSUSERPASSWORD 密码