Search code examples
javajdbcderby

Why does datasource work correctly without a URL to the database?


I want to know why I did not need to specify a URL for a datasource object.

This is maybe the other way round since most questions here seem to be why something did not work, but in this case, it did work, but I don't understand why.

I wrote this code:

import javax.sql.DataSource;
import org.apache.derby.jdbc.ClientDataSource;
...

public class DataSourceFactory {

private static final transient Logger scribe = Logger.getLogger(MyUI.class.getName());

public static DataSource getDataSource() throws Exception {
    ClientDataSource ds = null;
    try {
        ds = new ClientDataSource();
        ds.setDatabaseName("capitalism");
        ds.setUser("alan");
        ds.setPassword("insecure");
    } catch (Exception e) {
        scribe.log(Level.SEVERE, "Mr Developer, you messed up", e);
    }
    return ds;
}

}

then I used the new class thus, expecting something to go wrong:

public class MyUI extends UI {

    transient private FileHandler handler;
    private static final transient Logger scribe = Logger.getLogger(MyUI.class.getName());
    transient private DataSource ds;
    transient private Connection conn;

    protected void init(VaadinRequest vaadinRequest) {

    ...                
    try {
                ds = DataSourceFactory.getDataSource();
            } catch (Exception ex) {
                scribe.log(Level.SEVERE, "DataSource Didn't work", ex);
            }
            try {
                conn = ds.getConnection();
            } catch (SQLException ex) {
                scribe.log(Level.SEVERE, "Connection Didn't work", ex);
            }
            try {
                statement = conn.createStatement();
            } catch (SQLException ex) {
                scribe.log(Level.SEVERE, "Statement creation didn't work", ex);
            }
            String createSQL = "CREATE TABLE MYTABLE (FIRSTTHING VARCHAR(20), SECONDTHING VARCHAR(20))";
            try {
                statement.executeUpdate(createSQL);
            } catch (SQLException ex) {
                scribe.log(Level.SEVERE, "Statement didn't work", ex);
            }

But it didn't go wrong. It worked first time.

Before I started working on datasources, ie when I just used DriverManager, I could not establish a connection without the URL of the database. I was expecting to go through a long learning curve finding out how to get the DataSource to recognise the database, register it with JNDI and all the stuff the tutorials tell you about eg http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

Instead, it just found it.

I was working with NetBeans 8.0.1 and I created the 'capitalism' database as an embedded JDBC database in it. For what it's worth, the properties dialog of this database says the URL is jdbc:derby://localhost:1527/capitalism [alan on ALAN] and it was running when I executed the code. The only other relevant piece of information I can think of is that this was running in a vaadin project.

I suppose it's an unusual question to ask why something worked, but I would like to know because if I don't know why it worked, I can't reliably transfer it to a working context.


Solution

  • Most database-system-specific DataSource implementations default to localhost and the default port of that database system. You don't need to set the JDBC url, as those datasources are configured using properties for individual elements like hostname, databasename, etc (they usually don't even have a property for the entire url!).

    So if your database is running on localhost, you only need to set the databasename, username and password, and the datasource will know how to connect.