Except JSP, Servlet and HTML!

T2015_16

import java.sql.*;

public class T2015_16 {

    static Connection con;

    static Statement s;

    static ResultSet rs;

    public static void main(String[] args) throws Exception{

        deleteRecord();

    }

    public static void deleteRecord() throws Exception{

        Class.forName("com.mysql.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/fomdb";

        con=DriverManager.getConnection(url,"root","");

        if(con!=null){

            s=con.createStatement();

            String sql="DELETE FROM employee WHERE age>60";

            int res=s.executeUpdate(sql);

            if(res!=-1){

                rs=s.executeQuery("SELECT id,name,salary,post,age FROM employee");

                System.out.println("id    name    salary    post    age");

                while(rs.next()){

                    System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"   "+rs.getInt(3)+"   "+rs.getString(4)+"   "+rs.getInt(5));

                  System.out.println("___________________________________");

                }

            }

            else{

                System.out.println("Error in deleting records");  

            }     

        }

        else{

            System.out.println("Error connecting to database!");

        }

    }

}

 

T2015_17

import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.event.*;

public class T2015_17 extends JFrame implements ActionListener{

    JTextField t1,t2;

    JButton b;

    public T2015_17(){

        t1=new JTextField();

        t2=new JTextField();

        t2.setEnabled(false);

        b=new JButton("Reverse");

        setLayout(new GridLayout(3,1));

        add(t1); add(t2); add(b);

        setSize(200,400);

        setVisible(true);

        b.addActionListener(this);    

    }

    public void actionPerformed(ActionEvent e){

        int n=Integer.parseInt(t1.getText());

        int sum=0;

        while(n!=0){

           int r=n%10;

           sum+=r;

           n=n/10;

        }

        t2.setText("Sum is: "+sum);

    }

    public static void main(String[] args) {

        new T2015_17();

    }

}

 

T2016_2

 import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.event.*;

public class T2016_2 extends JFrame implements ActionListener{

    JTextField t1,t2;

    JLabel sum;

    JButton b;

    public T2016_2(){

        t1=new JTextField();

        t2=new JTextField();

        sum=new JLabel("");

        b=new JButton("Add");

        setLayout(new GridLayout(4,1));

        add(t1); add(t2); add(b); add(sum);

        setSize(200,400);

        setVisible(true);

        b.addActionListener(this);    

    }

    public void actionPerformed(ActionEvent e){

        int n1=Integer.parseInt(t1.getText());

        int n2=Integer.parseInt(t2.getText());

        int s=n1+n2;

        sum.setText("Sum is: "+s);

    }

    public static void main(String[] args) {

        new T2016_2();

    }

}

 

T2016_3

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class T2016_3 extends Applet implements KeyListener{

    TextField t1;

    Label lab;

    public void init(){

        t1=new TextField();

        add(t1);

        lab=new Label();

        add(lab);

        setSize(300,300);

        setLayout(new GridLayout(2,1));

    }

    public void start(){

        t1.addKeyListener(this);

    }

    public void keyTyped(KeyEvent e){}

    public void keyPressed(KeyEvent e){}

    public void keyReleased(KeyEvent e){

        String a=t1.getText();

        if(a.length()==5){

            lab.setText(a+" is of length 5");

        }

        else{

            lab.setText(a+" is not of length 5");

        }

    }

}

 

T2016_6

import java.sql.*;

public class T2016_6 {

    static Connection con;

    public static void main(String[] args) throws Exception{

        connect();

    }

    public static void connect() throws Exception{

        Class.forName("com.mysql.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/fomdb";

        con=DriverManager.getConnection(url,"root","");

        if(con!=null){

            System.out.println("Successfully connected to database");      

        }

        else{

            System.out.println("Error connecting to database!");

        }

    }

}

 

T2016_7

import java.awt.*;

public class T2016_7 extends Frame{

    Label l1,l2;

    Panel p1,p2;

    TextField t1;

    CheckboxGroup cb;

    Checkbox a,b,c;

    public T2016_7() {

      l2=new Label("Gender: ");

      cb=new CheckboxGroup();

      a=new Checkbox("Male",true,cb);

      b=new Checkbox("Female",false,cb);

      c=new Checkbox("Other",false,cb);

      add(l2); add(a); add(b); add(c);

      setLayout(new FlowLayout());

      setSize(400,200);

      setVisible(true);

    }

    public static void main(String[] args) {

        new T2016_7();

    }

}

 

T2017_2

import java.sql.*;

public class T2017_2 {

    static Connection con;

    static Statement s;

    static ResultSet rs;

    public static void main(String[] args) throws Exception{

        displayRecord();

    }

