import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class AddStudent1 extends Frame implements ActionListener,ItemListener
{
Label l1,l2,l3,l4,l5;
Choice ch1,ch2;
TextField t1,t2;
Button b1,b2,b3,b4,b5,b6;
String cnm;
int sem;
AddStudent1()
{
l1=new Label("Roll No. ");
l2=new Label("Name ");
l3=new Label("Course ");
l4=new Label("Sem ");
l5=new Label(" ");
ch1=new Choice();
ch1.addItem("BCA");
ch1.addItem("BBA");
ch1.addItem("BTech");
ch1.addItem("MBA");
ch1.addItem("MCA");
ch2=new Choice();
for(int i=1;i<=8;i++)
ch2.addItem(String.valueOf(i));
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Save");
b2=new Button("delete");
b3=new Button("cancle");
b4=new Button("clear");
b5=new Button("select");
b6=new Button("Update");
setLayout(new FlowLayout());
setBackground(Color.yellow);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(ch1);
add(l4);
add(ch2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l5);
add(b5);
add(b6);
//b1.addActionListener(this);
b2.addActionListener(this);
//b3.addActionListener(this);
//b4.addActionListener(this);
//b5.addActionListener(this);
b6.addActionListener(this);
ch1.addItemListener(this);
ch2.addItemListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
String qry="Insert into table1 values("+Integer.parseInt(t1.getText())+",'"+t2.getText()+"','"+cnm+"',"+sem+")";
int ans=st.executeUpdate(qry);
if(ans==1)
System.out.println("New Student Added Successfully !! ");
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}
if(ae.getSource()==b6)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select * from table1 where rollno="+t1.getText()+"");
while(res.next())
{
String s=res.getString(0);
t2.setText(s);
}
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch1)
{
cnm=String.valueOf(ie.getItem());
}
if(ie.getSource()==ch2)
{
sem=Integer.parseInt(String.valueOf(ie.getItem()));
}
}
public static void main(String a[])
{
AddStudent as=new AddStudent();
as.setSize(300,200);
as.setVisible(true);
as.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
import java.awt.event.*;
import java.sql.*;
public class AddStudent1 extends Frame implements ActionListener,ItemListener
{
Label l1,l2,l3,l4,l5;
Choice ch1,ch2;
TextField t1,t2;
Button b1,b2,b3,b4,b5,b6;
String cnm;
int sem;
AddStudent1()
{
l1=new Label("Roll No. ");
l2=new Label("Name ");
l3=new Label("Course ");
l4=new Label("Sem ");
l5=new Label(" ");
ch1=new Choice();
ch1.addItem("BCA");
ch1.addItem("BBA");
ch1.addItem("BTech");
ch1.addItem("MBA");
ch1.addItem("MCA");
ch2=new Choice();
for(int i=1;i<=8;i++)
ch2.addItem(String.valueOf(i));
t1=new TextField(15);
t2=new TextField(15);
b1=new Button("Save");
b2=new Button("delete");
b3=new Button("cancle");
b4=new Button("clear");
b5=new Button("select");
b6=new Button("Update");
setLayout(new FlowLayout());
setBackground(Color.yellow);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(ch1);
add(l4);
add(ch2);
add(b1);
add(b2);
add(b3);
add(b4);
add(l5);
add(b5);
add(b6);
//b1.addActionListener(this);
b2.addActionListener(this);
//b3.addActionListener(this);
//b4.addActionListener(this);
//b5.addActionListener(this);
b6.addActionListener(this);
ch1.addItemListener(this);
ch2.addItemListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
String qry="Insert into table1 values("+Integer.parseInt(t1.getText())+",'"+t2.getText()+"','"+cnm+"',"+sem+")";
int ans=st.executeUpdate(qry);
if(ans==1)
System.out.println("New Student Added Successfully !! ");
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}
if(ae.getSource()==b6)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("Error is "+cnfe.getMessage());
}
try
{
Connection con=DriverManager.getConnection("jdbc:odbc:Info");
Statement st=con.createStatement();
ResultSet res=st.executeQuery("select * from table1 where rollno="+t1.getText()+"");
while(res.next())
{
String s=res.getString(0);
t2.setText(s);
}
}
catch(SQLException se)
{
System.out.println("Error in database :: " +se.getMessage());
}
}}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource()==ch1)
{
cnm=String.valueOf(ie.getItem());
}
if(ie.getSource()==ch2)
{
sem=Integer.parseInt(String.valueOf(ie.getItem()));
}
}
public static void main(String a[])
{
AddStudent as=new AddStudent();
as.setSize(300,200);
as.setVisible(true);
as.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
No comments:
Post a Comment