jdbc:sqlite:database_file_path
Where database_file_path can be either relative or absolute path. For example:jdbc:sqlite:product.db
jdbc:sqlite:C:/work/product.db
And here is the syntax of database connection URL for memory database:jdbc:sqlite::memory:
jdbc:sqlite:
Class.forName("org.sqlite.JDBC");Or: DriverManager.registerDriver(new org.sqlite.JDBC());
package net.codejava.jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* This program demonstrates making JDBC connection to a SQLite database.
* @author www.codejava.net
*
*/
public class JdbcSQLiteConnection {
public static void main(String[] args) {
try {
Class.forName("org.sqlite.JDBC");
String dbURL = "jdbc:sqlite:product.db";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected to the database");
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
conn.close();
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}That's Java code example to establish connection to a SQLite database.To see the coding in action, I recommend you to watch the video below:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.