This is a student CRUD API project with JDBC connection to a MySQL 8.0 database. It allows to create new, read, update, and delete existing student data.
To get started, execute the following:
- Download Eclipse
- Download a SQL client like MySQL Workbench
- Run sql.sql to set up a database
- Start using our menu-driven app
Packages should be cohesive collections of classes. That would minimize coupling between classes that should not be coupled because they pertain to different features. In the package-by-layer approach, packages have high cohesion and a low degree of coupling between packages.
Our code follows common convention of organizing itself in different subpackages, each for a different child-layer: dao, db, main, and pojo
, whereby com.example
is the parent of them all. Hence, the name tree structure.
JDBC (Java Database Connectivity) is the Java API that manages connecting to a database, issuing queries and commands, and handling result sets obtained from the database.
The steps for connecting to a database with JDBC are as follows:
- Install or locate the database you want to access.
- Include the JDBC library.
- Ensure the JDBC driver you need is on your classpath.
- Use the JDBC library to obtain a connection to the database.
- Use the connection to issue SQL commands.
- Close the connection when you are finished.
As a developer, you can use JDBC to interact with a database from within a Java program. JDBC acts as a bridge from your code to the database. JDBC is the common API that your application code interacts with. Beneath that is the JDBC-compliant driver for the database you are using as shown in the figure below:
Source: InfoWorld
While JDBC is sufficient for simpler applications, most developers will eventually look to the Jakarta Persistence API (formerly the Java Persistence API) to develop a more formal data access layer.
There are many objects we only need one of: thread pools, caches, dialog boxes, objects that handle preferences and registry settings, objects used for logging, and objects that act as device drivers to devices like printers and graphics cards. In fact, for many of these types of objects, if we were to instantiate more than one we'd run into all sorts of problems like incorrect program behavior, overuse of resources, or inconsistent results.
The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it. Whenever you need an instance, just query the class and it will hand you back the single instance.
In short, a Singleton is a one-of-a-kind object for which only one instance is required. Following this best practice we put all our database code into one class. The so-called resulting Singleton class CP.java
is as follows:
public class CP {
static Connection conn;
public static Connection createc() throws SQLException {
//load driver class
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//create connection
String user="root";
String password="root";
String url="jdbc:mysql://localhost:3306/studentdata";
conn=DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
Here, we execute an SQL query using PreparedStatement
and SQL-argument-placeholders. Since this action has to be repeated at many occasions (CRUD) it makes sense to place the code inside a Singleton.
The DAO is responsible for interfacing with the database. It implements methods for saving/retrieving/deleting entities.
To enable mapping between Java code and database we create a Plain Old Object Class (POJO). For each table there is a corresponding POJO class. This is the convention.
For our case we just need one Student POJO since we have only one table students
in our database. It contains a default and a parameterized constructor, getter / setter methods, and a toString method.