我需要在运行时动态生成DB中所有表的INSERT stmt。我正在逐个获取表内容(数据)并在运行时确定列类型,同时循环通过ResultSet& ResultSetMetaData是为String类型添加Double Quotes(“)而不为非String类型的列添加相同的。但是当实现当前逻辑时,它会忽略第一条记录,并且对表中的所有剩余记录都可以正常工作。知道为什么它正在工作,因为在获取rs.getColumnType(int columnIndex)
时我们需要给出从1索引开始的列名。但是不能构建逻辑以便它将检查第一行而不是忽略它时刻..
请参阅下面的代码段
if (rs != null) {
while (rs.next()) {
sqlString = new StringBuffer();
for (int i = 1; i <= columnCount; i++) {
Object obj = rs.getObject(i);
if (obj == null) {
sqlString.append("null");
sqlString.append(",");
}
else
{
int sqlTypes = rsmd.getColumnType(i);
switch (sqlTypes) {
case Types.VARCHAR:
sqlString.append("\"");
if (obj != null)
sqlString.append(rs.getString(i));
sqlString.append("\"");
sqlString.append(",");
break;
case Types.NULL:
sqlString.append("null");
sqlString.append(",");
break;
case Types.CHAR:
sqlString.append("\"");
if (obj != null)
sqlString.append(rs.getString(i));
sqlString.append("\"");
sqlString.append(",");
break;
case Types.TIMESTAMP:
sqlString.append("\"");
if (obj != null)
sqlString.append(rs.getTimestamp(i));
sqlString.append("\"");
sqlString.append(",");
break;
case Types.DOUBLE:
if (obj != null)
sqlString.append(rs.getDouble(i));
sqlString.append(",");
break;
case Types.INTEGER:
if (obj != null)
sqlString.append(rs.getInt(i));
sqlString.append(",");
break;
case Types.SMALLINT:
if (obj != null)
sqlString.append(rs.getInt(i));
sqlString.append(",");
break;
case Types.DECIMAL:
if (obj != null)
sqlString.append(rs.getBigDecimal(i));
sqlString.append(",");
break;
/*default:
if (obj != null)
sqlString.append(obj.toString());
sqlString.append(",");
break;*/
}
}
}
答案 0 :(得分:2)
您是否尝试过使用do while循环:
{{1}}