A friendly place for programming greenhorns!
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
Author
Passing Table Names in PreparedStatement
Senthil Kumar
Ranch Hand
Joined: Oct 14, 2007
Posts: 33
posted
Apr 03, 2008 05:19:00
0
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
posted
Apr 03, 2008 06:22:00
0
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
I like...
posted
Apr 03, 2008 06:35:00
0
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
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter