0% found this document useful (0 votes)
62 views

Database Project Sourse Code

This Java code defines a class called StudentReg that provides a graphical user interface for student registration. It contains methods for initializing the GUI components, connecting to a MySQL database, retrieving student records from the database to populate a JTable, and handling button clicks for adding, editing and deleting student records.

Uploaded by

Foysal Soron
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Database Project Sourse Code

This Java code defines a class called StudentReg that provides a graphical user interface for student registration. It contains methods for initializing the GUI components, connecting to a MySQL database, retrieving student records from the database to populate a JTable, and handling button clicks for adding, editing and deleting student records.

Uploaded by

Foysal Soron
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

package StudentReg;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Vector;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

public class StudentReg extends javax.swing.JFrame {

public StudentReg() {

initComponents();

table_update();

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jPanel1 = new javax.swing.JPanel();

jLabel2 = new javax.swing.JLabel();


jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

txtname = new javax.swing.JTextField();

txtmobile = new javax.swing.JTextField();

txtcourse = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

jLabel1.setText("Student Registration");

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Registration",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 18))); // NOI18N

jLabel2.setText("Student Name");

jLabel3.setText("Mobile number");

jLabel4.setText("Course");
txtname.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtnameActionPerformed(evt);

});

txtcourse.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

txtcourseActionPerformed(evt);

});

jButton1.setText("Add");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

});

jButton2.setText("Edit");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

});
jButton3.setText("Delete");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addContainerGap()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(jPanel1Layout.createSequentialGroup()

.addComponent(jLabel2)

.addGap(18, 18, 18)

.addComponent(txtname))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel3)
.addComponent(jLabel4))

.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(txtcourse, javax.swing.GroupLayout.PREFERRED_SIZE, 117,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(txtmobile))))

.addContainerGap(26, Short.MAX_VALUE))

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addComponent(jButton1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton2)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton3))))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(23, 23, 23)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)

.addComponent(jLabel3))

.addComponent(txtmobile, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(29, 29, 29)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(txtcourse, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27,
Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton1)

.addComponent(jButton2)

.addComponent(jButton3))

.addContainerGap())

);

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

},

new String [] {

"ID", "Name", "Mobile ", "Course"

){

Class[] types = new Class [] {


java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class

};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

});

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

jTable1MouseClicked(evt);

});

jScrollPane1.setViewportView(jTable1);

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(216, 216, 216)

.addComponent(jLabel1)

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addGroup(layout.createSequentialGroup()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)


.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 430,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 10, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(26, 26, 26)

.addComponent(jLabel1)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(29, 29, 29)

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(30, Short.MAX_VALUE))

);

pack();

setLocationRelativeTo(null);

}// </editor-fold>

private void txtnameActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}

Connection con1;

PreparedStatement insert;

private void table_update()

int c;

try {

Class.forName("com.mysql.jdbc.Driver");

con1 = DriverManager.getConnection("jdbc:mysql://localhost/record","root","");

insert = con1.prepareStatement("select * from record");

ResultSet rs = insert.executeQuery();

ResultSetMetaData Rss = rs.getMetaData();

c = Rss.getColumnCount();

DefaultTableModel Df = (DefaultTableModel)jTable1.getModel();

Df.setRowCount(0);

while(rs.next())

Vector v2 = new Vector();

for(int a=1;a<=c;a++)

v2.add(rs.getString("ID"));

v2.add(rs.getString("Name"));

v2.add(rs.getString("Mobile"));
v2.add(rs.getString("Course"));

Df.addRow(v2);

catch (ClassNotFoundException | SQLException ex) {

Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String Name = txtname.getText();

String Moblie = txtmobile.getText();

String Course = txtcourse.getText();

try {

Class.forName("com.mysql.jdbc.Driver");

con1 = DriverManager.getConnection("jdbc:mysql://localhost/record","root","");

insert = con1.prepareStatement("insert into record(Name,Course,Mobile)values(?,?,?)");

insert.setString(1, Name);

insert.setString(2, Course);

insert.setString(3, Moblie);

insert.executeUpdate();

JOptionPane.showMessageDialog(this,"record Addeddd");

table_update();

txtname.setText("");
txtmobile.setText("");

txtcourse.setText("");

txtname.requestFocus();

catch (ClassNotFoundException | SQLException ex) {

Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

private void txtcourseActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {

DefaultTableModel Df = (DefaultTableModel)jTable1.getModel();

int selectedIndex = jTable1.getSelectedRow();

txtname.setText(Df.getValueAt(selectedIndex,1).toString());

txtmobile.setText(Df.getValueAt(selectedIndex,2).toString());

txtcourse.setText(Df.getValueAt(selectedIndex,3).toString());

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

DefaultTableModel Df = (DefaultTableModel)jTable1.getModel();

int selectedIndex = jTable1.getSelectedRow();

try {

int ID = Integer.parseInt(Df.getValueAt(selectedIndex,0).toString());

String Name = txtname.getText();


String Moblie = txtmobile.getText();

String Course = txtcourse.getText();

Class.forName("com.mysql.jdbc.Driver");

con1 = DriverManager.getConnection("jdbc:mysql://localhost/database project","root","");

insert = con1.prepareStatement("update record set Name= ?,Mobile= ?,Course= ? where ID=?");

insert.setString(1, Name);

insert.setString(2, Moblie);

insert.setString(3, Course);

insert.setInt(4, ID);

insert.executeUpdate();

JOptionPane.showMessageDialog(this,"Record Update");

table_update();

txtname.setText("");

txtmobile.setText("");

txtcourse.setText("");

txtname.requestFocus();

catch (ClassNotFoundException ex) {

Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

catch (SQLException ex) {

Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

DefaultTableModel Df = (DefaultTableModel)jTable1.getModel();

int selectedIndex = jTable1.getSelectedRow();

try {

int ID = Integer.parseInt(Df.getValueAt(selectedIndex,0).toString());

int dialogResult = JOptionPane.showConfirmDialog(null, "Do you want to Delete the


Recors","Warning",JOptionPane.YES_NO_OPTION);

if(dialogResult == JOptionPane.YES_OPTION)

Class.forName("com.mysql.jdbc.Driver");

con1 = DriverManager.getConnection("jdbc:mysql://localhost/database_project","root","");

insert = con1.prepareStatement("delete from record where ID=?");

insert.setInt(1, ID);

insert.executeUpdate();

JOptionPane.showMessageDialog(this,"Record Deleted");

table_update();

txtname.setText("");

txtmobile.setText("");

txtcourse.setText("");

txtname.requestFocus();

catch (ClassNotFoundException ex) {


Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

catch (SQLException ex) {

Logger.getLogger(StudentReg.class.getName()).log(Level.SEVERE, null, ex);

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* 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(StudentReg.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(StudentReg.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {


java.util.logging.Logger.getLogger(StudentReg.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(StudentReg.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

new StudentReg().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JPanel jPanel1;


private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTable jTable1;

private javax.swing.JTextField txtcourse;

private javax.swing.JTextField txtmobile;

private javax.swing.JTextField txtname;

// End of variables declaration

You might also like