• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refresh data of JTable when I click on save button ?

 
Mayur Saparia
Greenhorn
Posts: 19
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.event.*;
import java.util.*;
import java.sql.*;

public class addUser extends JFrame implements ActionListener {
static JFrame frame = new JFrame("");
DefaultTableModel list;
Object[] colheads = { "Sr No.", "Name", "Designation", "Username",
"Password", "Rights" };
Object[] newrow = { "", "", "", "", "", "" };
Container content;
JTable table;
JScrollPane jsrollpane;

JLabel[] labels = new JLabel[7];
JTextField[] text = new JTextField[4];
JLabel e1, e2, e3;

JPasswordField t2;
JComboBox rights;
JButton[] button = new JButton[4];

// database connectivity code
String dbUrl = "jdbc:mysql://localhost:3306/tsi_mm";

Connection con;
Statement stmt;
ResultSet rs;
// ResultSetMetaData rsmd;

Color WHITE = new Color(255, 255, 255);
Color BLACK = new Color(0, 0, 0);
Color SILVER = new Color(95, 94, 94);

public static void main(String a[]) {
new addUser();
}

addUser() {

Container content = frame.getContentPane();
content.setLayout(null);
content.setBackground(WHITE);

// Creating labels using for loop
int v = 160;
String[] label = { "User ID", "Name", "Designation", "Username",
"Password", "Rights" };

for (int i = 0; i < 6; i++) {
labels[i] = new JLabel(label[i]);
content.add(labels[i]);
labels[i].setFont(new java.awt.Font("Arial", Font.PLAIN, 16));
labels[i].setBounds(50, v, 200, 20);
labels[i].setForeground(SILVER);
labels[i].setBackground(WHITE);
v = v + 50;
}

// Creating TextFields using for loop
int x = 160;

for (int i = 0; i < 4; i++) {
text[i] = new JTextField();

text[0].setEnabled(false);

content.add(text[i]);

text[i].setBounds(200, x, 150, 20);
text[i].setForeground(BLACK);
text[i].setBackground(WHITE);
text[i].setFont(new Font("Arial", Font.BOLD, 12));
x = x + 50;
}

int y = 200;
String[] buttonName = { "Save", "Add More", "Cancel", "Home" };

for (int i = 0; i < 4; i++) {
button[i] = new JButton(buttonName[i]);
button[i].setBounds(y, 460, 90, 40);
content.add(button[i]);
button[i].addActionListener(this);
y = y + 110;
}

e1 = new JLabel("TSI Material Management System");
content.add(e1);
e1.setFont(new java.awt.Font("Arial", Font.BOLD, 18));
e1.setBounds(500, 25, 300, 20);
e1.setForeground(SILVER);
e1.setBackground(WHITE);

e2 = new JLabel("Add User Details");
content.add(e2);
e2.setFont(new java.awt.Font("Arial", Font.BOLD, 18));
e2.setBounds(50, 100, 300, 20);
e2.setForeground(SILVER);
e2.setBackground(WHITE);

t2 = new JPasswordField(10);
t2.setBounds(200, 360, 150, 20);
t2.setForeground(BLACK);
t2.setBackground(WHITE);
t2.setFont(new Font("Arial", Font.BOLD, 16));
content.add(t2);

rights = new JComboBox();
rights.setBounds(200, 410, 150, 20);
rights.setForeground(BLACK);
rights.setBackground(WHITE);
rights.setFont(new Font("Arial", Font.PLAIN, 12));
content.add(rights);
rights.addItem("Select Rights");
rights.addItem("Regular");
rights.addItem("Admin");
rights.setSelectedIndex(0);

e3 = new JLabel();
content.add(e3);
e3.setBounds(450, 160, 308, 210);
e3.setIcon(new ImageIcon(
"I://material management//tsimaterialManagement//groupLogo//logo.jpg"));
e3.setFont(new Font("Arial", Font.BOLD, 14));

JMenuBar menubar = new JMenuBar();
JMenu filemenu = new JMenu("File");
filemenu.add(new JSeparator());
JMenu editmenu = new JMenu("Edit");
editmenu.add(new JSeparator());
JMenuItem fileItem1 = new JMenuItem("Save");
JMenuItem fileItem2 = new JMenuItem("Add More");
JMenuItem fileItem3 = new JMenuItem("Home");
fileItem3.add(new JSeparator());
JMenuItem fileItem4 = new JMenuItem("Close");

filemenu.add(fileItem1);
filemenu.add(fileItem2);
filemenu.add(fileItem3);
filemenu.add(fileItem4);
menubar.add(filemenu);
menubar.add(editmenu);

// String[] columnNames = {"Name",
// "Designation",
// "Username",
// "Password",
// "Rights"}; will be used when table is inserted in the module

list = new DefaultTableModel(colheads, 0);
table = new JTable(list);
table.setEnabled(true);

jsrollpane = null;
jsrollpane = new JScrollPane(table);
jsrollpane.setBounds(50, 510, 1250, 400);
jsrollpane.setVisible(true);

content.add(jsrollpane);

frame.setBounds(0, 0, 1365, 740);
frame.setResizable(true);
frame.setJMenuBar(menubar);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// this code is written to get the user ID in the Disabled textField
// according to the last userId in the database
try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
String maxId = "Select max(user_id) from mm_adduser";
ResultSet rs;
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database

System.out.println(lastId);
int userId = lastId + 1;
text[0].setText(Integer.toString(userId));

String queryReport = "select * from mm_adduser";
rs = stmt.executeQuery(queryReport);
System.out.println("succesfull");
while (rs.next()) {

Object newrow[] = { rs.getInt(1), rs.getString(2),
rs.getString(3), rs.getString(4), rs.getString(5),
rs.getString(6) };
list.addRow(newrow);

}

con.close();
rs.close();

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == button[0]) {

save();

} else if (e.getSource() == button[1]) {
addmore();
}

}

