Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect SQLite with Java?

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.

like image 459
Rajapandian Avatar asked Oct 06 '09 13:10

Rajapandian


People also ask

How do I connect to a SQLite database?

To establish a database connection to an SQLite database, you need to create a new instance of the PDO class and pass a connection string to the constructor of the PDO object. Because you store the path to the sqlite database file in the Config class, you just simply use it to construct the connection string.

Does Java have SQLite?

The SQLiteJDBC package contains both Java classes, as well as native SQLite libraries for Windows, Mac OS X, and Linux. Connecting to an SQLite database: this tutorial shows you how to download SQLiteJDBC driver and connect to an existing SQLite database using JDBC.


1 Answers

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

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.

like image 102
Reverend Gonzo Avatar answered Oct 20 '22 16:10

Reverend Gonzo