You are on page 1of 10

LAB 2

#Student
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
public class hubung extends Frame
{
Connection con;
TextField tf1,tf2;
TextArea t1,t2;
Label l1,l2;
Panel p1,p2,p3,p4;
Button b1,b2,b3,b4,b5,b6,b7,b8;
public hubung() //constructor
{
setLayout(new FlowLayout());
setTitle("paan");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p1.setLayout(new GridLayout(3,3,20,20));
p2.setLayout(new BorderLayout());
p3.setLayout(new GridLayout(2,3,15,15));
tf1 = new TextField("",20);
tf2 = new TextField("",20);
tf2.setEchoChar('*');
t1 = new TextArea("Enter query",5,60);
t2 = new TextArea("Result",5,60);
l1 = new Label("username");
l2 = new Label("Password");
b1 = new Button("connect");
b2 = new Button("Disconnect");
b3 = new Button("Select");
b4 = new Button("Update");
b5 = new Button("Insert");
b6 = new Button("Delete");
b7 = new Button("Clear query");
b8 = new Button("Clear result");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
p2.add("Center",t1);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p2.add("South",p3);
p4.add(t2);
add(p1);
add(p2);

add(p4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
b1.addActionListener(new ConnectDB());
b2.addActionListener(new DisconnectDB());
b3.addActionListener(new Select());
b5.addActionListener(new Insert());
b4.addActionListener(new Update());
b6.addActionListener(new Delete());
b7.addActionListener(new ClearQuery());
b8.addActionListener(new ClearResult());
}
//}
//class ConnectDB
class ConnectDB implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //if using MySQL
//ClassforName("sun.jdbc.odbc.JdbcOdbcDriver"); //if using Ms Access
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/penasihat akademi
k",tf1.getText(),tf2.getText());
t2.setText("Connection success");
}
catch(ClassNotFoundException e2)
{
t2.setText("Class not found");
}
catch(Exception e1)
{
t2.setText("error in connection");
} } }
//class DisconnectDB
class DisconnectDB implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
con.commit();
con.close();
t2.append("Connection closed");
}
catch(Exception e1)
{
t2.setText("error in disconnection");

}
}
}
//class Select
class Select implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
ResultSet rs;
String query;
boolean more;
stmt = con.createStatement();
int count =0;
query = t1.getText();
rs = stmt.executeQuery(query);
more = rs.next();
if(!more)
{
t2.append("No results");
return;
}
t2.append("NO\tAname\tregistration_no\talamat\tcgpa");
while(more)
{
t2.append("\n"+rs.getInt("registration_no")+"\t"+rs.getString("name")+"\t"+rs.ge
tInt( "cgpa")+"\t"+rs.getString("address"));
count++;
more=rs.next();
}
t2.append("\n"+count+" rows selected");
rs.close();
stmt.close();
}
catch(Exception e1)
{
t2.setText("error in selection");
} } }
//class Update Data
class Update implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)
{
t2.append("Table dropped");
}

else
{
t2.append("\n"+rows+ " rows updated");
}
con.commit();
stmt.close();
}
catch(Exception e1)
{
}
}
}
//class Insert Data
class Insert implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)
{
t2.append("Table dropped");
}
else
{
t2.append("\n"+rows+ " rows inserted");
}
con.commit();
stmt.close();
}
catch(Exception e1)
{
}
}
}
//class Delete Data
class Delete implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)

{
t2.append("Table dropped");
}
else
{
t2.append("\n"+rows+ " row deleted");
}
con.commit();
stmt.close();
}
catch(Exception e1)
{
}
}
}
//class ClearQuery
class ClearQuery implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t1.setText("");
}
}
//class ClearResult
class ClearResult implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t2.setText("");
}
}
//main method for class Connect
public static void main(String[] args)
{
hubung c = new hubung();
c.setVisible(true);
c.setSize(300,400);
}
}