public void addmore() {

for (int i = 0; i < 4; i++) {
text[i].setText("");
}
t2.setText("");
rights.setSelectedIndex(0);
try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
String maxId = "Select max(user_id) from mm_adduser";
ResultSet rs;
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database
text[0].setText(Integer.toString(lastId + 1));

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

}

public void save() {
int n = JOptionPane.showConfirmDialog(frame,
"Would you like to save the record?", "Confirm",
JOptionPane.YES_NO_OPTION);

if (n == JOptionPane.YES_OPTION) {
try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
PreparedStatement pstmt = null;
ResultSet rs;
String maxId = "Select max(user_id) from mm_adduser";
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database

int userId = lastId + 1;
String user_name = text[1].getText();
String user_designation = text[2].getText();
String user_username = text[3].getText();
String user_password = t2.getText();
String user_rights = (String) rights.getSelectedItem();

// JOptionPane.showMessageDialog(null, user_rights);

String query = "Insert into mm_adduser (user_id,user_name,user_designation,user_username,user_password,user_rights) VALUES (?,?,?,?,?,?)";
pstmt = con.prepareStatement(query);
pstmt.setInt(1, userId);
pstmt.setString(2, user_name);
pstmt.setString(3, user_designation);
pstmt.setString(4, user_username);
pstmt.setString(5, user_password);
pstmt.setString(6, user_rights);
int check = pstmt.executeUpdate();
if (check == 1) {
JOptionPane.showMessageDialog(null,
"Data saved successfully !");
} else {
JOptionPane.showMessageDialog(null, "Data saving failed !");
}

for (int i = 0; i < 4; i++) {
text[i].setText("");
}
t2.setText("");
rights.setSelectedIndex(0);

rs = stmt.executeQuery(maxId);
int LastId = 0;
while (rs.next()) {
LastId = rs.getInt(1);
}// to get the last user Id from the database

text[0].setText(Integer.toString(LastId + 1));

con.close();
rs.close();
pstmt.close();

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

} else if (n == JOptionPane.NO_OPTION) {

for (int i = 0; i < 4; i++) {
text[i].setText("");
}
t2.setText("");
rights.setSelectedIndex(0);
try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
String maxId = "Select max(user_id) from mm_adduser";
ResultSet rs;
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database
text[0].setText(Integer.toString(lastId + 1));

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

JOptionPane.showMessageDialog(null,
"You choose not to save the data !");

}

}
}



In this code, when i click on SAVE button the data gets saved in the database but it doesn't reflect immediately in the Table . Please tell me how to proceed for it.
So basically i want to learn how to refresh the table on click of save button so that data changes are reflected immediately.

Thanks in advance.
Mayur.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could reload/rebuild the table from the database following a save.
 
Mayur Saparia
Greenhorn
Posts: 19
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used the following code for the JTable to refresh it when the data is saved.

public void save() {
int n = JOptionPane.showConfirmDialog(frame,
"Would you like to save the record?", "Confirm",
JOptionPane.YES_NO_OPTION);

if (n == JOptionPane.YES_OPTION) {
try {



Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
PreparedStatement pstmt = null;
ResultSet rs;
String maxId = "Select max(customer_id) from mm_customerrecords";
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database

int userId = lastId + 1;
String customer_name = text[1].getText();
String customer_representativeName = text[2].getText();
String customer_email = text[3].getText();
String customer_phno = text[4].getText();
String customer_address = address.getText();


// JOptionPane.showMessageDialog(null, user_rights);

String query = "Insert into mm_customerrecords (customer_id,customer_name,customer_representativeName,customer_email,customer_phno,customer_address) VALUES (?,?,?,?,?,?)";
pstmt = con.prepareStatement(query);
pstmt.setInt(1,userId);
pstmt.setString(2, customer_name);
pstmt.setString(3, customer_representativeName);
pstmt.setString(4, customer_email);
pstmt.setString(5, customer_phno);
pstmt.setString(6, customer_address);
int check = pstmt.executeUpdate();
if (check == 1) {
JOptionPane.showMessageDialog(null,
"Data saved successfully !");
list.fireTableDataChanged();
} else {
JOptionPane.showMessageDialog(null, "Data saving failed !");
}

for (int i = 0; i < 5; i++) {
text[i].setText("");
}
address.setText("");


rs = stmt.executeQuery(maxId);
int LastId = 0;
while (rs.next()) {
LastId = rs.getInt(1);
}// to get the last user Id from the database

text[0].setText(Integer.toString(LastId + 1));

con.close();
rs.close();
pstmt.close();

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

} else if (n == JOptionPane.NO_OPTION) {

for (int i = 0; i < 4; i++) {
text[i].setText("");
}
address.setText("");

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(dbUrl, "root", "");
Statement stmt = con.createStatement();
String maxId = "Select max(customer_id) from mm_customerrecords";
ResultSet rs;
rs = stmt.executeQuery(maxId);
int lastId = 0;
while (rs.next()) {
lastId = rs.getInt(1);
}// to get the last user Id from the database
text[0].setText(Integer.toString(lastId + 1));

} // end try

catch (ClassNotFoundException e) {
// System.out.println(e);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e);
}

JOptionPane.showMessageDialog(null,
"You choose not to save the data !");

}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic