SQLite总结

废话不多说:

优点,高并发读快速度读取超越所有主流大中型数据库

缺点,缺少同步机制,读写不能同时,且同时只能有一个写入线程

用途,硬盘式缓存

另附一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;
	}

}

  

转载于:https://www.cnblogs.com/swtjavaspace/p/6704119.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值