#Lecturer
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;
public class lecturer_pa extends Frame
{
Connection con;
TextField tf1,tf2;
TextArea t1,t2;

Label l1,l2;
Panel p1,p2,p3,p4;
Button b1,b2,b3,b4,b5,b6,b7,b8;
public lecturer_pa() //constructor
{
setLayout(new FlowLayout());
setTitle("ekin");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p1.setLayout(new GridLayout(3,3,20,20));
p2.setLayout(new BorderLayout());
p3.setLayout(new GridLayout(2,3,15,15));
tf1 = new TextField("",20);
tf2 = new TextField("",20);
tf2.setEchoChar('*');
t1 = new TextArea("Enter query",5,60);
t2 = new TextArea("Result",5,60);
l1 = new Label("username");
l2 = new Label("Password");
b1 = new Button("connect");
b2 = new Button("Disconnect");
b3 = new Button("Search");
b4 = new Button("Update");
b5 = new Button("Save");
b6 = new Button("Delete");
b7 = new Button("Clear query");
b8 = new Button("Clear result");
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(tf2);
p1.add(b1);
p1.add(b2);
p2.add("Center",t1);
p3.add(b3);
p3.add(b4);
p3.add(b5);
p3.add(b6);
p3.add(b7);
p3.add(b8);
p2.add("South",p3);
p4.add(t2);
add(p1);
add(p2);
add(p4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
b1.addActionListener(new ConnectDB());
b2.addActionListener(new DisconnectDB());

b3.addActionListener(new Search());
b5.addActionListener(new Save());
b4.addActionListener(new Update());
b6.addActionListener(new Delete());
b7.addActionListener(new ClearQuery());
b8.addActionListener(new ClearResult());
}
//}
//class ConnectDB
class ConnectDB implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Class.forName("com.mysql.jdbc.Driver"); //if using MySQL
//ClassforName("sun.jdbc.odbc.JdbcOdbcDriver"); //if using Ms Access
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/penasihat akademi
k",tf1.getText(),tf2.getText());
t2.setText("Connection success");
}
catch(ClassNotFoundException e2)
{
t2.setText("Class not found");
}
catch(Exception e1)
{
t2.setText("error in connection");
} } }
//class DisconnectDB
class DisconnectDB implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
con.commit();
con.close();
t2.append("Connection closed");
}
catch(Exception e1)
{
t2.setText("error in disconnection");
}
}
}
//class Select
class Search implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;

ResultSet rs;
String query;
boolean more;
stmt = con.createStatement();
int count =0;
query = t1.getText();
rs = stmt.executeQuery(query);
more = rs.next();
if(!more)
{
t2.append("No results");
return;
}
t2.append("NO\tname\tstaff_id\tphone_no");
while(more)
{
t2.append("\n"+rs.getString("name")+"\t"+rs.getString("staff_id")+"\t"+rs.getStr
ing( "phone_no"));
count++;
more=rs.next();
}
t2.append("\n"+count+" rows selected");
rs.close();
stmt.close();
}
catch(Exception e1)
{
t2.setText("error in selection");
} } }
//class Update Data
class Update implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)
{
t2.append("Table dropped");
}
else
{
t2.append("\n"+rows+ " rows updated");
}
con.commit();
stmt.close();
}
catch(Exception e1)
{
}
}

}
//class Insert Data
class Save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)
{
t2.append("Table dropped");
}
else
{
t2.append("\n"+rows+ " rows inserted");
}
con.commit();
stmt.close();
}
catch(Exception e1)
{
}
}
}
//class Delete Data
class Delete implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
Statement stmt;
String query;
int rows;
stmt = con.createStatement();
query = t1.getText();
rows = stmt.executeUpdate(query);
if(rows == -1)
{
t2.append("Table dropped");
}
else
{
t2.append("\n"+rows+ " row deleted");
}
con.commit();
stmt.close();
}
catch(Exception e1)

{
}
}
}
//class ClearQuery
class ClearQuery implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t1.setText("");
}
}
//class ClearResult
class ClearResult implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
t2.setText("");
}
}
//main method for class Connect
public static void main(String[] args)
{
lecturer_pa c = new lecturer_pa();
c.setVisible(true);
c.setSize(300,400);
}
}

You might also like