如何将 SQLite 与 Java 连接? [英] How to connect SQLite with Java?

查看:34
本文介绍了如何将 SQLite 与 Java 连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的代码从 Java 应用程序访问 SQLite 数据库.我的代码是

I am using one simple code to access the SQLite database from Java application . My code is

 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 public class ConnectSQLite 
 {  
  public static void main(String[] args) 
  {  
     Connection connection = null;  
     ResultSet resultSet = null;  
     Statement statement = null;  

     try 
     {  
         Class.forName("org.sqlite.JDBC");  
         connection = DriverManager.getConnection("jdbc:sqlite:D:\testdb.db");  
         statement = connection.createStatement();  
         resultSet = statement  
                 .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");  
         while (resultSet.next()) 
         {  
             System.out.println("EMPLOYEE NAME:"  
                     + resultSet.getString("EMPNAME"));  
         }  
     } 
     catch (Exception e) 
     {  
         e.printStackTrace();  
     }
     finally 
     {  
         try 
         {  
             resultSet.close();  
             statement.close();  
             connection.close();  
         } 
         catch (Exception e) 
         {  
             e.printStackTrace();  
         }  
     }  
 }  
}  

但是这段代码给出了一个例外

But this code gives one exception like

java.lang.ClassNotFoundException: org.sqlite.JDBC

我该如何解决这个问题,请帮帮我.

How can I slove this,please help me.

推荐答案

您的类路径中需要有一个 SQLite JDBC 驱动程序.

You need to have a SQLite JDBC driver in your classpath.

Taro L. Saito (xerial) fork Zentus 项目,现在以sqlite-jdbc.它捆绑了主要平台的本机驱动程序,因此您无需单独配置它们.

Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.

这篇关于如何将 SQLite 与 Java 连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