Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- /*
- <applet code="DbDemo.class" height="500" width="500">
- </applet>
- */
- public class DbDemo extends Applet implements ActionListner
- {
- Label lblname,lblroll;
- Button ins,update,del,search;
- TextField Txtnm,Txtroll;
- public void init()
- {
- lblname=new Label("enter name");
- lblroll=new Label("enter roll");
- ins=new Button("insert");
- update=new Button("update");
- del=new Button("delete");
- search=new Button("search");
- Txtnm=new TextField();
- txtroll=new TextField();
- add(lblname);
- add(Txtnm);
- add(lblroll);
- add(Txtroll);
- add(ins);
- add(update);
- add(del);
- add(search);
- ins.add ActionListner(this);
- update.add ActionListner(this);
- del.add ActionListner(this);
- search.add ActionListner(this);
- }
- public void actionPerformed(ActionEvent ae)
- {
- String lbl=ae.getActionCommand();
- String n=Txtnm.getText();
- String r=Txtroll.getText();
- if(lbl.equals("insert"))
- {
- DbMain d=new Dbmain();
- d.insert(n,r);
- }
- if(lbl.equals("update"))
- {
- DbMain d=new Dbmain();
- d.update(n,r);
- }
- if(lbl.equals("delete"))
- {
- DbMain d=new Dbmain();
- d.delete(r);
- }
- if(lbl.equals("search"))
- {
- DbMain d=new Dbmain();
- d.search();
- }
- }
- }
- class DbMain
- {
- DbMain(){
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/stud?zeroDateTimeBehavior=convertToNull", "Stud",
- "");
- Statement stmt=conn.createStatement();
- String query="select * from Stud";
- ResultSet rs=stmt.executeQuery(query);
- }
- catch(SQLException e1)
- {
- System.out.println("Error"+e1);
- }
- catch(ClassNotFoundException e1)
- {
- System.out.println("Error"+e1);
- }
- }
- public void search()
- {
- String query="select * from Stud";
- try
- {
- rs=stmt.executeQuery(query);
- while(rs.next())
- {
- int r=rs.getInt("Roll");
- String n=rs.getString("Name");
- System.out.println("Name="+n+"\t"+"Roll ="+r);
- }
- }
- catch(Exception e1)
- {
- System.out.println("Error"+e1);
- }
- }
- public void insert(int r,string n)
- {
- try
- {
- String q="insert into Stud values("+r+",'"+n+";)";
- int cnt=stmt.executeUpdate(q);
- if(cnt>0)
- { System.out.println("inserted");}
- else
- {System.out.println("not inserted");}
- }
- catch(Exception e)
- {System.out.println("error:"+e);}
- }
- public void delete(int r)
- {
- try
- {
- String q="delete from Stud where roll="+r+"";
- int cnt=stmt.executeUpdate(q);
- if(cnt>0)
- { System.out.println("deleted");}
- else
- {System.out.println("not not deleted");}
- }
- catch(Exception e)
- {System.out.println("error:"+e);}
- }
- public void update(int r,String n)
- {
- try
- {
- String q="update Stud where set name='"+n+"' where roll="+r+"";
- int cnt=stmt.executeQuery(q);
- if(cnt>0)
- { System.out.println("updated");}
- else
- {System.out.println("not updated");}
- }
- catch(Exception e)
- {System.out.println("error:"+e);}
- }
- }
Add Comment
Please, Sign In to add comment