0% found this document useful (0 votes)
19 views3 pages

Midexam

This Java code defines a class called classWork1 that extends JFrame and implements ItemListener and ActionListener interfaces. It creates a GUI with panels, combo boxes, text fields, buttons to register and display student course registration details by connecting to a MySQL database. When items are selected from the combo boxes, it retrieves related data from the database and displays in text fields. The register button inserts new records into the student table, and display button retrieves and shows records in a JTable.

Uploaded by

Amanuel Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Midexam

This Java code defines a class called classWork1 that extends JFrame and implements ItemListener and ActionListener interfaces. It creates a GUI with panels, combo boxes, text fields, buttons to register and display student course registration details by connecting to a MySQL database. When items are selected from the combo boxes, it retrieves related data from the database and displays in text fields. The register button inserts new records into the student table, and display button retrieves and shows records in a JTable.

Uploaded by

Amanuel Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package mid_exam;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class classWork1 extends JFrame implements ItemListener,ActionListener{
JPanel np,wp,cp;
JComboBox cname,gender;
JTextField chour,batch,sem,name;
JSpinner age;
JButton register,display;
JTable mytable;
JScrollPane jsp;
Connection conn;
Statement stmt;
ResultSet rs;
String url="jdbc:mysql://localhost/campus";
String user="root";
String password="";

public classWork1(){

np=new JPanel(new FlowLayout());


JLabel title=new JLabel("Student Course Registration");
np.add(title);
this.add(np,BorderLayout.NORTH);

wp=new JPanel(new GridLayout(12,0,5,10));


cname=new JComboBox();
// cname.addItemListener(this);
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="select Course_Name from course_table";
rs=stmt.executeQuery(sql);

while(rs.next()){
cname.addItem(rs.getString("Course_Name"));
}

}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
cname.setSelectedIndex(-1);
wp.add(cname);
cname.addItemListener(this);

chour=new JTextField(20);
wp.add(chour);
batch=new JTextField(20);
wp.add(batch);
sem=new JTextField(20);
wp.add(sem);
name=new JTextField(20);
wp.add(name);
SpinnerNumberModel value=new SpinnerNumberModel(18,18,25,1);
age=new JSpinner(value);
wp.add(age);
String g[]={"Male","Female","Unknown"};
gender=new JComboBox(g);
wp.add(gender);
register=new JButton("Register");
register.addActionListener(this);
wp.add(register);
display=new JButton("Display");
display.addActionListener(this);
wp.add(display);
wp.setVisible(true);
this.getContentPane().add(wp,BorderLayout.WEST);

setVisible(true);
setSize(800,600);
setLocation(300,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void itemStateChanged(ItemEvent ie){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="SELECT * FROM `course_table` where
Course_Name='"+cname.getSelectedItem()+"'";
rs=stmt.executeQuery(sql);
while(rs.next()){
chour.setText(rs.getString("Credit_Hour"));
batch.setText(rs.getString("Batch"));
sem.setText(rs.getString("Semster"));
}
} catch (Exception ex) {
Logger.getLogger(classWork1.class.getName()).log(Level.SEVERE, null,
ex);
}

}
@Override
public void actionPerformed(ActionEvent ae){

if(ae.getSource()==register){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="insert into
student_table(Course_Name,Credit_Hour,Batch,Semster,Name,Age,Gender)"
+ "values('"+cname.getSelectedItem()+"','"+chour.getText()
+"','"+batch.getText()+"','"+sem.getText()+"','"+name.getText()
+"','"+age.getValue()+"','"+gender.getSelectedItem()+"')";
int status=stmt.executeUpdate(sql);
if(status > 0){
JOptionPane.showMessageDialog(null, "Inserted");
cp.setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "Not Inserted");
cp.setVisible(false);
}

}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
cp.setVisible(false);
}
}

if(ae.getSource()==display){
try{
DefaultTableModel model=new DefaultTableModel();
model.setColumnIdentifiers(new Object[]{"Course Name","Credit
Hour","Name","Age","Gender"});
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="SELECT * FROM `student_table`";
rs=stmt.executeQuery(sql);
while(rs.next()){
model.addRow(new Object[]
{rs.getString("Course_Name"),rs.getString("Credit_Hour"),rs.getString("Name"),rs.ge
tInt("Age"),rs.getString("Gender")});
}
mytable=new JTable();
mytable.setModel(model);
jsp=new JScrollPane(mytable);
cp = new JPanel();
cp.add(jsp);
this.getContentPane().add(cp,BorderLayout.CENTER);

}
catch(Exception ex){
JOptionPane.showMessageDialog(null, ex);
}
}
}
public static void main(String[]args){

new classWork1();
}

You might also like