Introduction of the Project
Have you ever wondered how the criminal record of individuals is stored and managed? If yes, today we will write a program that creates a criminal record management system using Java. We will also be implementing GUI in order to make it more user interactive along with SQL database to store information regarding criminal name, age, crime & arrest date.
Objectives
We basically have two main objectives while writing this code. The 1st one is to create a user interface using some graphics, and the 2nd objective is to implement the tables that contain the criminal’s information and attach the functionality of adding & updating information to the database.
Requirements
The major requirement to code this program is
- Swing to create the Graphical User Interface (GUI) of the program. (The Swing framework of Java has several components that give developers the freedom and flexibility to make eye-catching interfaces for windows-based applications. The best part about using Swing to create components like tables, scroll panes, buttons, and the like is that the framework is platform-independent.)
Source Code
package com.company; public class Main { public static void main(String[] args) { new Crimes(); } } package com.company; import com.toedter.calendar.JDateChooser; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.*; import java.text.DateFormat; import java.util.Vector; public class Crimes { private JTextField idData; private JTextField nameData; private JTextField crimeData; private JTextField ageData; private JPanel dateCrime; private JTable table1; private JButton ADDRECORDButton; private JButton UPDATERECORDButton; private JPanel crimePanel; JDateChooser dateChooser = new JDateChooser(); JFrame crime = new JFrame(); public Crimes(){ crime.setDefaultCloseOperation(crime.EXIT_ON_CLOSE); crime.setContentPane(crimePanel); crime.pack(); crime.setLocationRelativeTo(null); crime.setSize(600,500); crime.setVisible(true); dateCrime.add(dateChooser); tableData(); ADDRECORDButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(idData.equals("")|| nameData.equals("")|| crimeData.equals("")|| ageData.equals("")||dateChooser.getDate()==null){ JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record."); }else{ try { String sql = "insert into criminal"+"(Case_ID,Name,Crime_Type,Age,Arrest_Date)"+"values (?,?,?,?,?)"; Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root"); PreparedStatement statement = connection.prepareStatement(sql); String strDate = DateFormat.getDateInstance().format(dateChooser.getDate()); statement.setInt(1,Integer.parseInt(idData.getText())); statement.setString(2, nameData.getText()); statement.setString(3, crimeData.getText()); statement.setInt(4,Integer.parseInt(ageData.getText())); statement.setString(5,strDate); statement.executeUpdate(); JOptionPane.showMessageDialog(null,"ITEM ADDED SUCCESSFULLY"); idData.setText(""); nameData.setText(""); crimeData.setText(""); ageData.setText(""); dateChooser.setCalendar(null); }catch (Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } tableData(); } } }); UPDATERECORDButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ String strDate = DateFormat.getDateInstance().format(dateChooser.getDate()); String sql = "UPDATE criminal " + "SET Name = '"+ nameData.getText()+"',Crime_Type='"+ crimeData.getText()+"',Age='"+Integer.parseInt(ageData.getText())+"',Arrest_Date='"+strDate+"' WHERE Case_ID="+Integer.parseInt(idData.getText()); Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root"); PreparedStatement statement = connection.prepareStatement(sql); statement.executeUpdate(); JOptionPane.showMessageDialog(null,"Updated successfully"); }catch (Exception e2){ System.out.println(e2); } tableData(); } }); table1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { DefaultTableModel dm = (DefaultTableModel)table1.getModel(); int rowSelect = table1.getSelectedRow(); idData.setText(dm.getValueAt(rowSelect,0).toString()); nameData.setText(dm.getValueAt(rowSelect,1).toString()); crimeData.setText(dm.getValueAt(rowSelect,2).toString()); ageData.setText(dm.getValueAt(rowSelect,3).toString()); } }); } public void tableData() { try{ String a= "Select* from criminal"; Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root"); Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(a); table1.setModel(buildTableModel(rs)); }catch (Exception ex1){ JOptionPane.showMessageDialog(null,ex1.getMessage()); } } public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); // names of columns Vector<String> columnNames = new Vector<String>(); int columnCount = metaData.getColumnCount(); for (int column = 1; column <= columnCount; column++) { columnNames.add(metaData.getColumnName(column)); } // data of the table Vector<Vector<Object>> data = new Vector<Vector<Object>>(); while (rs.next()) { Vector<Object> vector = new Vector<Object>(); for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { vector.add(rs.getObject(columnIndex)); } data.add(vector); } return new DefaultTableModel(data, columnNames); } }
Explanation of the Code
The code can be broken down into two sections. One involves creating the GUI, and the other is the retrieval of information from the SQL database & updating it according to the user’s input.
Let us look at the GUI first:
1. It consists of 4 textfields and a calendat that require criminal-related data to be added to the database.
2. Then, we add an “ADD” button, which inserts data into the database from the textfields.
3. It consists of an Update Button that, as the name suggests, updates the database.
Moving to the retrieval, we will apply the following:
1. Build a connection first with the database using the Connection object.
2. Inject the query that stores table data in ResultSet.
3. Finally, send data to Jtable.
4. Addition & Updating the data require the injection of a query that manipulates the data & finally, using the method tableData() to display data into Jtable().
Output
Main Interface
Conclusion
This tutorial was about a criminal record management system using Java to manage criminal records. We used the Swing module for the GUI and created a very efficient code to store & get access to the criminal record, as it manages almost all kinds of information required to know about a criminal.

Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools. He has been writing content since last 5 years. He is a Senior Manager at Rude Labs Pvt. Ltd.
0 Comments