Results 1 to 5 of 5
  1. #1
    drieskn is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default 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:
    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();
    My questions:
    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.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup(dbName);
    Connection 	con = ds.getConnection();
    above code should be a example of ds
    other setting for jdbc resource and connection pool should set in J2EE server

  3. #3
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    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)

  4. #4
    drieskn is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    The statement
    Java Code:
    DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource();
    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.
    So creating an object by
    Java Code:
    DataSource ds = new ClientDataSource();
    and trying to invoke ds.getPort() doesn't work.

    drieskn
    Attached Files Attached Files

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    i dont know why Tutorial page code like that...

    have you check that
    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
    from ClientDataSource (Apache Derby 10.5 API Documentation)

Similar Threads

  1. JasperReports dataSource.
    By hendrix79 in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2009, 11:54 AM
  2. Replies: 1
    Last Post: 10-20-2008, 07:29 AM
  3. Datasource starts after web application
    By anosh_nathaniel in forum XML
    Replies: 0
    Last Post: 12-12-2007, 11:49 AM
  4. Cannot set DataSource for RowSet
    By piterskiy in forum JDBC
    Replies: 0
    Last Post: 12-07-2007, 08:58 PM
  5. Datasource
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-02-2007, 12:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •