/* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template */ /** * * @author drago */ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class Admin_data extends javax.swing.JFrame { /** * Creates new form Admin_data */ public Admin_data() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); View = new javax.swing.JButton(); back = new javax.swing.JButton(); insert_data = new javax.swing.JButton(); Search_btn = new javax.swing.JButton(); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null}, {null, null, null, null}, {null, null, null, null}, {null, null, null, null} }, new String [] { "Title 1", "Title 2", "Title 3", "Title 4" } )); jScrollPane1.setViewportView(jTable1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); View.setBackground(new java.awt.Color(102, 153, 255)); View.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N View.setForeground(new java.awt.Color(255, 255, 255)); View.setText("View"); View.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ViewActionPerformed(evt); } }); back.setBackground(new java.awt.Color(0, 153, 153)); back.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N back.setForeground(new java.awt.Color(255, 255, 255)); back.setText("<"); back.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backActionPerformed(evt); } }); insert_data.setBackground(new java.awt.Color(0, 153, 0)); insert_data.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N insert_data.setForeground(new java.awt.Color(255, 255, 255)); insert_data.setText("Insert"); insert_data.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { insert_dataActionPerformed(evt); } }); Search_btn.setText("Search"); Search_btn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Search_btnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(170, 170, 170) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(back, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(View, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(insert_data, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE) .addComponent(Search_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(0, 192, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(73, 73, 73) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(View) .addComponent(insert_data)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(back) .addComponent(Search_btn)) .addContainerGap(467, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents private void ViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewActionPerformed // TODO add your handling code here: admin_viewData avd = new admin_viewData(); avd.setVisible(true); // Close or hide the registration form this.dispose(); }//GEN-LAST:event_ViewActionPerformed private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed welcome ad = new welcome(); ad.setVisible(true); // Close or hide the registration form this.dispose(); }//GEN-LAST:event_backActionPerformed private void insert_dataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insert_dataActionPerformed insert_admin_data iad = new insert_admin_data(); iad.setVisible(true); // Close or hide the registration form this.dispose(); }//GEN-LAST:event_insert_dataActionPerformed private void Search_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Search_btnActionPerformed String searchTerm = JOptionPane.showInputDialog(this, "Enter search term:"); // JDBC connection parameters String url = "jdbc:mysql://localhost:3306/login_form"; String user = "root"; String pass = ""; try (Connection connection = DriverManager.getConnection(url, user, pass)) { // SQL query to select data from the admin_table based on the search term String query = "SELECT * FROM admin_table WHERE name LIKE ? OR email LIKE ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { // Set the parameters in the prepared statement preparedStatement.setString(1, "%" + searchTerm + "%"); preparedStatement.setString(2, "%" + searchTerm + "%"); try (ResultSet resultSet = preparedStatement.executeQuery()) { // Process the result set and display the data in a JOptionPane dialog StringBuilder resultMessage = new StringBuilder("Search Results:\n"); while (resultSet.next()) { String adminId = resultSet.getString("id"); String adminName = resultSet.getString("name"); String adminEmail = resultSet.getString("email"); // Append the retrieved data to the message resultMessage.append("Admin ID: ").append(adminId) .append(", Name: ").append(adminName) .append(", Email: ").append(adminEmail).append("\n"); } // Display the message in a JOptionPane dialog JOptionPane.showMessageDialog(this, resultMessage.toString()); } } } catch (SQLException ex) { // Handle exceptions ex.printStackTrace(); } }//GEN-LAST:event_Search_btnActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Admin_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Admin_data().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Search_btn; private javax.swing.JButton View; private javax.swing.JButton back; private javax.swing.JButton insert_data; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables private void admin_viewData() { String url = "jdbc:mysql://localhost:3306/login_form"; String user = "root"; String pass = ""; try (Connection connection = DriverManager.getConnection(url, user, pass)) { // SQL query to select all data from the admin_table String query = "SELECT * FROM admin_table"; try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { try (ResultSet resultSet = preparedStatement.executeQuery()) { // Process the result set and display the data while (resultSet.next()) { String adminId = resultSet.getString("id"); // Assuming you have an "id" column String adminName = resultSet.getString("name"); String adminEmail = resultSet.getString("email"); // Display or log the retrieved data System.out.println("Admin ID: " + adminId + ", Name: " + adminName + ", Email: " + adminEmail); } } } } catch (SQLException ex) { // Handle exceptions ex.printStackTrace(); } } private void DeleteData() { // Prompt the user for an admin ID to delete String adminIdToDelete = JOptionPane.showInputDialog(this, "Enter Admin ID to delete:"); // JDBC connection parameters String url = "jdbc:mysql://localhost:3306/login_form"; String user = "root"; String pass = ""; try (Connection connection = DriverManager.getConnection(url, user, pass)) { // SQL query to delete data from the admin_table based on ID String query = "DELETE FROM admin_table WHERE id = ?"; try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, adminIdToDelete); // Execute the update int rowsAffected = preparedStatement.executeUpdate(); if (rowsAffected > 0) { // Data deleted successfully JOptionPane.showMessageDialog(this, "Admin data deleted successfully!"); } else { // Handle deletion failure JOptionPane.showMessageDialog(this, "Failed to delete admin data. Admin ID not found."); } } } catch (SQLException ex) { // Handle exceptions ex.printStackTrace(); } } }