Darneezie
Darneezie

Reputation: 67

postgresql + Java

I am trying to execute this sql code in java but after it executes, I need to set it to a value im trying to enter into a table.

st = "INSERT INTO master_st4(unix_timestamp, hrap_xy, rain) values(?, ?, ?)";
state = connection.prepareStatement(st);
state.setInt(1, "select extract (epoch from '2014-03-01 06:00:00 UTC':: timestamp with time zone)");
state.setInt(2, in);
state.setDouble(3, arrBDS);                    
state.executeUpdate();

I know this returns something else than an integer, but I need a integer to pass into the first of the three set statements. unix_timestamp is an integer, hrap_xy is an integer, rain is a real number so double.

Here is the error message:

Exception in thread "main" java.lang.NumberFormatException: For input string: "select extract (epoch from '2014-03-01 06:00:00 UTC':: timestamp with time zone)" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at JgribDEX.main(JgribDEX.java:110)

Upvotes: 0

Views: 158

Answers (1)

SergeyB
SergeyB

Reputation: 9848

What if you put the subquery right into the query like so:

st = "INSERT INTO master_st4(unix_timestamp, hrap_xy, rain) values((select extract (epoch from '2014-03-01 06:00:00 UTC':: timestamp with time zone)), ?, ?)";
state = connection.prepareStatement(st);
state.setInt(1, in);
state.setDouble(2, arrBDS);                    
state.executeUpdate();

Upvotes: 1

Related Questions