import javax.swing.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class Student_data extends javax.swing.JFrame {
/**
* Creates new form Student_data
*/
public Student_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() {
insert_btn = new javax.swing.JButton();
view_btn = new javax.swing.JButton();
back_btn = new javax.swing.JButton();
search_btn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
insert_btn.setBackground(new java.awt.Color(102, 153, 255));
insert_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
insert_btn.setForeground(new java.awt.Color(255, 255, 255));
insert_btn.setText("insert");
insert_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insert_btnActionPerformed(evt);
}
});
view_btn.setBackground(new java.awt.Color(51, 153, 0));
view_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
view_btn.setForeground(new java.awt.Color(255, 255, 255));
view_btn.setText("view");
view_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
view_btnActionPerformed(evt);
}
});
back_btn.setBackground(new java.awt.Color(153, 153, 255));
back_btn.setFont(new java.awt.Font("Segoe UI", 3, 12)); // NOI18N
back_btn.setForeground(new java.awt.Color(255, 255, 255));
back_btn.setText("<");
back_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
back_btnActionPerformed(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(65, 65, 65)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(back_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(insert_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(view_btn, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
.addComponent(search_btn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(37, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(view_btn)
.addComponent(insert_btn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(back_btn)
.addComponent(search_btn))
.addContainerGap(192, Short.MAX_VALUE))
);
pack();
}// //GEN-END:initComponents
private void insert_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insert_btnActionPerformed
// JDBC connection parameters
String id = JOptionPane.showInputDialog(this, "Enter Student ID:");
String name = JOptionPane.showInputDialog(this, "Enter Student Name:");
String email = JOptionPane.showInputDialog(this, "Enter Student Email:");
// Perform insertion logic into the database
String url = "jdbc:mysql://localhost:3306/login_form";
String user = "root";
String pass = "";
try (Connection connection = DriverManager.getConnection(url, user, pass)) {
String query = "INSERT INTO student_table (id, name, email) VALUES (?, ?, ?)";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setString(1, id);
preparedStatement.setString(2, name);
preparedStatement.setString(3, email);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "Data inserted successfully!");
} else {
JOptionPane.showMessageDialog(this, "Failed to insert data!");
}
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}//GEN-LAST:event_insert_btnActionPerformed
private void view_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_view_btnActionPerformed
student_view_data svd = new student_view_data();
svd.setVisible(true);
// Close or hide the registration form
this.dispose(); }//GEN-LAST:event_view_btnActionPerformed
private void back_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_btnActionPerformed
welcome ad = new welcome();
ad.setVisible(true);
// Close or hide the registration form
this.dispose(); // TODO add your handling code here:
}//GEN-LAST:event_back_btnActionPerformed
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 student_table based on the search term
String query = "SELECT * FROM student_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 studentId = resultSet.getString("id");
String studentName = resultSet.getString("name");
String studentEmail = resultSet.getString("email");
// Append the retrieved data to the message
resultMessage.append("Student ID: ").append(studentId)
.append(", Name: ").append(studentName)
.append(", Email: ").append(studentEmail).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(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
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 | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student_data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Student_data().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton back_btn;
private javax.swing.JButton insert_btn;
private javax.swing.JButton search_btn;
private javax.swing.JButton view_btn;
// End of variables declaration//GEN-END:variables
}