SQLite数据库Java驱动下载及连接示例(sqlite-jdbc-3.30.1.jar)

万喜
2023-12-01

一、SQLite数据库Java驱动下载

Google一下,sqlite-jdbc的下载地址:https://bitbucket.org/xerial/sqlite-jdbc/downloads/

我们下载最新版的sqlite-jdbc-3.30.1.jar的即可。

 

二、SQLite数据库Java连接示例

import java.sql.*;

// 简单的连接SQLite的Java程序示例

public class SqliteTest 
{
	public static void main(String[] args) {
    	
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
    	
		try {
			// 连接SQLite
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Dragon\\Desktop\\data.db");
			stat = conn.createStatement();
			// 查询数据
			rs = stat.executeQuery("select * from student"); 

			// 打印数据
			while (rs.next()) {
				System.out.print("ID = " + rs.getString("ID") + "\t");
				System.out.print("name = " + rs.getString("name") + "\t");
				System.out.println("age = " + rs.getString("age"));
			}
        }
        catch(ClassNotFoundException | SQLException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace ();
        }
        
        finally {
        	if(rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	if(stat != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	if(conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        }
        
    }
}

 

 类似资料: