Вы находитесь на странице: 1из 5

AIM: JDBC Program ********************************************************* import java.io.*; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.sql.

*; /*<applet code="menu.class" width=500 height=500> </applet>*/ public class menu1 extends Frame implements WindowListener,ActionListener { MenuBar mb; MenuItem stu1dent,rollnowise,namewise,allresult; public static menu1 m; student st; int x,y,d; public menu1() { super("menu"); addWindowListener(this); x=y=500; setSize(x,y); addMenu(); show(); } public static void main(String args[]) { m=new menu1(); } void addMenu() { MenuBar mb=new MenuBar(); Menu register=new Menu("REGISTER"); register.add("STUDENT"); register.add("EXIT"); mb.add(register); setMenuBar(mb); register.addActionListener(this); } public void actionPerformed(ActionEvent ae) { String arg=ae.getActionCommand(); if(ae.getSource() instanceof Menu) if(arg.equals("EXIT")) { System.exit(0); } if(ae.getSource() instanceof Menu) if("STUDENT".equals(arg)) { st=new student(); st.show(); } } public void windowClosed(WindowEvent we){} public void windowDeiconified(WindowEvent we){} public void windowIconified(WindowEvent we){}

public void windowActivated(WindowEvent we){} public void windowDeactivated(WindowEvent we){} public void windowOpened(WindowEvent we){} public void windowClosing(WindowEvent we) { dispose(); System.exit(0); } } //class which help in storing records in the database class student extends Frame implements ActionListener,WindowListener { public static student st; TextField tf_name=new TextField(20); TextField tf_rollno=new TextField(20); TextField tf_colg=new TextField(20); Label l2=new Label("ROLLNO"); Label l1=new Label("NAME"); Label l4=new Label("COLG"); Button but_add=new Button("ADD"); Button but_edit=new Button("EDIT"); Button but_find=new Button("FIND"); Button but_delete=new Button("DELETE"); Button but_cancel=new Button("RESET"); Button ok=new Button("OK"); Dialog dlg; Label msg; int x,y,d; public student() { super("palce"); addWindowListener(this); setLayout(new GridLayout(6,1)); //setBackground(Color.CYAN); setVisible(true); addmenu(); x=400; y=400; d=12; setSize(x,y); show(); } void addmenu() { Panel p1=new Panel(); p1.add(l1); p1.add(tf_name); p1.add(l2); p1.add(tf_rollno); Panel p3=new Panel(); p3.add(but_add); p3.add(but_find); p3.add(but_cancel); p3.add(but_edit); p3.add(but_delete); Panel p5=new Panel(); p5.add(l4); p5.add(tf_colg); add(p1); add(p5);

add(p3); but_add.addActionListener(this); but_cancel.addActionListener(this); but_find.addActionListener(this); but_delete.addActionListener(this); but_edit.addActionListener(this); ok.addActionListener(this); //Dialog for confirmation dlg=new Dialog(this,"Inventory Management System",false); dlg.setLayout(new GridLayout(2,1)); dlg.setSize(100,100); dlg.setLocation(200,100); ok.setSize(50,50); msg=new Label("Record Updated"); dlg.add(msg); dlg.add(ok); } public void actionPerformed(ActionEvent e) { String arg=e.getActionCommand(); if(e.getSource() instanceof Button) if("ADD".equals(arg)) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:stu1"); Statement st; String sqlStr; sqlStr="insert into stu1(NAME,ROLLNO,COLG)values('"+tf_name.getText()+"',"+tf_ rollno.getText()+",'"+tf_colg.getText()+"')"; st=con.createStatement(); st.executeUpdate(sqlStr); msg.setText("REGISTERED SUCCESSFULLY"); dlg.show(); } catch(ClassNotFoundException se) { msg.setText("ERROR"); dlg.show(); } catch(SQLException se) { msg.setText("ENTER TEXTFIELD"); dlg.show(); } if ( e.getSource() instanceof Button) if ("OK".equals(arg)) { dlg.dispose(); } if(e.getSource() instanceof Button) if("RESET".equals(arg)) { tf_name.setText(""); tf_rollno.setText(""); tf_colg.setText(""); } if(e.getSource() instanceof Button) if("FIND".equals(arg)) try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:stu1"); Statement st; String sqlstr; sqlstr="select * from stu1 where ROLLNO ="+tf_rollno.getText()+""; st=con.createStatement(); ResultSet rs; rs=st.executeQuery(sqlstr); rs.next(); tf_name.setText(""+rs.getString("NAME")); tf_colg.setText(""+rs.getString("COLG")); } catch(ClassNotFoundException se) { msg.setText("RECORD NOT FOUND"); dlg.show(); } catch(SQLException se) { msg.setText("RECORD NOT FOUND"); dlg.show(); } if(e.getSource() instanceof Button) if("DELETE".equals(arg)) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:stu1"); Statement st; String sqlstr; sqlstr="delete * from stu1 where ROLLNO="+tf_rollno.getText()+""; st=con.createStatement(); st.executeUpdate(sqlstr); tf_name.setText(""); tf_colg.setText(""); tf_rollno.setText(""); msg.setText("RECORD DELETED"); dlg.show(); } catch(ClassNotFoundException se) { tf_name.setText("Error : " + se.toString()); } catch(SQLException se) { tf_name.setText("Error : " + se.toString()); } if(e.getSource() instanceof Button) if("EDIT".equals(arg)) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:stu1"); Statement st; String sqlstr; sqlstr="update stu1 set NAME='"+tf_name.getText()+"',COLG='"+tf_colg.getText ()+"' where ROLLNO="+tf_rollno.getText(); st=con.createStatement(); st.executeUpdate(sqlstr); msg.setText("RECORD UPDATED");

dlg.show(); } catch(ClassNotFoundException se) { tf_name.setText("Error : " + se.toString()); } catch(SQLException se) { tf_name.setText("Error : " + se.toString()); } } public void windowClosed(WindowEvent we){} public void windowDeiconified(WindowEvent we){} public void windowIconified(WindowEvent we){} public void windowActivated(WindowEvent we){} public void windowDeactivated(WindowEvent we){} public void windowOpened(WindowEvent we){} public void windowClosing(WindowEvent we) { dispose(); System.exit(0); }}

OUTPUT:

Вам также может понравиться