java Maven RESTfull Web 服务项目中的 SQLite
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25956126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite in maven RESTfull web services project
提问by ndKan
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!
我是编写 REST-full web 服务的新手。我正在做一个项目,我需要能够从我的服务器获取和发布一些数据。我的计划是创建 SQLite 数据库,但我没有在 Maven 中这样做的任何经验。此外,如果有任何其他(更简单的)方法来收集数据,我也会考虑它。任何帮助都会很棒!谢谢!
回答by Magnilex
In Java, you use a JDBCdriver 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.
在 Java 中,您使用JDBC驱动程序与数据库进行标准化通信。您选择使用 SQLLite 可能没问题(听起来您正在尝试学习基础 RESTful Web 服务)。对于“真正的”应用程序,您可能会选择其他一些数据库,例如 PostgreSQL 或 MySQL。
Xerials sqlite-jdbcseems to be a popular implementation of a JDBC driver for SQLite.
Xerials sqlite-jdbc似乎是 SQLite 的 JDBC 驱动程序的流行实现。
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:
使用 Maven,您需要做的就是向 pom.xml 添加依赖项。然后 Maven 将下载 jar 和任何必要的依赖项,并允许您在应用程序中使用它:
<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:
有关如何设置连接和对数据库运行查询的示例,Xerial sqlite-jdbc 主页上的示例示例似乎是最好的起点:
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);
}
}
}
}