java通过JDBC访问Sqlite的实现实例

在代码开始之前,需要先引入依赖包:

maven引入依赖:

<dependency>
	<groupId>org.xerial</groupId>
	<artifactId>sqlite-jdbc</artifactId>
	<version>3.7.2</version>
	<type>jar</type>
	<scope>compile</scope>
</dependency>
如果不用Maven的话,得想办法下载sqlite-jdbc的jar包了!


以下是实现代码:

package com.ipeaksoft.idioms.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ipeaksoft.idioms.constant.SettingMap;
import com.ipeaksoft.idioms.entity.Idiom;

public class Sqlite3Util {

	public static Connection connection;
	
	public static Connection getConnection(){
		
		if(connection!=null){
			return connection;
		}
		
		try {
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager.getConnection("jdbc:sqlite:"+ SettingMap.getSetting(SettingMap.KEY_DB3_PATH));
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			connection = null;
		}
		
		return connection;
	}
	
	private static final String savesql = "insert into t_idioms(idiom, description, level, image, difficulty, pinyin, sortOrder) values(?, ?, ?, ?, ?, ?, ?)";
	
	private static final String updatesql = "update t_idioms set idiom=?, description=?, level=?, image=?, difficulty=?, pinyin=?, sortOrder=? where id=?";
	
	/**
	 * 保存
	 * @param idiom
	 * @return
	 * @throws SQLException 
	 */
	public static Idiom save(Idiom idiom) throws SQLException{
		
		Connection connection = getConnection();
		
		PreparedStatement prep = connection.prepareStatement(savesql);
		
		prep.setString(1, idiom.getIdiom());
		prep.setString(2, idiom.getDescription());
		prep.setInt(3, idiom.getLevel());
		prep.setBytes(4, idiom.getImage());
		prep.setInt(5, idiom.getDifficulty());
		prep.setString(6, idiom.getPinyin());
		prep.setInt(7, idiom.getSortOrder());
		prep.addBatch();
		
		connection.setAutoCommit(false);
		prep.executeBatch();
		connection.setAutoCommit(true);
		
		return idiom;
	}

	/**
	 * 修改
	 * @param idiom
	 * @throws SQLException
	 */
	public static void update(Idiom idiom) throws SQLException {
		Connection connection = getConnection();
		
		PreparedStatement prep = connection.prepareStatement(updatesql);
		
		prep.setString(1, idiom.getIdiom());
		prep.setString(2, idiom.getDescription());
		prep.setInt(3, idiom.getLevel());
		prep.setBytes(4, idiom.getImage());
		prep.setInt(5, idiom.getDifficulty());
		prep.setString(6, idiom.getPinyin());
		prep.setInt(7, idiom.getSortOrder());
		prep.setInt(8, idiom.getId());
		
		connection.setAutoCommit(false);
		prep.executeUpdate();
		connection.setAutoCommit(true);
	}
	
	/**
	 * 列表
	 * @param sql
	 * @return
	 * @throws SQLException 
	 */
	public static List<Idiom> list(String sql) throws SQLException{

		Connection connection = getConnection();
		Statement stat = connection.createStatement();
		ResultSet rs = stat.executeQuery(sql);
		
		List<Idiom> idioms = new ArrayList<>();
		
		Idiom idiom;
		while (rs.next()) {
			idiom = new Idiom();
			readResult(rs, idiom);
			idioms.add(idiom);
		}
		rs.close();
		
		return idioms;
	}
	
	/**
	 * 获取
	 * @param sql
	 * @return
	 * @throws SQLException 
	 */
	public static Idiom get(int id) throws SQLException{

		Connection connection = getConnection();
		Statement stat = connection.createStatement();
		ResultSet rs = stat.executeQuery("select * from t_idioms where id = " + id);
		
		Idiom idiom;
		if (rs.next()) {
			idiom = new Idiom();
			readResult(rs, idiom);
		}else{
			idiom = null;
		}
		rs.close();
		
		return idiom;
	}

	private static void readResult(ResultSet rs, Idiom idiom) throws SQLException {
		idiom.setId(rs.getInt("id"));
		idiom.setIdiom(rs.getString("idiom"));
		idiom.setPinyin(rs.getString("pinyin"));
		idiom.setImage(rs.getBytes("image"));
		idiom.setLevel(rs.getInt("level"));
		idiom.setDifficulty(rs.getInt("difficulty"));
		idiom.setDescription(rs.getString("description"));
		idiom.setSortOrder(rs.getInt("sortOrder"));
	}
	
	/**
	 * 执行Sql
	 * @param sql
	 * @throws SQLException
	 */
	public static void execute(String sql) throws SQLException{

		Connection connection = getConnection();
		
		Statement statement = connection.createStatement();
		
		statement.execute(sql);
		
	}

}

转载于:https://my.oschina.net/minglic/blog/150385

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值