Results 1 to 5 of 5
Thread: DriverManager vs. DataSource
- 02-04-2009, 10:45 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
DriverManager vs. DataSource
Hello,
I am using Eclipse 3.4.2 and Java 1.6. I have a Apache Derby database installed. I try to learn using this by following the JDBC Database Access trail in the Sun Java Tutorials.
I can make a connection to the database using DriverManager.getConnection, this works fine. However, the tutorial states that using the DataSource interface is the preferred method. So I try that also. Unfortunately, the tutorial only gives this example:
My questions:Java Code:InitialContext ic = new InitialContext() DataSource ds = ic.lookup("java:comp/env/jdbc/myDB"); Connection con = ds.getConnection(); DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource() ds.setPort(1527); ds.setHost("localhost"); ds.setUser("APP") ds.setPassword("APP"); Connection con = ds.getConnection();
1) ds and con are declared twice. Can it be that two examples have been merged inadvertedly?
2) the methods setPort ... are not defined for DataSource. Indeed, the tutorial goes on to say that "DataSource implementations must provide getter and setter methods for each property they support." Is this something that has to be done outside my program, e.g. with DTP?
3) Does anyone know a book or tutorial where DataSource is better explained?
Thanks in advance, DriesKn.
- 02-04-2009, 10:59 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
above code should be a example of dsJava Code:InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); Connection con = ds.getConnection();
other setting for jdbc resource and connection pool should set in J2EE server
- 02-04-2009, 11:05 AM #3
1) Obviously few samples are merged.
2) Second DataSource actually is of org.apache.derby.jdbc.ClientDataSource type and therefore has setPort() method.
3) I find javadoc explanation pretty good. DataSource (Java 2 Platform SE v1.4.2)
- 02-04-2009, 01:38 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
The statement
looks like a method invocation, but org.apache.derby.jdbc.ClientDataSource is a class. I expect that I have to use some static method of this class to give me a reference to the data source, but there isn't such a method. I can't find setPort either, for that matter. I ran the ClassSpy ( in the Reflection part of the Java Tutorials) program on this class.Java Code:DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource();
So creating an object byand trying to invoke ds.getPort() doesn't work.Java Code:DataSource ds = new ClientDataSource();
drieskn
- 02-05-2009, 04:38 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i dont know why Tutorial page code like that...
have you check that
from ClientDataSource (Apache Derby 10.5 API Documentation)Java Code:Use these DataSources if your application runs under JDBC3.0 or JDBC2.0, that is, on the following Java Virtual Machines: JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0 JDBC 2.0 - Java 2 - JDK 1.2,1.3
Similar Threads
-
JasperReports dataSource.
By hendrix79 in forum Advanced JavaReplies: 0Last Post: 01-29-2009, 10:54 AM -
Specify SSL Context to DriverManager while getting SQL connection
By dwell4lov in forum JDBCReplies: 1Last Post: 10-20-2008, 06:29 AM -
Datasource starts after web application
By anosh_nathaniel in forum XMLReplies: 0Last Post: 12-12-2007, 10:49 AM -
Cannot set DataSource for RowSet
By piterskiy in forum JDBCReplies: 0Last Post: 12-07-2007, 07:58 PM -
Datasource
By samson in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-02-2007, 11:06 AM


LinkBack URL
About LinkBacks

Bookmarks