当前位置:数据库 > SQLite >>

Java使用Sqlite

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

1、下载Sqlitejdbc
 

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

3、使用


[java
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(); 
            } 
        } 
 
    } 
 

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();
   }
  }

 }

}


 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,