Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite in maven RESTfull web services project [closed]

I am new in programming REST-full 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!

like image 603
ndKan Avatar asked Sep 21 '14 05:09

ndKan


1 Answers

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:

<dependencies>
    <dependency>
      <groupId>org.xerial</groupId>
      <artifactId>sqlite-jdbc</artifactId>
      <version>3.7.2</version>
    </dependency>
</dependencies>

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
    Class.forName("org.sqlite.JDBC");

    Connection connection = null;
    try
    {
      // 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");
      while(rs.next())
      {
        // 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
      System.err.println(e.getMessage());
    }
    finally
    {
      try
      {
        if(connection != null)
          connection.close();
      }
      catch(SQLException e)
      {
        // connection close failed.
        System.err.println(e);
      }
    }
  }
}
like image 124
Magnilex Avatar answered Nov 18 '22 18:11

Magnilex