|
SQLite嵌入式数据库:
官网:http://www.sqlite.org/index.html
SQLite介绍:http://baike.baidu.com/view/19310.htm
SQLite下载地址(本人下载的是Mac os版本,它支持windows、Linux、window phone 8):
http://www.sqlite.org/download.html
Mac os下操作(http://www.sqlite.org/sqlite.html):
用java操作前需要驱动包sqllite-jdbc-3.7.2.jar包,这是当前最高版本。
下载地址:http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/
实例代码:
package com.dwen.sqlite;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* SQLite 嵌入式小型数据库,由C语言实现。 适应小型应用开发,它有占用内存小、轻量、零配置、速度快等特点。
*
* @author Dwen
* @version v 0.1 2013-9-23 上午09:57:40
*/
public class SqliteTest {
/**
* 入口
*
* @param args
*/
public static void main(String[] args) {
try {
//连接sqlite JDBC
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager
.getConnection("jdbc:sqlite:/Users/b/Desktop/git/ex1.db");
// 事务,禁止自动提交,设置回滚点
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
//创建表
statement.executeUpdate("create table tbl4(id integer primary key autoincrement,name varchar(20),age smallint);");
//插入数据
statement.executeUpdate("insert into tbl4 values(null,'dwen',26);");
statement.executeUpdate("insert into tbl4 values(null,'test',22);");
conn.commit();// 提交事务
// 查询sql
ResultSet rs = statement.executeQuery("select * from tbl4;");
while (rs.next()) {
System.out.println("id = " + rs.getInt("id"));
System.out.println("name = " + rs.getString("name"));
System.out.println("age = " + rs.getInt("age"));
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
|
|
|