SqliteHelper

package com.mapgis.cloud.util;


import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;
import org.sqlite.SQLiteOpenMode;


import java.io.File;
import java.sql.*;
import java.util.Objects;


public class SqliteStore {
private static String dbName = "virtualIGS.db";
   private static String dataDir = "data";
   private static int timeout = 10;
// private static int timeout = 1000;
static SQLiteConfig sqlConfig = new SQLiteConfig();


   private SqliteStore() {
dbName = dataDir + File.separator + dbName;
      try {
init();
} catch (Exception e) {
e.printStackTrace();
}
sqlConfig.setSynchronous(SQLiteConfig.SynchronousMode.OFF);
sqlConfig.setJournalMode(SQLiteConfig.JournalMode.WAL);
sqlConfig.setOpenMode(SQLiteOpenMode.READWRITE);
}


private static class DBStoreHolder {
static SqliteStore instance = new SqliteStore();
}


public static SqliteStore getInstance() {
return DBStoreHolder.instance;
}


public synchronized void init() {
File dbDir = new File(dataDir);
      if (!dbDir.exists()) {
dbDir.mkdirs();
}
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
   }


public Connection getConnection() {
Connection conn = null;
      try {
conn = DriverManager.getConnection("jdbc:sqlite:" + dbName);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}


public static int execute(String sql) {
SqliteStore store = SqliteStore.getInstance();
Connection conn = store.getConnection();
      int ret = store.execute(sql, conn);
store.close(conn);
      return ret;
}
public static int getIntResult(String sql){
SqliteStore store = SqliteStore.getInstance();
Connection conn = store.getConnection();
      int ret =  store.getIntResult(sql,conn);
store.close(conn);
      return ret;
}




public int execute(String sql, Connection connection) {
Statement statement;
      int result = 0;
      try {
statement = connection.createStatement();
statement.setQueryTimeout(timeout);
result = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}


/**
    * 执行SQL语句查询(手动关闭)
    *
    * @param sql
* @return
    */
public ResultSet query(String sql) {
ResultSet result = null;
      try {
SQLiteDataSource ds = new SQLiteDataSource(sqlConfig);
ds.setUrl("jdbc:sqlite:" + dbName);
Connection conn = ds.getConnection();
Statement statement = conn.createStatement();
statement.setQueryTimeout(timeout);
result = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}


public ResultSet query(String sql, Connection connection) {
Statement statement;
ResultSet result = null;
      try {
statement = connection.createStatement();
statement.setQueryTimeout(timeout);
result = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}


public Object getResult(String sql, Connection connection){
ResultSet set = query(sql,connection);
      try {
if(set.next()){
return set.getObject(1);
}
      } catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int getIntResult(String sql, Connection connection){
ResultSet set = query(sql,connection);
      try {
if(set.next()){
return set.getInt(1);
}
      } catch (SQLException e) {
e.printStackTrace();
}
return 0;
}


public int delete(String sql, Connection connection) {
Statement statement;
      int result = 0;
      try {
statement = connection.createStatement();
statement.setQueryTimeout(timeout);
result = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}


public void close(Connection conn) {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
      } catch (SQLException e) {
e.printStackTrace();
}
   }


/**
    * 查询后关闭连接
*
    * @param rlt
*/
public void closeRltSet(ResultSet rlt) {
try {
if (rlt != null) {
rlt.close();
rlt.getStatement().close();
rlt.getStatement().getConnection().close();
}
      } catch (SQLException e) {
e.printStackTrace();
}
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值