Lab 5 Hamza Nazir
Advanced Object-Oriented Programming
LAB REPORT: 5
Submitted By:
Hamza Nazir
(2872)
Submitted To:
Ma’am Syeda Humaira Batool
DATE:
Bscs-40B (4rd Semester) Evening
Tasks:
Objective
Database Connectivity
Code:
Class
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;
Lab 5 Hamza Nazir 1
public class st_info implements ActionListener {
public static void main(String[] args) {
new st_info();
// getconnection();
/// createtable();
}
JFrame f;
JLabel l1;
JLabel l2;
JTextField t1;
JLabel l3;
JTextField t2;
JLabel l4;
JComboBox cb;
JLabel l5;
JTextField t3;
JButton b1;
JButton b2;
st_info()
{
f=new JFrame("SMS");
f.setSize(400,400);
l1=new JLabel("Student Mangment System");
l1.setBounds(50, 30, 200, 40);
f.add(l1);
/// Component
l2 = new JLabel("Std_id");
l2.setBounds(30, 80, 200, 40);
f.add(l2);
/// text field
t1 = new JTextField();
t1.setBounds(100, 80, 200, 20);
f.add(t1);
//
//
//
l3 = new JLabel("Std_Name ");
l3.setBounds(30, 130, 200, 40);
f.add(l3);
t2 = new JTextField();
t2.setBounds(100, 130, 200, 20);
f.add(t2);
l4 = new JLabel("Std_degree");
l4.setBounds(30, 180, 200, 40);
f.add(l4);
/// combobox
String degree[] = {"Select Degree","MS", "BS", "PHD"};
cb = new JComboBox(degree);
cb.setBounds(100, 180, 200, 40);
f.add(cb);
l5 = new JLabel("Std_Cgpa ");
l5.setBounds(30, 230, 200, 40);
f.add(l5);
t3 = new JTextField();
t3.setBounds(100, 230, 200, 40);
f.add(t3);
/// Button
b1= new JButton("Insert");
b1.setBounds(120, 290, 100,30);
f.add(b1);
Lab 5 Hamza Nazir 2
b2= new JButton("Delete");
b2.setBounds(220, 290, 100,30);
f.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
f.setLayout(null);
f.setVisible(true);
String s_id;
String s_name;
String s_cgpa;
String s_degree;
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1)
{
s_id=t1.getText();
s_name=t2.getText();
s_cgpa=t3.getText();
s_degree= cb.getToolTipText();
Connection conn=getconnection();
try {
assert conn != null;
PreparedStatement insert=conn.prepareStatement( "INSERT INTO student_info(st_id,st_name,st_deg,st_gpa) VALUE(?,?,?,?)");
insert.setString(1,s_id );
insert.setString(2,s_name );
insert.setString(3,s_degree );
insert.setString(4,s_cgpa );
insert.executeUpdate();
System.out.println("insert selected");
} catch (Exception ex) {
System.out.println("execption in insertion ");
}
}
if (e.getSource()==b2)
{
}
}
public static void createtable() {
Connection conn = getconnection();
try {
PreparedStatement create = conn.prepareStatement("create table student_info(id int(20) primary key auto_increment, st_id varcha
create.executeUpdate();
} catch (Exception ex) {
System.out.println("execption in creation me ");
}
public static Connection getconnection()
{
System.out.println("-----------enter getconnection function----------");
try{
String driver="com.mysql.cj.jdbc.Driver";
String databseurl="jdbc:mysql://localhost:3306/testjdbc";
// String databseurl="jdbc:mysqul://localhost/phpmyadmin/index.php?route=/server/databases";
String username="root";
String password="";
Class.forName(driver);
Connection conn= DriverManager.getConnection(databseurl,username,password);
System.out.println("Data base connected");
Lab 5 Hamza Nazir 3
return conn;
} catch (Exception e) {
System.out.println("err aya hai connection me:"+e);
}
return null;
}
}
Output
Lab 5 Hamza Nazir 4