tags:

views:

971

answers:

10

Is there a way to execute a query(containing built in DB function) using PreparedStatement?

Example: insert into foo (location) values (pointfromtext('12.56666 13.67777',4130)) Here pointfromtext is a built in function.

+2  A: 

Sure, that should work.

If not, what is your database system, and can you run the exact same command from the SQL command line?

Thilo
A: 

The scope of a PreparedStatement object is exactly to execute queries. If the query contains built in DB function is ok, and everything should work if the same query works outside the PreparedStatement.

As Thilo said, test your query form SQL command line, or with the SQL graphical tool that you usually use.

alexmeia
A: 

I guess I was not clear. I can run the above SQL as a statement.

What I want to do is parametrize it and run it using a prepared statement. Something like this.

PreparedStatement bar = connection.prepareStatement("insert into foo (location) values (pointfromtext('? ?',4130)))"); bar.setDouble(1, 13.67777); bar.setDouble(2, 13.67777); bar.executeUpdate();

But the above does not work. How do I go about doing it?

Abhi
A: 

Did you put connection.commit() after this code?

The code should be:

 PreparedStatement bar = connection.prepareStatement("insert into foo (location) values (pointfromtext('? ?',4130)))");
 bar.setDouble(1, 13.67777);
 bar.setDouble(2, 13.67777); 
 bar.executeUpdate();
 connection.commit();
alexmeia
A: 

This executes when executed through command propmpt

insert into map_address (location) values(pointfromtext('POINT(12.565 13.457)',4130))

Now the prepared statement equivalent of this would be

PreparedStatement preparedStatement = getConnection().prepareStatement("insert into map_address (location) values(pointfromtext('POINT(? ?)',4130))");
preparedStatement.setDouble(1, 12.56565665);
preparedStatement.setDouble(2, 12.57565757);
preparedStatement.executeUpdate();

When I execute this I get the following error

Exception in thread "main" org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
    at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:52)
Abhi
A: 

Did you try to don't set doubles in the preparaedStatement? Just for testing, you should try to insert this parameters directly in the String, something like:

String sql = "insert into map_address (location) values(pointfromtext('POINT(" +  "12.56565665" + " " + "12.57565757" + ")',4130))"

PreparedStatement preparedStatement = getConnection().prepareStatement(sql);

and then try to execute the update.

alexmeia
+1  A: 

The question marks are not being evaluated correctly because they are between simple quotes. Remove them, and it should work,

Alexandre
A: 

That is the problem. If I remove the quotes the function does not execute(Db throws error). I tried escaping and stuff like that. Did not work. Any pointers?

Abhi
Which errors does the DB throws without quotes?What is the code of your DB function pointfromtext?
Alexandre
This is the error DB throws(with out quotes):Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"It is a stored procedure provided by postgis extension of postgresql. I have not taken a look at the internals of the stored procedure.
Abhi
Michael Myers
A: 

Perhaps you've found a problem with the Postgresql JDBC driver, rather than with JDBC per se. In general what you want to achieve works with JDBC.

Steve McLeod
+2  A: 

By what I've seen, the first parameter on pointfromtext function is a string, and the second a number. So, try the following:

PreparedStatement preparedStatement = getConnection().prepareStatement("insert into map_address (location) values(pointfromtext('POINT(' || ? || ' ' || ? || ')',4130))");
preparedStatement.setString(1, "12.56565665");
preparedStatement.setString(2, "12.57565757");
preparedStatement.executeUpdate();
Alexandre
Is String concatenation the only way out? Is there no way I can tell JDBC to escape the quotes and let it know that ? is to be substituted with a parameter?
Abhi
You can also bind a single string "POINT(12.56,12.57)" into it. And maybe there is an alternative to pointfromtext, something like pointfrom_xy which can take two numbers directly.
Thilo