gpt4 book ai didi

java - 如何动态确定 ResultSet 列类型

转载 作者:行者123 更新时间:2023-11-30 02:50:14 24 4
gpt4 key购买 nike

我需要在运行时动态为数据库中的所有表生成 INSERT stmt。我正在逐一获取表内容(数据)并在运行时确定列类型,同时循环遍历 ResultSet 和 ResultSetMetaData,以便为字符串类型添加双引号(“),而不为非字符串类型的列添加相同的双引号(”) 。但是,根据当前逻辑的实现,它会忽略第一条记录,并且对于表中的所有剩余记录都可以正常工作。我知道它为什么可以工作,就像在获取 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;*/
}
}
}

最佳答案

您是否尝试过使用这样的 do while 循环:

if (rs != null) {
do{
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;*/
}
}while (rs.next());
}

关于java - 如何动态确定 ResultSet 列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38919891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com