1.sqliteJDBC
sqliteJDBCDriver可以在这个网站下载https://bitbucket.org/xerial/sqlite-jdbc/overview,当前稳定版本sqlite-jdbc-3.7.2.jar
2. Java 代码
添加sqlite-jdbc-3.7.2.jar,与你添加其他jar包的方法一样。
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.ResultSet;
- importjava.sql.sqlException;
- importjava.sql.Statement;
- publicclasssqliteTest
- {
- staticvoidmain(String[]args)throwsClassNotFoundException
- //loadthesqlite-JDBCdriverusingthecurrentclassloader
- Class.forName("org.sqlite.JDBC");
- Connectionconnection=null;
- try
- //createadatabaseconnection
- connection=DriverManager.getConnection("jdbc:sqlite:sample.db");
- Statementstatement=connection.createStatement();
- statement.setQueryTimeout(30);//settimeoutto30sec.
- statement.executeUpdate("droptableifexistsperson");
- statement.executeUpdate("createtableperson(idinteger,namestring)");
- statement.executeUpdate("insertintopersonvalues(1,'leo')");
- statement.executeUpdate("insertintopersonvalues(2,'yui')");
- ResultSetrs=statement.executeQuery("select*fromperson");
- while(rs.next())
- //readtheresultset
- System.out.println("name="+rs.getString("name"));
- System.out.println("id="+rs.getInt("id"));
- }
- }
- catch(sqlExceptione)
- {
- //iftheerrormessageis"outofmemory",
- //itprobablymeansnodatabasefileisfound
- System.err.println(e.getMessage());
- finally
- try
- if(connection!=null)
- connection.close();
- catch(sqlExceptione)
- //connectioncloseFailed.
- System.err.println(e);
- }