ndKan ndKan - 1 year ago 210
Java Question

SQLite in maven RESTfull web services project

I am new in programming RESTfull web services. I am working on a project where I need to be able to GET and POST some data from and to my server. My plan is to create SQLite database but I don't have any experience in doing so in Maven. Also, if there is any other (easier) way to collect the data I would consider it as well. Any help would be great! Thanks!

Answer Source

In Java, you use a JDBC driver for standardized communication with a database. Your choice to use SQLLite is probably OK (it sounds like you are trying to learn basics RESTful webservices). For a "real" application you would probably pick some other database like PostgreSQL or MySQL.

Xerials sqlite-jdbc seems to be a popular implementation of a JDBC driver for SQLite.

With Maven, all you need to do is to add a dependency to your pom.xml. Maven will then download the jar, and any necessary dependencies and allow you to use it in your application:


For an example on how to set up a connection and run queries against the database, the sample example at the Xerial sqlite-jdbc homepage seems like the best of starting points:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sample
  public static void main(String[] args) throws ClassNotFoundException
    // load the sqlite-JDBC driver using the current class loader

    Connection connection = null;
      // create a database connection
      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
      Statement statement = connection.createStatement();
      statement.setQueryTimeout(30);  // set timeout to 30 sec.

      statement.executeUpdate("drop table if exists person");
      statement.executeUpdate("create table person (id integer, name string)");
      statement.executeUpdate("insert into person values(1, 'leo')");
      statement.executeUpdate("insert into person values(2, 'yui')");
      ResultSet rs = statement.executeQuery("select * from person");
        // read the result set
        System.out.println("name = " + rs.getString("name"));
        System.out.println("id = " + rs.getInt("id"));
    catch(SQLException e)
      // if the error message is "out of memory", 
      // it probably means no database file is found
        if(connection != null)
      catch(SQLException e)
        // connection close failed.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download