자바 JDBC 를 통 해 Sqlite 에 접근 하 는 실례
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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.