aspose file tools*
The moose likes JDBC and the fly likes jdbc objects in different JVM's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc objects in different JVM Watch "jdbc objects in different JVM New topic
Author

jdbc objects in different JVM's

Ashwini Sharma
Greenhorn

Joined: Nov 05, 2004
Posts: 4
Can anyone please tell me if I can use a Connection, ResultSet and Statement object in the following way specified.


///////////////////////////////////////////

Properties p = new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://172.20.8.140:7001");


Context ctx = null;
try {
ctx = new InitialContext(p);

DataSource ds = (DataSource) ctx.lookup("jdbc/MySQL");
//jdbc/MySQL is the jndi of datasource deployed in weblogic server
Connection con = ds.getConnection();

Statement stmt = con.createStatement();
java.sql.ResultSet rs = stmt.executeQuery("select * from emp");

while(rs.next())
{
out.println(rs.getString(1));
out.println(rs.getString(2));
}


} catch (NamingException ex) {
out.println(ex.toString());
} catch (Exception ex) {
out.println(ex.toString());
}

///////////////////////////////////////////

The above code is in a completely different machine.

Also I would like to know whether I can reference an object that is
in different JVM (Application Server) with a reference variable that
is in different JVM (other than where the object itself is) ???


Awaiting for responses.

Thanks in advance.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Ashwini,
Welcome to JavaRanch!

Yes, you can access a database on another machine. You can't reference objects in other JVMs though.

Two comments on your code:
1) You need to close the connection, statement and resultset in a finally block. Without that, there will be a resource leak.
2) I suggested using the column names instead of "select * ..." It's a good habit to get into. It is more robust for schema changes and if you have a lot of columns can be more efficient.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ashwini Sharma
Greenhorn

Joined: Nov 05, 2004
Posts: 4
Hi Jeanne ,

Thanks for the reply. I did understand your point regarding the closing of resultsets and using column names instead of "*" in a SQL statement.

But I would also like to know one more thing.

Please look at the following piece of code

/******************************************************************/
DataSource ds = (DataSource) ctx.lookup("jdbc/MySQL");
//jdbc/MySQL is the jndi of datasource deployed in weblogic server
Connection con = ds.getConnection();
/******************************************************************/

Here I am looking up a datasource object which resides in application
server. The application server runs in different JVM.
Then as you have told in the previous reply that we can't referece object in different JVM, how am i getting a reference of DataSource object in the client JVM???

Also if we look at the connection object, I am using
ds.getConnection()
to obtain the connection object.

Where is this connection object, is it in the Application server where the DataSource object is or is it in the client machine.
If it is in the application server then is it not again a remote reference???
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

I meant that in general you can't use objects in a different JVM. JNDI is an exception to the rule. When you look up something like a datasource or connection, you are requesting the object to be sent to your JVM.

(Sorry I didn't read the initial question carefully enough.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbc objects in different JVM's