자바 JDBC 를 통 해 Sqlite 에 접근 하 는 실례

5464 단어 자바sqlitejdbc
코드 가 시작 되 기 전에 의존 패 키 지 를 먼저 도입 해 야 합 니 다.
maven 도입 의존:
4
<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);
		
	}

}

좋은 웹페이지 즐겨찾기