Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jdbc objects in different JVM's

 
Ashwini Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwini Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic