Java使用Sqlite

Sqlite作为一种轻量级的数据库,使用起来非常的方便。

1、下载Sqlitejdbc

网址:https://bitbucket.org/xerial/sqlite-jdbc

2、将下载到的jar可以直接放到工程中

3、使用

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
	private static Connection con;
	private static Statement stmt;
	private static ResultSet res;
	public static void main(String[] args) {
		try {
			Class.forName("org.sqlite.JDBC");
			con = DriverManager.getConnection("jdbc:sqlite://D:/db.db");
			stmt = con.createStatement();
			String sql = "SELECT * FROM user;";
			res = stmt.executeQuery(sql);
			while(res.next()){
				System.out.println("id:"+res.getInt(1));
				System.out.println("name:"+res.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{			
			try {
				res.close();
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}


你可能感兴趣的:(sqlite)