-
Home
-
Course
-
Databases
-
Create in memory database
Create in memory database
Thursday, October 26, 2023
⁃
Difficulty:
Intermediate
Objective
Create a Java program that creates a SQLite version 3 in memory 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 in memory connection, then create a new command using the SQLiteCommand
object and finally run the command once for each table.
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:
Install-Package System.Data.SQLite -Version 1.0.112
The connection string required to connect in memory is as follows:
Data Source=:memory:;Version=3;New=True;
Input
create table person (name varchar(20), age int)
create table teacher (name varchar(20))
Output
Solution
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class CreateSQLiteMemoryDatabase {
public static void main(String[] args) {
String connectionString = "jdbc:sqlite::memory:";
createTables(connectionString);
}
public static void createTables(String connectionString) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection(connectionString);
connection.setAutoCommit(false);
statement = connection.createStatement();
String createTablePersona = "CREATE TABLE persona (nombre TEXT, edad INT)";
statement.executeUpdate(createTablePersona);
String createTableProfesor = "CREATE TABLE 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);
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class CreateSQLiteMemoryDatabase {
public static void main(String[] args) {
String connectionString = "jdbc:sqlite::memory:";
createTables(connectionString);
}
public static void createTables(String connectionString) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection(connectionString);
connection.setAutoCommit(false);
statement = connection.createStatement();
String createTablePersona = "CREATE TABLE persona (nombre TEXT, edad INT)";
statement.executeUpdate(createTablePersona);
String createTableProfesor = "CREATE TABLE 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
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
Copied
The code has been successfully copied to the clipboard.