    public static void displayRecord() throws Exception{

        Class.forName("com.mysql.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/testdb";

        con=DriverManager.getConnection(url,"root","");

        if(con!=null){

            rs=s.executeQuery("SELECT id,name,post,salary FROM employee");

            System.out.println("id          |           name            |           post            |           salary");

            while(rs.next()){

                System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"   "+rs.getString(3)+"   "+rs.getInt(4));

              System.out.println("____________________________________");

            }

        }   

        else{

            System.out.println("Error connecting to database!");

        }

    }

}

 

T2017_5

import java.applet.*;

import java.awt.event.*;

import java.awt.*;

public class T2017_5 extends Applet implements ActionListener{

    Panel p1,p2;

    Label l1,l2,l3;

    TextField t1,t2,t3;

    Button b1,b2;

    public void init(){

        p1=new Panel();

        p2=new Panel();

        p1.setLayout(new GridLayout(3,2));

        p2.setLayout(new FlowLayout());

        l1=new Label("Number 1");

        l2=new Label("Number 2");

        l3=new Label("Result");

        t1=new TextField();

        t2=new TextField();

        t3=new TextField();

        t3.setEditable(false);




        p1.add(l1); p1.add(t1);

        p1.add(l2); p1.add(t2);

        p1.add(l3); p1.add(t3);

        

        add(p1); add(p2);

        

        b1=new Button("+");

        b2=new Button("-");

        p2.add(b1); p2.add(b2);

    }

       

    public void start(){

        b1.addActionListener(this);

        b2.addActionListener(this);

    }

       

    public void actionPerformed(ActionEvent e){

       int a=Integer.parseInt(t1.getText());

       int b=Integer.parseInt(t2.getText());

       if(e.getSource()==b1){

        int c=a+b;

        t3.setText(" "+c);

       }

       else if(e.getSource()==b2){

        int c=a-b;

        t3.setText(" "+c);

       } 




   }

}

 

T2017_6

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

public class T2017_6 extends JFrame{

    TextField t1;

    JComboBox<String> combo;

    String[] s={"BIM","BHM","BSc. CSIT","BBM","BCA"};

    public T2017_6(){

        t1=new TextField();

        t1.setEditable(false);

        combo=new JComboBox<String>(s);

        add(combo);

        add(t1);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        combo.addItemListener(new ItemListener(){

            public void itemStateChanged(ItemEvent e){

                t1.setText(""+combo.getSelectedItem());

            }

        });

        setLayout(new GridLayout(2,1));

        setSize(100,100);

        setVisible(true);

    }

    public static void main(String[] args) {

        new T2017_6();

    }

}

 

T2018_4

 import java.sql.*;

public class T2018_4 { 

    private static Connection con;

    private static Statement s;

    public static void main(String[] args) throws Exception{

       insertRecords();

    }

    public static void insertRecords() throws Exception{

        Class.forName("com.mysql.jdbc.Driver");

        String url="jdbc:mysql://localhost:3306/Library";

        con=DriverManager.getConnection(url,"root","");

        if(con!=null){

            s=con.createStatement();

            String sql="INSERT INTO book (id,title,author.price) VALUES "

                    + "(25,'AbcBook','AbcAuthor',2500),"

                    + "(47,'MnoBook','MnoAuthor',800),"

                    + "(33,'PqrBook','PqrAuthor',625),"

                    + "(49,'XyzBook','XyzAuthor',1200),"

                    + "(11,'StuBook','StuAuthor',1200) ";

            int res=s.executeUpdate(sql);

            if(res!=-1){

                System.out.println("Successfully inserted in table BOOK");

            }

            else{

                System.out.println("Could not insert into table");

            } 

        }

        else{

            System.out.println("Error in connectiong to database!");

        }

    }

}

 

T2018_5

import java.awt.*;

import java.applet.*;

/*

<applet code="T2018_5.class" height="300" width="500">

<param name="name" value="Laxmi" />

<param name="age" value="20" />

</applet>

*/

public class T2018_5 extends Applet 

{

String n;

String a;

public void init(){

        n=getParameter("name");

        a=getParameter("age");

}

public void paint(Graphics g){

        g.drawString("Name is: " + n, 20, 20);

        g.drawString("Age is: " + a, 20, 40);

}

}

 

T2018_6

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class T2018_6 extends JFrame implements ActionListener{

    JTextField t1;

    JButton b1,b2;

    public T2018_6() {

        t1=new JTextField();

        b1=new JButton("ok");

        b2=new JButton("clear");

        add(t1); t1.setEditable(false);

        t1.setPreferredSize(new Dimension(100,20));

        add(b1);

        add(b2);

        setLayout(new FlowLayout());

        setSize(50,200);

        setVisible(true);

        b1.addActionListener(this);

        b2.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource()==b1){

            t1.setText("Welcome");

        }

        else if(e.getSource()==b2){

            t1.setText("");

        }

    }

    public static void main(String[] args) {

        new T2018_6();

    } 

}

 


Didn't Find Any Subjects/Contents?

Click on the contribute button to contribute subjects materials on Study Notes Nepal.

Contribute

Leave a Reply

Your email address will not be published. Required fields are marked *


Join Our Facebook Community Group

Study Notes Nepal