package dao.impl;
import util.JdbcUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class test {
public static List tableNameANDSQL() throws SQLException {
List<String> strings = new ArrayList<>();
String sql = "select * from test where FLAG='Y' "; //先把Oracle数据库中的表查询一次,用“flag=‘Y‘”为条件查询
Connection connection = JdbcUtil.OracleGet(); //从工具类中获取Oracle的链接驱动
Statement Statement = connection.createStatement();
ResultSet resultSet = Statement.executeQuery(sql);
while (resultSet.next()) {
String tableName = resultSet.getString("TableName");
System.out.println("tableName = " + tableName);
String oracleSql = resultSet.getString("OracleSql");
System.out.println("oracleSql = " + oracleSql);
String SQLServerSql = resultSet.getString("SQLServerSql");
System.out.println("SQLServerSql = " + SQLServerSql);
String pk = resultSet.getString("pk"); //主键
System.out.println("pk = " + pk);
List list = OracleSelectALL(oracleSql, SQLServerSql, pk);
}
JdbcUtil.close2(resultSet, Statement, connection);
return strings;
}
public static List OracleSelectALL(String OracleSql, String SQLServerSql, String pk) {
try {
Connection conn = JdbcUtil.OracleGet(); //从工具类中获取到链接数据库
PreparedStatement stmt = conn.prepareStatement(OracleSql);
ResultSet rs = stmt.executeQuery(); //rs就是Oracale数据的结果集
ResultSetMetaData rm = rs.getMetaData(); //从结果集中获取数据
int c = rm.getColumnCount(); //从数据中获取到列的数量
ArrayList<String> list = new ArrayList<>();
while (rs.next()) {
String s = null; //定义 一个 Sting s来 拼接字符串
String pkString = "1=1"; //1=1,是为了防止where后面没有条件做查询的时候,以防报错
for (int i = 0; i < c; i++) { //循环,以列的数量来做次数
String columnName = rm.getColumnName(i + 1);
//拼接 s是Oracle数据的
s += rs.getString(columnName); //a+=b <==> a=a+b s=s+rs.getString(columnName);
//s1是 SQL Server数据的
if (pk.contains(columnName)) {
//拼接where 语句
pkString += " and " + columnName + "='" + rs.getString(columnName) + "'";
System.out.println("pkString = " + pkString);
}
}
String s1 = SQLServerSelectALL(SQLServerSql + pkString); // SQLServerSql += pkString <==> SQLServerSql = SQLServerSql + pkString;
if (!s.equals(s1)) { //equals 比较内容
//如果不一致就直接返回sqlserver的字符串
System.out.println(s1);
//写个方法,把数据插入到数据库中
list.add(s1);
}
}
JdbcUtil.close2(rs, stmt, conn);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//sqlserver的结果集
public static String SQLServerSelectALL(String sql) throws Exception {
String s = null; //定义出一个Sting 用来拼接字符串
Connection conn = JdbcUtil.SQLServerGet(); //从工具类中获取到链接数据库
PreparedStatement stmt = conn.prepareStatement(sql); //执行sql
ResultSet rs = conn.createStatement().executeQuery(sql); //rs就是sqlserver数据的结果集
ResultSetMetaData rm = rs.getMetaData(); //从结果集中获取数据
int c = rm.getColumnCount(); //从数据中获取到数据量
while (rs.next()) {
for (int i = 0; i < c; i++) {
String columnName = rm.getColumnName(i + 1); //获取列名 id
//拼接
s += rs.getString(columnName);
//通过列名获取列值拼接到 s 中
System.out.println("s = " + s);
}
}
JdbcUtil.close2(rs, stmt, conn);
return s; //把 s 返回
}
//把数据插入数据库
//已经知道表名,但不知道要插入多少数据,所以要使用循环或者占位符
public static void insert(String tableName, Object... obj) throws Exception {
String sql = "insert into table (?,?)value(?,?)";
Connection conn = JdbcUtil.SQLServerGet();
PreparedStatement st = conn.prepareStatement(sql);
st.executeQuery(sql);
for (int i = 1; i <= obj.length; i++) {
st.setObject(i, obj[i - 1]);
}
JdbcUtil.close1(null, st, conn);
}
}