How to configure SQLite in Tomcat 6?

Can you provide instructions for using sqlite in tomcat 6? I am using the Xerial sqlite jdbc driver. In my application, I have several sqlite databases (.db files) and you will need to connect to another sqlite database depending on what user is logging into? Where can I put all .db files - in the root directory of webapp or anywhere in the system or using WEB-INF?

Thank,

Deep

+3
source share
2 answers

What we did is very similar. Unfortunately, you cannot create a SQLite connection pool on Tomcat, because SQLite has a database file for each user.

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);
        }
    }

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

sqlitejdbc-v056.jar.

,

+5

sqlite3 Tomcat 7. , .
- JDBC (org.sqlite.JDBC), sqlite-jdbc-3.7.2.jar( ). https://bitbucket.org/xerial/sqlite-jdbc/downloads yourTomcat/lib
- sqlite db . "dbs" tomcat .

. 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>

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>

. ( "admin" "name" ):

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;
}

. tomcat tomcat.dbcp , , dbcp, commons, org.apache.commons.dbcp.BasicDataSourceFactory. tomcat.dbcp, tomcat7, context.xml, .

+7

Source: https://habr.com/ru/post/1776934/


All Articles