简体   繁体   中英

How to create temp table in informix using java?

SampleSQL.sql

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

My below code isn't creating the temp table, but when I execute the above SQL in Informix server it creates the table without issue.

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());

If you cannot insert records into this table, then maybe you try to insert them using different database connection. Temporary tables exist only during life of connection in which they were created.

Your code executes CREATE TEMP TABLE ... very well. You have some errors in your SQL statement which ends with syntax error from Informix:

  1. Instead of underscore in TABLE_TMP use space, or replace text in other way as you should use CREATE TEMP TABLE table_name ...
  2. You must provide column types. Instead of column1, column2, column3 use column1 int, column2 int, column3 int (of course you can replace int with other SQL type)

I tested your code with:

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

and it works. I also tested that you can insert into this table:

...
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);

My results:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM