Menu

Database_creation

Luis M. Villa

MILK by Example

(Return to examples index)

In this section, you can find examples of how to use MILK database framework. I don't want to make you loose your time so, to the point:

Let's create our database !

First of all, we need to create a Db class, that represents a database in our project. In order to do this, just create a class that extends Db. The compiler forces us to implement createDataSource method (as its name says, it is the way we obtain the datasource to provide database connections):

package net.sourceforge.milk.examples;

import javax.sql.DataSource;

import net.sourceforge.milk.Db;

import org.apache.commons.dbcp.BasicDataSource;

public class IssuesDb extends Db {

    @Override
    protected DataSource createDataSource() {

        // For this example, we create a DataSource to a H2 test database

        final BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.h2.Driver");
        ds.setUrl("jdbc:h2:~/test");
        return ds;
    }
}

And we are done! we are now ready to NEXT: launch some queries >>.


Related

Wiki: Examples
Wiki: Launch_queries
Wiki: examples_toc

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.