废话不多说:
优点,高并发读快速度读取超越所有主流大中型数据库
缺点,缺少同步机制,读写不能同时,且同时只能有一个写入线程
用途,硬盘式缓存
另附一SQLite工具类:
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;
public class SQLiteHelper {
private static final String dbDriver = "org.sqlite.JDBC";
public static int createTable(String dbUrl, String sql) {
try {
if (sql == null || sql.trim().length() < 1)
return -1;
Class.forName(dbDriver);
if (dbUrl.indexOf("jdbc:sqlite:") != 0) {
dbUrl = "jdbc:sqlite:" + dbUrl;
}
if (!dbUrl.endsWith(".db")) {
dbUrl = dbUrl + ".db";
}
Connection con = DriverManager.getConnection(dbUrl);
con.setAutoCommit(true);
System.out.println(sql);
FileWriter wr = new FileWriter("create.sql", true);
wr.append(sql + "\n");
wr.close();
con.close();
return con.prepareStatement(sql).executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public static int excuteUpdate(String dbUrl, String sql, Object[]... params) {
try {
if (sql == null || sql.trim().length() < 1)
return -1;
Class.forName(dbDriver);
if (dbUrl.indexOf("jdbc:sqlite:") != 0) {
dbUrl = "jdbc:sqlite:" + dbUrl;
}
if (!dbUrl.endsWith(".db")) {
dbUrl = dbUrl + ".db";
}
Connection con = DriverManager.getConnection(dbUrl);
con.setAutoCommit(false);
Object[] param = null;
PreparedStatement ps = con.prepareStatement(sql);
for (int i = 0; i < params.length;) {
if (params != null) {
param = params[i];
for (int j = 1; j <= param.length;) {
if (param[j - 1] == null) {
continue;
}
ps.setObject(j, param);
j++;
}
}
ps.addBatch();
i++;
}
int num = ps.executeUpdate();
ps.clearBatch();
con.commit();
con.close();
return num;
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public static List<Map<String, Object>> readDb(String dbUrl, String sql, Object... param) {
try {
if (sql == null || sql.trim().length() < 1)
return null;
if (dbUrl.indexOf("jdbc:sqlite:") != 0) {
dbUrl = "jdbc:sqlite:" + dbUrl;
}
if (!dbUrl.endsWith(".db")) {
dbUrl = dbUrl + ".db";
}
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbUrl);
PreparedStatement ps = con.prepareStatement(sql);
for (int j = 1; j <= param.length;) {
if (param[j - 1] == null) {
continue;
}
ps.setObject(j, param);
j++;
}
con.close();
return new ListResult(ps.executeQuery()).getListRestlt();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}