Introduction of the Project
Let us write the source code for Online Document Management System In Java that creates a management system to store documents easily. We are going to apply an SQL database to provide a way to save information about available documents, the date & time the document was uploaded, and the name of the document.
Objectives
- To apply the Swing module to create an interactive interface for the user to manage the documentation system.
- To implement a table with file information like its name, the date and time the document was uploaded & the document itself.
Requirements
Source Code
package com.company; public class Main { public static void main(String[] args) { new Docs(); } } package com.company; import java.nio.file.Files; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.* import java.io.File; import java.io.FileReader; import java.sql.*; import java.*; public class Docs { private JTextField nameData; private JTable table1; private JButton ADDRECORDButton; private JButton UPDATERECORDButton; private JPanel docsPanel; private JPanel datePanel; private JComboBox type; private JButton SELECTFILEButton; private JTextField path; private JButton BACKButton; private JTextArea textArea1; private JButton RESETButton; JFrame docsF = new JFrame(); public Docs(){ docsF.setContentPane(docsPanel); docsF.pack(); docsF.setVisible(true); tableData(); ADDRECORDButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(nameData.getText().equals("")){ JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record."); }else{ String sql = "insert into docs"+"(DOC_NAME,DATE,DATA)"+"values (?,?,?)"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root"); PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1,nameData.getText()); statement.setString(2, dtf.format(now)); File file = new File(path.getText()); FileReader reader = new FileReader(file); statement.setCharacterStream(3,reader,file.length()); statement.executeUpdate(); JOptionPane.showMessageDialog(null,"DETAILS ADDED SUCCESSFULLY"); nameData.setText(""); path.setText(""); }catch (Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } tableData(); } } }); UPDATERECORDButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ File file = new File(path.getText()); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); String sql = "UPDATE docs " + "SET DATA = '"+ Files.readString(file.toPath())+"',DATE= '"+dtf.format(now)+"'"+ " WHERE DOC_NAME= '"+nameData.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(); } }); SELECTFILEButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int res = fileChooser.showOpenDialog(null); if(res == JFileChooser.APPROVE_OPTION){ path.setText(fileChooser.getSelectedFile().getAbsolutePath()); }else { JOptionPane.showMessageDialog(null,"PLEASE SELECT FILE"); } } }); table1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { DefaultTableModel dm = (DefaultTableModel)table1.getModel(); int rowIndex = table1.getSelectedRow(); nameData.setText(dm.getValueAt(rowIndex,0).toString()); textArea1.setText(dm.getValueAt(rowIndex,2).toString()); } }); RESETButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { docsF.dispose(); new Docs(); } }); } public void tableData() { try{ String a= "Select* from docs"; 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
Let’s look at the GUI first:
1. The main screen consists of four buttons: add a record, update a record, reset screen & select file.
2. There is a text area where stored information is visible.
3. Finally, a Jtable() displays the information stored in the database.
Retrieval from SQL Database, we will apply:
1. The very first thing to do is establish a connection to the database using a connection object.
2. Now, Paste a query containing table data into a ResultSet().
3. Then, sends the data to Jtable.
4. Adding & Updating the data requires the injection of a query that manipulates the data & finally, using the method tableData() to display data into Jtable().
5. JfileChooser() is utilized to pop the file selection window & then stored locally then sent to the database.
Output
Main Interface
Conclusion
We have successfully implemented the Swing module and SQL database to create a coding project on Online Document Management System In Java that helps you to store your important documents. This is a very efficient program and an easy way to manage your uploaded documents and get access to it with a very easy interface.

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