Create a Java program that creates a version 3 SQLite database with two tables (Person and Teacher). Use the SQL statements in the entry to create the tables.
To connect to SQLite open a new connection against the database file, then create a new command using the SQLiteCommand object and finally run the command once for each table.
SQLiteCommand
Remember that to connect to SQLite you will need the reference of System.Data.SQLite , you can get it from official page or install it directly in your project using the Nuguet package manager, executing the following command in the console:
System.Data.SQLite
Install-Package System.Data.SQLite -Version 1.0.112
create table if not exists person (name varchar(20), age int) create table if not exists teacher (name varchar(20))
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class CreateSQLiteDatabase { public static void main(String[] args) { String databaseName = "out.sqlite"; createDatabaseIfNotExists(databaseName); createTablesIfNotExists(databaseName); } public static void createDatabaseIfNotExists(String databaseName) { try { Class.forName("org.sqlite.JDBC"); Connection connection = DriverManager.getConnection("jdbc:sqlite:" + databaseName); connection.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } } public static void createTablesIfNotExists(String databaseName) { Connection connection = null; Statement statement = null; try { Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:" + databaseName); connection.setAutoCommit(false); statement = connection.createStatement(); String createTablePersona = "CREATE TABLE IF NOT EXISTS persona (nombre TEXT, edad INT)"; statement.executeUpdate(createTablePersona); String createTableProfesor = "CREATE TABLE IF NOT EXISTS profesor (nombre TEXT)"; statement.executeUpdate(createTableProfesor); statement.close(); connection.commit(); connection.close(); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Create a Java program that creates a version 3 SQLite database with two tables (Person and Teacher).
Create a Java program that creates a SQLite version 3 in memory database with two tables (Person and Teacher).
Create a Java program that allows you to perform CRUD operations from SQLite, the program must have the following menu.
Practice with exercises in Java to manage databases. Learn how to perform CRUD actions on SQLite databases.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.