日期:2014-05-16 浏览次数:20590 次
首发地址:http://inmethetiger.iteye.com/blog/1680669
在bdb中的原生态包中并不包含对SQLite的支持。但是提供了一个jdbc.jar包,这个包能像访问SQLite数据库一样使用bdb的数据库
SQLite连接数据库的方法
需要sqlite-jdbc.jar包
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class SQLiteT { public static void main(String[] args) throws SQLException, ClassNotFoundException { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:geoportal.db","root","root"); Statement stat = conn.createStatement(); stat.executeUpdate("create table tbl1(name varchar(20), salary int);");// 创建一个表,两列 stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);");// 插入数据 stat.executeUpdate("insert into tbl1 values('LiSi',7800);"); stat.executeUpdate("insert into tbl1 values('WangWu',5800);"); stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);"); ResultSet rs = stat.executeQuery("select * from tbl1;");// 查询数据 while (rs.next()) {// 将查询到的数据打印出来 System.out.print("name = " + rs.getString("name") + " ");// 列属性一 System.out.println("salary = " + rs.getString("salary"));// 列属性二 } } }
?
?使用bdb原生态api创建数据库的方法:
需要db.jar
?
DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); if (create) { dbConfig.setAllowCreate(true); dbConfig.setType(DatabaseType.BTREE); } // catalog is needed for serial bindings (java serialization) Database catalogDb = env.openDatabase(null, "catalog", null, dbConfig); catalog = new StoredClassCatalog(catalogDb); // use Integer tuple binding for key entries TupleBinding keyBinding = TupleBinding.getPrimitiveBinding(Integer.class); // use String serial binding for data entries SerialBinding dataBinding = new SerialBinding(catalog, String.class); this.db = env.openDatabase(null, "helloworld", null, dbConfig); // create a map view of the database this.map = new StoredSortedMap(db, keyBinding, dataBinding, true);
?
?
在bdb中使用SQLite的方法
需要bdb中下载的jar包中的jdbc.jar
?
package db.sqllitedb; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class BDB_SQLlite { public static void main(String[] args) { Connection conn = null; SQLite.Database db = null; try { Class.forName("SQLite.JDBCDriver").newInstance(); conn = DriverManager.getConnection("jdbc:sqlite:/blabla.db"); java.lang.reflect.Method m = conn.getClass().getMethod( "getSQLiteDatabase", null); db = (SQLite.Database) m.invoke(conn, null); System.out.println(db.version()); Statement stat = conn.createStatement(); stat.executeUpdate("create table tbl1(name varchar(20), salary int);");// 创建一个表,两列 stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);");// 插入数据 stat.executeUpdate("insert into tbl1 values('LiSi',7800);"); stat.executeUpdate("insert into tbl1 values('WangWu',5800);"); stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);"); ResultSet rs = stat.executeQuery("select * from tbl1;");// 查询数据 while (rs.next()) {// 将查询到的数据打印出来 System.out.print("name = " + rs.getString("name") + " ");// 列属性一 System.out.println("salary = " + rs.getString("salary"));// 列属性二 } } catch (Exception e) { e.printStackTrace(); } } }
?
其中访问SQLite的方法类似。最重要的是驱动包的加载方式。