`
qindongliang1922
  • 浏览: 2147914 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:116342
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:124604
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:58481
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:70362
社区版块
存档分类
最新评论

JDBC操作SQLite数据库

    博客分类:
  • JAVA
阅读更多
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至今已经有14个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

上面的这段话,来自百科,介绍的还算是比较详细,SQLite作为一款轻量级的嵌入式数据库,体积非常小,而且非常省内存,也可以说它是一个内存库,但是它也有持久化的功能,它生生成的文件以db结尾。

下面来看下如何使用它,sqlite是轻量级的数据库,所以在程序中使用时,无须安装,无须下载,只需要下载sqlite的jdbc驱动包,就相当于拥有一个数据库了,而且,完全支持SQL语法,非常强大。代码如下:
package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 
 * 测试SQLite的使用
 * 
 * **/
public class Test {

	public static void main(String[] args) throws Exception{
		m();
	}
	public static void m() throws Exception{
	 Class.forName("org.sqlite.JDBC");   
	 Connection connection = null;  
	    try  
	    {  
	      // create a database connection  
	      connection = DriverManager.getConnection("jdbc:sqlite:sample.db");  
	      Statement statement = 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, '我是第一个学生')");  
	      statement.executeUpdate("insert into person values(2, '中国人')");  
	      statement.executeUpdate("insert into person values(45, '外国人')");  
	      statement.executeUpdate("insert into person values(4, '中国人')");  
	      ResultSet rs = statement.executeQuery("select *   from person order by id "); 
	    System.out.println("打印所有:");
	      while(rs.next())  
	      {  
	        // read the result set  
	       System.out.println("id: "+rs.getInt("id")+"   name: " + rs.getString("name"));  
	        
	      }  
	    }  
	    catch(SQLException e)  
	    { 
	      System.err.println(e.getMessage());  
	    }  
	    finally  
	    {  
	      try  
	      {  
	        if(connection != null)  
	          connection.close();  
	      }  
	      catch(SQLException e)  
	      {  
	        // connection close failed.  
	        System.err.println(e);  
	      }  
	    }  
	  }  

	
}



运行结果如下所示:

打印所有:
id: 1   name: 我是第一个学生
id: 2   name: 中国人
id: 4   name: 中国人
id: 45   name: 外国人



非常简单,方便,易用,参考资料:http://baike.baidu.com/view/19310.htm?fr=aladdin
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics