Dynamic table name in a prepared staement
hii javaites
I am having a Query in which i want to have the contents of a table whose name is not harcoded but set through some string
I ma having Query like this
String strOtype="";
ps=con.prepareStatement("select * from (select orderNo from order where otype=?)");
ps.setString(1, strOtype);
Rs=ps.executeQuery();
but when i m running this
only o/p of inner query is coming
Can anyone help me in this
Mmmm so you have a database design where you have one table per order or some such nonsense. Yummy.
Get a new design that one sucks.
And no you can't do that.. dynamically do table names in SQL or PreparedStatements or any other voodoo you throw at it.
But god almighty fix that design. This very issue was actually the feature on dailywtf this week.
> hey buddy
>
> I wnat 2 know can i give the name of table dynamiclly
> in a prepared statement.
No.
> Or is there any way around.]
No.
> bye the ay i m not using this design , tht was just
> for example.
>
Good then then you won't be bothered by that.
Hey buddy
Let me tell u can give the name of table dynamically in a prepared statement.
Let me tell u how 2 go abt it
// get the name of table in a string
String strTableName=rqquest.getParameter/("TableName");
String sql="select * from "+strTableName;
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
So bro this woks................
As outlined in this thread multiple tables for the same entity is a BAD idea, think of it this way a table is like a house you don't buy a new house each time you want to store something inside, bad analogy but it gives you the basic idea.
Just create a products table with prodid (primary key) productname etc etc and insert them, all your problems will be solved.