sqllite java 下載地址

https://bitbucket.org/xerial/sqlite-jdbc/overview


mportjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;publicclassSample{publicstaticvoidmain(String[]args)throwsClassNotFoundException{// load the sqlite-JDBC driver using the current class loaderClass.forName("org.sqlite.JDBC");Connectionconnection=null;try{// create a database connectionconnection=DriverManager.getConnection("jdbc:sqlite:sample.db");Statementstatement=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')");ResultSetrs=statement.executeQuery("select * from person");while(rs.next()){// read the result setSystem.out.println("name = "+rs.getString("name"));System.out.println("id = "+rs.getInt("id"));}}catch(SQLExceptione){// if the error message is "out of memory", // it probably means no database file is foundSystem.err.println(e.getMessage());}finally{try{if(connection!=null)connection.close();}catch(SQLExceptione){// connection close failed.System.err.println(e);}}}}

How to Specify Database Files

Connectionconnection=DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");

How to Use Memory Databases

Connectionconnection=DriverManager.getConnection("jdbc:sqlite::memory:");


Added READ_UNCOMMITTED mode support for better query performance: (see alsohttp://www.sqlite.org/sharedcache.html )

// READ_UNCOMMITTED mode works only in shared_cache mode.Propertiesprop=newProperties();prop.setProperty("shared_cache","true");Connectionconn=DriverManager.getConnection("jdbc:sqlite:",prop);conn.setTransactionIsolation(Conn.TRANSACTION_READ_UNCOMMITTED);

Using SQLiteJDBC with Maven2


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

Using SQLiteJDBC with Tomcat6 Web Server


Do not include sqlite-jdbc-(version).jar in WEB-INF/lib folder of your web application package, since multiple web applications hosted by the same Tomcat server cannot load the sqlite-jdbc native library more than once. That is the specification of JNI (Java Native Interface). You will observe UnsatisfiedLinkError exception with the message "no SQLite library found".

Work-around of this problem is to put sqlite-jdbc-(version).jar file into (TOMCAT_HOME)/libdirecotry, in which multiple web applications can share the same native library file (.dll, .jnilib, .so) extracted from this sqlite-jdbc jar file.

If you are using Maven for your web application, set the dependency scope as 'provided', and manually put the SQLite JDBC jar file into (TOMCAT_HOME)/lib folder.

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章