aspose file tools
The moose likes JDBC and the fly likes Passing Table Names in PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Passing Table Names in PreparedStatement" Watch "Passing Table Names in PreparedStatement" New topic
Author

Passing Table Names in PreparedStatement

Senthil Kumar
Ranch Hand

Joined: Oct 14, 2007
Posts: 33
Hi all,
I was trying to pass a table name in the query dynamically like

query = "select * from ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1,"table1");

It Throws an Exception
"Exception in thread "main" com.sybase.jdbc3.jdbc.SybSQLException: ASA Error -141: Table '@p0' not found"

Is there a way to pass the table name dynamically?
Thanks in Advance
Prem Shankar
Greenhorn

Joined: Jul 28, 2003
Posts: 21
Senthil,
We cannot pass tables as bind variable using prepared statement..
Instead form the query string dynamically.
eg: query="select * from "+var1;
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Well we can't pass the tablename directly to prepared statements.

But, if you want to use stored procedures then you can pass table name dynamically using Dynamic SQL.

For further reading of Dynamic SQL refer this article on Dynamic SQL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Table Names in PreparedStatement
 
Similar Threads
Hibernate createSQLQuery parameter substitution problem
In PreparedStatement table name can vary
html radio button add to database
execute Create table statement from JSP
PreparedStatement