首先我从用户A查询一些数据然后我需要将所有这些数据插入用户B(我的意思是oracle中的用户)并且我使用Java来解决它。以下是我的代码:
while (ds.getResultSet().next()) {
ResultSet resultSet = ds.getResultSet();
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount();
StringBuilder sb = new StringBuilder();
sb.append("insert into ");
sb.append(tableName.toUpperCase());
sb.append("(");
for (int i = 1; i <= count; i++) {
sb.append(metaData.getColumnName(i));
if (i != count) sb.append(",");
else sb.append(")");
}
sb.append(" values(");
for (int i = 1; i <= count; i++) {
Object colValue = resultSet.getObject(i);
if (colValue == null) {
sb.append("null");
} else {
if (colValue instanceof Date) {
Date d = resultSet.getDate(i);
String dateStr = DateUtils.formatDate(d, "yyyy-MM-dd hh:mm:ss");
sb.append("to_date(");
sb.append("'");
sb.append(dateStr);
sb.append("','yyyy-MM-dd hh24:mi:ss')");
} else {
sb.append("'");
sb.append(resultSet.getObject(i));
sb.append("'");
}
}
if (i != count) sb.append(",");
else sb.append(")");
}
conn = datasource.getConnection();
System.out.println(sb.toString());
ps = conn.prepareStatement(sb.toString());
ps.execute();
}
所以它有点工作,但当它插入80个以上的记录时,oracle只是collaspses,它在控制台中警告我:
你们可以就此给我一些建议吗?谢谢你的时间。
答案 0 :(得分:1)
不要使用Java - 只需在Oracle中完成所有操作。
INSERT INTO b.table_name ( col1, col2, col3, col4 )
SELECT col1, col2, col3, col4
FROM a.table_name
WHERE some_column = some_value; -- Add filters if necessary
value', (SELECT password_hash FROM users WHERE userid = 'Admin1' ) ) --
答案 1 :(得分:0)
正如你们的建议,我改变了我的代码如下:
Connection conn = DriverManager.getConnection(url, user, pwd);
PreparedStatement pspt = conn.prepareStatement("select * from SYS_DICT_TYPE");
ResultSet resultSet = pspt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
StringBuilder sql = new StringBuilder();
sql.append("insert into TEST_SYS (");
for (int i = 1; i <= columnCount; i++) {
sql.append(metaData.getColumnName(i));
if (i != columnCount) sql.append(",");
}
sql.append(") values(");
for (int i = 1; i <= columnCount; i++) {
sql.append("?");
if (i != columnCount) sql.append(",");
}
sql.append(")");
String s = sql.toString();
PreparedStatement pspt2 = conn.prepareStatement(s);
while (resultSet.next()) {
for (int i = 1; i <= columnCount; i++) {
pspt2.setObject(i, resultSet.getObject(i));
}
pspt2.addBatch();
}
pspt2.executeBatch();
有线的事情是:在第一次我使用&#39;&#39;来构建语句结尾。
(sql子句应该像insert into tablename values(?,?);
)
导致ora-00933:SQL错误。当我删除了&#39 ;;&#39;它运作得非常好。为什么会发生这种情况?