patternjavaMinor
JTable for employees in SQL table
Viewed 0 times
jtableemployeessqlfortable
Problem
I have been studying Java for 4 months and this project is what I came up with. I know I should make more classes and methods for quality but I am still learning. Please help to me if there is any simpler and cleaner ways to achieve the same results as this.
```
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUI extends JDialog {
private JTextField txtID;
private JTextField txtName;
private JTextField txtDep;
private JTextField txtGender;
private JTextField txtPosition;
private JTextField txtSalary;
private JTable table;
ResultSet rs;
Connection conn;
Statement statement;
public GUI() throws SQLException {
setBounds(100, 100, 671, 448);
getContentPane().setLayout(null);
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println(ex.getMessage());
}
table = new JTable();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 635, 205);
getContentPane().add(scrollPane);
scrollPane.setViewportView(table);
String
```
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUI extends JDialog {
private JTextField txtID;
private JTextField txtName;
private JTextField txtDep;
private JTextField txtGender;
private JTextField txtPosition;
private JTextField txtSalary;
private JTable table;
ResultSet rs;
Connection conn;
Statement statement;
public GUI() throws SQLException {
setBounds(100, 100, 671, 448);
getContentPane().setLayout(null);
try {
// Set System L&F
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.out.println(ex.getMessage());
}
table = new JTable();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 635, 205);
getContentPane().add(scrollPane);
scrollPane.setViewportView(table);
String
Solution
Prefer composition over inheritance
Unless you want to modify the behavior of a class, don't inherit from it. You should have a field which is the
Hungarian notation
With a good IDE, or with good naming, you don't have to add the type of in the variable name.
This could have been called
Vector is a thing of the past
The
PreparedStatement
You should learned to use PreparedStatement in Java so that code that looks like this would become safer and cleaner :
One of the problem with creating a query by appending
Unless you want to modify the behavior of a class, don't inherit from it. You should have a field which is the
JDialog and work with it.Hungarian notation
With a good IDE, or with good naming, you don't have to add the type of in the variable name.
FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.LEFT);This could have been called
buttonsLayout or something similar. Be clear with your name and take the time to choose name that matters. Re-read your code from bottom up, this can help sometimes to see where the names are troublesome.Vector is a thing of the past
The
Vector class should not been used anymore. Prefer the List interface with an implementation like ArrayList over it. See why I should not use Vector in Java for more information. PreparedStatement
You should learned to use PreparedStatement in Java so that code that looks like this would become safer and cleaner :
String sql_stmt3 = "UPDATE employees SET full_name = '" + txtName.getText() + "'";
sql_stmt3 += ",gender = '" + txtGender.getText() + "'";
sql_stmt3 += ",department = '" + txtDep.getText() + "'";
sql_stmt3 += ",position = '" + txtPosition.getText() + "'";
sql_stmt3 += ",salary = '" + txtSalary.getText() + "'";
sql_stmt3 += " WHERE employee_id = '" + txtID.getText() + "'";One of the problem with creating a query by appending
String is that your vulnerable to injection. By using PreparedStatement, you're negating a bit part of that issue, and you can re-use a preparedstatement too.Code Snippets
FlowLayout fl_buttonPane = new FlowLayout(FlowLayout.LEFT);String sql_stmt3 = "UPDATE employees SET full_name = '" + txtName.getText() + "'";
sql_stmt3 += ",gender = '" + txtGender.getText() + "'";
sql_stmt3 += ",department = '" + txtDep.getText() + "'";
sql_stmt3 += ",position = '" + txtPosition.getText() + "'";
sql_stmt3 += ",salary = '" + txtSalary.getText() + "'";
sql_stmt3 += " WHERE employee_id = '" + txtID.getText() + "'";Context
StackExchange Code Review Q#150835, answer score: 2
Revisions (0)
No revisions yet.