Java maven RESTfull web服务项目中的SQLite

Java maven RESTfull web服务项目中的SQLite,java,sqlite,maven,web,Java,Sqlite,Maven,Web,我是编程REST完整web服务的新手。我正在从事一个项目,我需要能够从我的服务器获取和发布一些数据。我的计划是创建SQLite数据库,但我没有在Maven中这样做的经验。此外,如果有任何其他(更容易)收集数据的方法,我也会考虑。任何帮助都会很好!谢谢 在Java中,使用驱动程序与数据库进行标准化通信。您选择使用SQLLite可能还可以(听起来您正在尝试学习基本的RESTful Web服务)。对于“真正的”应用程序,您可能会选择其他数据库,如PostgreSQL或MySQL Xerials似乎是S

我是编程REST完整web服务的新手。我正在从事一个项目,我需要能够从我的服务器获取和发布一些数据。我的计划是创建SQLite数据库,但我没有在Maven中这样做的经验。此外,如果有任何其他(更容易)收集数据的方法,我也会考虑。任何帮助都会很好!谢谢

在Java中,使用驱动程序与数据库进行标准化通信。您选择使用SQLLite可能还可以(听起来您正在尝试学习基本的RESTful Web服务)。对于“真正的”应用程序,您可能会选择其他数据库,如PostgreSQL或MySQL

Xerials似乎是SQLite JDBC驱动程序的流行实现

使用Maven,只需向pom.xml添加依赖项。Maven随后将下载jar和任何必要的依赖项,并允许您在应用程序中使用它:

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

SQLite和Maven之间并不完全相关——一个是平面文件数据库,另一个是项目管理工具。您可能需要使用第三方库,该库允许您写入SQLite,Maven可以将其拉下来,但这就是Maven的工作范围。我要找的是@Makoto哪个库?你能给我一些提示或者指向一些有用的教程吗?我应该把sample.db文件放在哪里?在我的项目根?@pablobaldez中,这已经超出了这个问题的范围。这个答案指向SQLLiteJDBC实现。您必须查阅文档以了解如何设置它。@pablobaldez是的,您的项目根目录。如果不存在这样的文件,SQLite将创建一个具有给定名称的文件
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);
      }
    }
  }
}