For my final project in my Software construction, design, and architecture class, my partner and I had the idea of creating a web page that was able to access data of a SQLite database. The structure of this project was to connect to a database using Java as a backend and use Rest api to send out sql commands. We needed to run the backend on a server using a Spring Boot framework available to us. Once our connection was setup and our backend methods to access the database was ready we had to create the front end. We used Typescript and Java Angular to create our front end, or in other words our web page. The typescript code had to connect to the Java rest api so our chain of connections from the top goes; Typescript and Angular -> Java Rest api -> SQLite database.
Starting the project the very first obstacle we ran into was connection Java rest api to the SQLite database. It was something we had never done before, but thankfully there was helpful resources online. We found out that there was a series of Java imports to facilitate this function. A few imports needed are as follows:
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Using these imports we were able to create an object of type ‘Connection’ in order to establish the actual connection to the database. Here is our method:
public static Connection getConnection() {
if (conn == null) {
try {
conn = DriverManager.getConnection(“jdbc:sqlite:” + dbpath);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
The set method to set our path:
DatabaseSQL.setPath(“C:/…(insert path here)…”);
We are now able to get the connection inside our Java rest api classes that contain our SQLite query methods using this statement:
Connection conn = DatabaseSQL.getConnection();
As I said this information can be found through many sources online so we were lucky such a function was available for us to use at our disposal. We needed the connection to be established before we could proceed with anything else because the entire project relied on accessing the database. However, in order to actually know if our connection was working we couldn’t rely on the absence of errors. We needed to create a rest api method to access the database and give us a result so that we could be sure.
From the blog cs@worcester – Zac's Blog by zloureiro and used with permission of the author. All other rights reserved by the author.