NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class MainPage extends JFrame {

JFrame frame = new JFrame();
int updateId;

MainPage() {
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(420, 420);
frame.setLayout(null);
frame.setVisible(true);

JLabel label = new JLabel("Ene bol undsen huudas");
label.setBounds(80, 24, 140, 25);
frame.add(label);

JScrollPane sp = new JScrollPane();
sp.setBounds(10, 64, 366, 107);
frame.add(sp);

JTextField username = new JTextField();
username.setBounds(10, 200, 100, 30);
frame.add(username);

JTextField emaile = new JTextField();
emaile.setBounds(10, 280, 100, 30);
frame.add(emaile);

JButton updateBTN = new JButton("Update");
updateBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateData(username.getText(), emaile.getText());
}
});
frame.add(updateBTN);
updateBTN.setBounds(200, 300, 89, 23);

JButton deleteBTN = new JButton("Delete");
deleteBTN.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteData();
}
});
frame.add(deleteBTN);
deleteBTN.setBounds(300, 300, 89, 23);

loadTableData(sp);

}

private void loadTableData(JScrollPane sp) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sarnai", "root", "");
Statement stmt = con.createStatement();
String sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);
String[] column = { "ID", "Name", "Email" };
DefaultTableModel tdm = new DefaultTableModel(column, 0);
while (rs.next()) {
String id = rs.getString("u_id");
String uname = rs.getString("username");
String email = rs.getString("email");

String[] data = { id, uname, email };
tdm.addRow(data);
}

JTable jt = new JTable(tdm);
sp.setViewportView(jt);

jt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int row = jt.getSelectedRow();
if (row < 0) {
System.out.print("Didn't select any row");
} else {
updateId = Integer.parseInt(jt.getValueAt(row, 0).toString());
username.setText(jt.getValueAt(row, 1).toString());
emaile.setText(jt.getValueAt(row, 2).toString());
}
}
});

} catch (Exception e) {
System.out.print(e);
}
}

private void updateData(String username, String email) {
if (updateId == 0) {
JOptionPane.showMessageDialog(null, "Please select a user to update");
return;
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sarnai", "root", "");
String sql = "UPDATE users SET username = ?, email = ? WHERE u_id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, email);
pstmt.setInt(3, updateId);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "User updated successfully");
con.close();
frame.dispose();
new MainPage();
} catch (Exception e) {
System.out.print(e);
}
}

private void deleteData() {
if (updateId == 0) {
JOptionPane.showMessageDialog(null, "Please select a user to delete");
return;
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sarnai", "root", "");
String sql = "DELETE FROM users WHERE u_id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, updateId);
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "User deleted successfully");
con.close();
frame.dispose();
new MainPage();
} catch (Exception e) {
System.out.print(e);
}
}

public static void main(String[] args) {
new MainPage();
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.