java 如何在 Tomcat 6 中配置 SQLite?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4279632/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:39:55  来源:igfitidea点击:

How to configure SQLite in Tomcat 6?

javasqlitetomcatjdbc

提问by DG.

Can you please provide the steps on how to use sqlite in tomcat 6? I am using Xerial sqlite jdbc driver. In my application, I have got multiple sqlite databases (.db files) and would need to connect to a different sqlite database depending on what user logs in ? Where can I put all the .db files - with in the webapp root's directory or any where on the system or with in WEB-INF?

您能否提供有关如何在 tomcat 6 中使用 sqlite 的步骤?我正在使用 Xerial sqlite jdbc 驱动程序。在我的应用程序中,我有多个 sqlite 数据库(.db 文件)并且需要根据用户登录的内容连接到不同的 sqlite 数据库?我可以将所有 .db 文件放在哪里 - 在 webapp 根目录中或系统上的任何位置或在 WEB-INF 中?

Thanks,

谢谢,

Deep

深的

回答by MattC

I just went through configuring sqlite3 with Tomcat 7. Everything is working now, so thought I'd share my setup.
- Download the JDBC driver (org.sqlite.JDBC) that lives in sqlite-jdbc-3.7.2.jar (or whatever the latest version is). https://bitbucket.org/xerial/sqlite-jdbc/downloads
and copy it to yourTomcat/lib
- You can copy the sqlite db anywhere you want to. For my setup, I created a 'dbs' directory under my tomcat install, and put it there.

我刚刚使用 Tomcat 7 配置了 sqlite3。现在一切正常,所以我想我会分享我的设置。
- 下载位于 sqlite-jdbc-3.7.2.jar(或任何最新版本)中的 JDBC 驱动程序 (org.sqlite.JDBC)。https://bitbucket.org/xerial/sqlite-jdbc/downloads
并将其复制到 yourTomcat/lib
- 您可以将 sqlite 数据库复制到您想要的任何位置。对于我的设置,我在我的 tomcat 安装下创建了一个“dbs”目录,并将其放在那里。

Now set up your app. If you don't have a META-INF/context.xml file, then create one. This is a minimal file:

现在设置您的应用程序。如果您没有 META-INF/context.xml 文件,则创建一个。这是一个最小的文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context>  
  <Resource name="jdbc/yourdb" 
            auth="Container" 
            type="javax.sql.DataSource" 
            driverClassName="org.sqlite.JDBC"
            url="jdbc:sqlite:/${catalina.home}/dbs/yourDB.db"
            factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory">
  </Resource>
</Context>

Then add the following to WEB-INF/web.xml:

然后将以下内容添加到 WEB-INF/web.xml 中:

<resource-ref>
    <description>Reviews Database</description>
    <res-ref-name>jdbc/yourdb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

At this point, you should be good to go. Here is some sample code for accessing the database (I have a table 'admin' with a column 'username'):

此时,您应该可以开始了。下面是一些用于访问数据库的示例代码(我有一个表 'admin' 与一个列 'username'):

public String getName() {
    LOG.info("getting name : " + this.name);
    try {
          Context ctx = new InitialContext();
          DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/yourdb");
          Connection conn = ds.getConnection();
          Statement stat = conn.createStatement();
          ResultSet rs = stat.executeQuery("select username from admin");
          this.name = rs.getString(1);
    } catch (SQLException se) {
          LOG.info(se.toString());
    } catch (NamingException ne) {
          LOG.info(ne.toString());
    }
    return this.name;
}

Note: some distros of tomcat don't come with tomcat.dbcp by default, if you run into problems it may be easier to reference the dbcp class that comes with commons, org.apache.commons.dbcp.BasicDataSourceFactory. I had this problem with tomcat.dbcp not included in my tomcat7 installation and once I switched the reference in context.xml everything was working fine.

注意:默认情况下,tomcat 的某些发行版不附带 tomcat.dbcp,如果遇到问题,可能更容易引用 commons 附带的 dbcp 类 org.apache.commons.dbcp.BasicDataSourceFactory。我的 tomcat.dbcp 没有包含在我的 tomcat7 安装中,我遇到了这个问题,一旦我切换了 context.xml 中的引用,一切都正常。

回答by Buhake Sindi

What we did is pretty similar. Unfortunately you cannot create a SQLite Connection pool on Tomcat as SQLite has a database file for each user.

我们所做的非常相似。不幸的是,您无法在 Tomcat 上创建 SQLite 连接池,因为 SQLite 为每个用户都有一个数据库文件。

Just copy the jar file in TOMCAT_HOME/libfolder but you cannot call a connection via JNDI. You will have to do something like this:

只需将 jar 文件复制到文件TOMCAT_HOME/lib夹中,但您无法通过 JNDI 调用连接。你将不得不做这样的事情:

/**
     * 
     * @param driverClassName
     * @param url
     * @param user
     * @param password
     * @throws SQLException 
     * @throws Exception 
     */
    public DefaultJdbcTransaction(String driverClassName, String url, String user, String password) throws SQLException {
        super();
        // TODO Auto-generated constructor stub
        try {
            Class.forName(driverClassName).newInstance();

            if (user == null && password == null) {
                connection = DriverManager.getConnection(url);
            } else {
                connection = DriverManager.getConnection(url, user, password);
            }
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            throw new SQLException(e);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            throw new SQLException(e);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            throw new SQLException(e);
        }
    }

Where url="jdbc:sqlite:/path/to/sqlite/file/userId.db", driverClassName="org.sqlite.JDBC", and (user = password = null).

其中url="jdbc:sqlite:/path/to/sqlite/file/userId.db"driverClassName="org.sqlite.JDBC"、 和 ( user = password = null)。

I'm using sqlitejdbc-v056.jar.

我正在使用sqlitejdbc-v056.jar.

Hope this helps

希望这可以帮助