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

[466 byte] By [help_eachothera] at [2007-10-2 7:47:48]
# 1

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.

help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
hey buddyI wnat 2 know can i give the name of table dynamiclly in a prepared statement.Or is there any way around.]bye the ay i m not using this design , tht was just for example.Thanx
help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> 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.

help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

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................

help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
You aren't setting the table name in the prepared statement at all. You are doing by sticking two strings together. That is not using the prepared statement which is what you asked and the reply I gave.
help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
Anyways BoysThanx a lotCan u tell me when 2 use Rowset
help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
> Anyways Boys> > Thanx a lot> > Can u tell me when 2 use RowsetI think a better question is when are you considering using it? What purpose do you have. Then someone can tell you if that is an appropriate use of it.
help_eachothera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
> Can u tell me when 2 use RowsetWhen you need to. Or did you have a specific question that the documentation couldn't answer?
dcmintera at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

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.

spear_arrowa at 2007-7-16 21:34:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...