作者:我就是老笨2013 | 来源:互联网 | 2023-08-25 17:13
IamtryingtocreateOracleSQLscriptsforcreatingreplacingseveralviewsbyusinginformationa
I am trying to create Oracle SQL scripts for creating/replacing several views by using information acquired from the data dictionary.
我正在尝试使用从数据字典中获取的信息创建Oracle SQL脚本以创建/替换多个视图。
I already managed to set up a working database connection via JDBC drivers and a query to the data dictionary, which works as well and returns the correct values.
我已经设法通过JDBC驱动程序和对数据字典的查询来建立工作数据库连接,该数据字典也可以工作并返回正确的值。
However upon storing the queried information in a String array - which is then added to an ArrayList of String[] - something seems to go wrong, as all arrays seem to have the same values in their respective index positions and I don't have a clue why that is.
然而,在将查询的信息存储在String数组中时 - 然后将其添加到String []的ArrayList中 - 似乎出现了问题,因为所有数组似乎在各自的索引位置具有相同的值,而我没有提示为什么会这样。
Here's my code, I'd appreciate if someone can spot the error:
这是我的代码,如果有人能发现错误,我会很感激:
public ArrayList getDataDictionary(ArrayList dbInfo, String table) throws SQLException {
ArrayList result = new ArrayList();
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;
}
3 个解决方案