sqlite3在java 中的使用经验

[color=red][/color][color=orange][/color][size=medium][/size][align=left][/align]
我写了一个测试例子,如果是java,使用jdbc挺好。
如果是VC.net,可以使用它的ado.net包。都挺容易的。
以windows下jdbc为例:
下载sqlite的dll(www.sqlite.org)并
下载jdbc包:http://www.ch-werner.de/javasqlite/javasqlite-20050608-win32.zip
把解压后的文件放到jre目录下边,
下边是测试的代码, 测试结果见http://ehero.mblogger.cn/posts/40583.aspx

以下是测试代码:
import java.sql.*;

public class TestConn {
void test(){
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
System.out.println(new java.util.Date());
try {
Class.forName("SQLite.JDBCDriver");
conn = DriverManager.getConnection(
"jdbc:sqlite:/f:/test.db", //jdbc:oracle:thin:@hex:1521:ORCL
"",
"");
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate("create table hehe(id number, name varchar(32))");
System.out.println("建表hehe成功!");
for (int i=0; i<1000000; i++)
{
stmt.executeUpdate("INSERT INTO hehe VALUES("+i+", '我爱中国"+i+"')");

}

conn.commit();
System.out.println("不建索引查询:");
System.out.println(new java.util.Date());

rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 499999 and name ='我爱中国500001'");
if (rset.next()){
System.out.println(rset.getInt("id"));
System.out.println(rset.getString("name"));
}
if (rset!=null){ rset.close(); rset = null; }

System.out.println(new java.util.Date());

System.out.println("建索引:");
System.out.println(new java.util.Date());
stmt.executeUpdate("CREATE INDEX hehe_idx on hehe(id)");
stmt.executeUpdate("CREATE INDEX hehe_idx2 on hehe(name)");
conn.commit();
System.out.println(new java.util.Date());

System.out.println("建索引后的查询:");
System.out.println(new java.util.Date());
rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 499999 and name = '我爱中国500001'");
if (rset.next()){
System.out.println(rset.getInt("id"));
System.out.println(rset.getString("name"));
}
System.out.println(new java.util.Date());
stmt.executeUpdate("drop table hehe");
System.out.println("删除表hehe成功!");
conn.commit();

System.out.println(new java.util.Date());

} catch(ClassNotFoundException cnfe) {
System.out.println("Can't find class for driver: "+cnfe.getMessage());
System.exit(-1);
} catch (SQLException e){
System.out.println(e.getMessage());
System.exit(-1);
} finally
{
try {
if (rset!=null) rset.close();
stmt.close();
conn.close();
} catch (SQLException e) {

}
}
}

public static void main(String[] args) {
TestConn conn = new TestConn();
conn.test();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值