You execute that just the way you would execute any other SQL statement. (I am assuming that you already use JDBC and don't need to be told to read the tutorial.) However, if your database doesn't support subselects (as the one I use doesn't) then you will have to redesign that. Often a subselect can be rewritten as a (less beautiful) join.
> SELECT name,countryid FROM supplier WHERE id = (SELECT
> id FROM materials WHERE usage = 'raw')
If your database supports this, JDBC will support it, too. If it doesn't, JDBC won't do anything magic for you - you'll have to rewrite this as a JOIN, or run two separate SELECTS, and build the outer select dynamically from the result of the inner select..
My source looks like that:
try{
conPnProc = DriverManager.getConnection("jdbc:odbc:pnprocur","","");
conPnSupp = DriverManager.getConnection("jdbc:odbc:pnsupply","","");
conAddress = DriverManager.getConnection("jdbc:odbc:address","","");
Statement pnProcStmt = conPnProc.createStatement();
Statement pnSuppStmt = conPnSupp.createStatement();
Statement addressStmt = conAddress.createStatement();
ResultSet rs = pnProcStmt.executeQuery("SELECT * FROM pnprocur, address");
}catch(SQLException e){}
When I use an other table name then that one the connection was created for he gives me an error that: can not open database.
So I can't do any joins or embedded SELECT statement.
In your SQL statement, "SELECT * FROM pnprocur, address" the syntax for such a statement is "SELECT data FROM table" - but your tables - pnprocur and address in this statement are the namesake of the database which you connect to in your conenction statements :
conPnProc = DriverManager.getConnection("jdbc:odbc:pnprocur","","");
conAddress = DriverManager.getConnection("jdbc:odbc:address","","");
Is there in fact a db named pnprocur, aswell as a table named pnprocur (and adress etc) ?
Also, it is better to split up connection to db and interrogation, that way you know exactly where your code has failed - from your error message, it appears that there is a problem with your connection string... which should look like :
DriverManager.getConnection("jdbc:odbc:DATABASE_NAME","","");
> DrClap said
>
> > Often a subselect can be
> > rewritten as a (less beautiful) join.
>
> What do you have against joins? Were you bitten by
> one as small child perhaps?
No, the subselect version just looks more elegant to me, and I usually find it easier to understand. (It wasn't until I was middle-aged that I was bitten by joins.)
> > DrClap said
> >
> > > Often a subselect can be
> > > rewritten as a (less beautiful) join.
> >
> > What do you have against joins? Were you bitten by
> > one as small child perhaps?
>
> No, the subselect version just looks more elegant to
> me, and I usually find it easier to understand. (It
> wasn't until I was middle-aged that I was bitten by
> joins.)
I find subselects more elegant too, but it could be easier for databases to optimize join statements. As far as I remember, Oracle can optimize subselects by converting them internally into a join.