2016-02-26 124 views
0

SampleSQL.sql如何使用java在informix中創建臨時表?

CREATE TEMP TABLE_TMP(column1, column2, column3)with NO LOG; 

我下面的代碼沒有創建臨時表,但是當我在Informix服務器執行上述SQL也沒有問題創建表。

Class.forName("com.informix.jdbc.IfxDriver"); 
conn = DriverManager.getConnection(url); 

//Informix temp table creation 
BufferedReader tempScriptReader = new BufferedReader(new FileReader("SampleSQL.sql")); 
String str; 
StringBuffer tempTableQry = new StringBuffer(); 
while ((str = tempScriptReader.readLine()) != null) { 
    tempTableQry.append(str + "\n "); 
} 
tempScriptReader.close(); 
stmt = conn.createStatement(); 
//prepStatement = conn.prepareStatement(tempTableQry.toString()); 
System.out.println(tempTableQry.toString()); 
stmt.executeUpdate(tempTableQry.toString()); 
+0

如何'stmt.executeUpdate()'報告錯誤?你有沒有看過它報告的錯誤?爲什麼'executeUpdate()'?沒有一個簡單的'executeImmediate()'?您顯示的文件中的SQL究竟是什麼?如果是這樣,你錯過了列的類型信息。 –

+0

嗨喬納森,謝謝你的迴應。 stmt.executeUpdate()不會給出任何錯誤。但是當我嘗試將值插入臨時表(此處爲TABLE_TMP)時,系統會拋出一個錯誤,指出「指定的表不在數據庫中」。看來executeUpdate不會創建臨時表。任何對此的幫助表示讚賞。 – DBest

+0

不知道 - 我寫了足夠少的Java,沒有使用JDBC的代碼。本教程是否在JDBC中使用['executeQuery()'vs'executeUpdate()'vs execute()'](http://javaconceptoftheday.com/difference-between-executequery-executeupdate-execute-in-jdbc /)的幫助呢? AFAICT,你應該沒問題。你打印出你傳遞給'executeUpdate()'的字符串嗎? –

回答

0

如果您不能將記錄插入到此表中,那麼您可能嘗試使用不同的數據庫連接插入它們。臨時表僅在其創建連接的生命期間存在。

您的代碼很好地執行CREATE TEMP TABLE ...。你有一些錯誤,在SQL語句與語法錯誤從Informix結束:

  1. 而是在TABLE_TMP利用空間下劃線的,或者以其他方式替換文本,你應該使用CREATE TEMP TABLE table_name ...
  2. 您必須提供列類型。取而代之的column1, column2, column3使用column1 int, column2 int, column3 int(當然你也可以與其他SQL類型更換int

我測試你的代碼:

CREATE TEMP TABLE TABLE_TMP(column1 int, column2 int, column3 int) with NO LOG; 

和它的作品。我還測試了你可以插入到這個表:

... 
stmt.executeUpdate(tempTableQry.toString()); 
for (int i = 0; i < 3; ++i) { 
    int r = stmt.executeUpdate("INSERT INTO TABLE_TMP (column1, column2, column3) VALUES(" + i + ", " + i + ", " + i + ")"); 
    System.out.println("execute result: " + r); 
} 
Statement sq = conn.createStatement(); 
ResultSet rs = sq.executeQuery("SELECT COUNT(*) FROM TABLE_TMP"); 
rs.next(); 
int count = rs.getInt(1); 
System.out.println("record count: " + count); 

我的結果:

CREATE TEMP TABLE TABLE_TMP(column1 int, column2 int, column3 int) with NO LOG; 
execute result: 1 
execute result: 1 
execute result: 1 
record count: 3 
+0

它工作正常。謝謝Michal – DBest