我正在尝试使用从数据字典中获取的信息创建Oracle SQL脚本以创建/替换多个视图。
我已经设法通过JDBC驱动程序和对数据字典的查询来建立一个有效的数据库连接,该数据字典也能正常运行并返回正确的值。
然而,在将查询的信息存储在String数组中时 - 然后将其添加到String []的ArrayList中 - 似乎出现了问题,因为所有数组似乎在各自的索引位置都具有相同的值而且我没有&# 39;有一个线索,为什么会这样。
这是我的代码,如果有人能发现错误,我会很感激:
public ArrayList<String[]> getDataDictionary(ArrayList<String> dbInfo, String table) throws SQLException {
ArrayList<String[]> result = new ArrayList<String[]>();
String[] resultTemp = new String[2];
... connection variables (URL, User, Pass)
... get connection, etc.
try {
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '" + table + "'");
while (rs.next()) {
resultTemp[0] = rs.getString("COLUMN_NAME");
resultTemp[1] = rs.getString("DATA_TYPE");
// database values
System.out.println(rs.getString("COLUMN_NAME"));
System.out.println(rs.getString("DATA_TYPE"));
// array values
System.out.println(resultTemp[0]);
System.out.println(resultTemp[1]);
//The above sout's return the proper values for each pass of the loop
//This is what feels the strangest to me. The values are correct here, but when queried later they are wrong
result.add(resultTemp);
}
String[] test = new String[2];
// sout's return wrong values now, i.e. the value returned is always the same for all arrays queried in the ArrayList
//I don't understand how that can be, because the correct values were added to the ArrayList a few lines above and now they are wrong with no changes made
test = result.get(0);
System.out.println(test[0]);
System.out.println(test[1]);
test = result.get(1);
System.out.println(test[0]);
System.out.println(test[1]);
test = result.get(2);
System.out.println(test[0]);
System.out.println(test[1]);
rs.close();
statement.close();
con.close();
return result;
} catch(Exception e)
{
e.printStackTrace();
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error!");
alert.setHeaderText("Invalid SQL!");
alert.setContentText("Please verify the information you provided!");
alert.showAndWait();
return null;
}
答案 0 :(得分:3)
你应该在循环中创建数组实例。
while (rs.next()) {
String[] resultTemp = new String[2];
resultTemp[0] = rs.getString("COLUMN_NAME");
resultTemp[1] = rs.getString("DATA_TYPE");
....
如果不这样做会导致同一个数组多次添加到result
。
答案 1 :(得分:3)
在每个循环中存储对同一对象的引用。
你必须在每个lopp创建一个新数组:
while (rs.next()) {
resultTemp = new String[2];
resultTemp[0] = rs.getString("COLUMN_NAME");
resultTemp[1] = rs.getString("DATA_TYPE");
// database values
System.out.println(rs.getString("COLUMN_NAME"));
System.out.println(rs.getString("DATA_TYPE"));
// array values
System.out.println(resultTemp[0]);
System.out.println(resultTemp[1]);
//The above sout's return the proper values for each pass of the loop
//This is what feels the strangest to me. The values are correct here, but when queried later they are wrong
result.add(resultTemp);
}
答案 2 :(得分:2)
您正在覆盖相同的数组String[] resultTemp = new String[2];
while (rs.next()) {
String[] resultTemp = new String[2];
resultTemp[0] = rs.getString("COLUMN_NAME");
resultTemp[1] = rs.getString("DATA_TYPE");
在while
循环内初始化它。所以当你添加
result.add(resultTemp);
result
将保留其中resultTemp[]
个对象的列表。