Write a Java program to display the record of Student(rollNo, name, marks) on the screen by selecting rollno from the choice component.


Write a Java program to display the record of Student(rollNo, name, marks) on the screen by selecting rollno from the choice component.



Program:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.io.*;

public class Student extends Frame implements ActionListener {
    Choice rollNo;
    Connection con;
    Statement stmt;
    ResultSet rs;
    String sql;
    public Student() throws SQLException, ClassNotFoundException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:patientDSN");
        stmt = con.createStatement();
        rs.executeQuery("Select * from student");
        setSize(300,200);
        setVisible(true);
        setLayout(new FlowLayout());
        addWindowListener(new WClose());
        rollNo.ItemListener(this);
        while(rs.next()){
            rollNo.addItem(String.valueOf(rs.getInt(1)));
        }
        add(rollNo);
    }
    stmt.close();
    con.close();
    rs.close();

    public static void main(String args[]){
        new Student();
    }

    public void ItemStateChanged(ItemEvent ie){
        int rno;
        try {
            rno = Integer.parseInt(rollNo.getSelectedItem());
            sql = "select rno, name, marks from student where rno ="+rno;
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                sql="[RollNo: "+rs.getInt(1)+", Name: "+rs.getString(2)+", Marks: "+rs.getFloat(3)+"]";
            }
            repaint();
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public void paint(Graphics g) {
        Font f = new Font("Arial",Font.plain, 20);
        g.setFont(f);
        g.drawString(sql, 150,150);
    }
}

class WClose extends WindowAdapter {
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }
}

Post a Comment

Previous Post Next Post