Wednesday, 3 May 2023

Write a Java program to display first record from student table (rno, sname, per) onto the TextFields by clicking on button. (Assume Student table is already created)

 

Write a Java program to display first record from student table (rno, sname, per) onto the TextFields by clicking on button. (Assume Student table is already created).

 import java.sql.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Slip19 extends JFrame implements ActionListener {

     JButton jb;

    JTextField t1, t2, t3;

    Container c;

 

     Connection con;

    Statement st;

    ResultSet rs;

 

     public Slip19() {

 

         c = getContentPane();

        c.setLayout(null);

        t1 = new JTextField();

        t1.setFont(new Font("Arial", Font.PLAIN, 20));

        t1.setSize(200, 30);

        t1.setLocation(100, 50);

        c.add(t1);

 

 

        t2 = new JTextField();

        t2.setFont(new Font("Arial", Font.PLAIN, 20));

        t2.setSize(200, 30);

        t2.setLocation(100, 100);

        c.add(t2);

 

 

 

        t3 = new JTextField();

        t3.setFont(new Font("Arial", Font.PLAIN, 20));

        t3.setSize(200, 30);

        t3.setLocation(100, 150);

                c.add(t3);

 

 

 

        jb = new JButton("FIRST RECORD");

        jb.setFont(new Font("Arial", Font.PLAIN, 20));

        jb.setSize(200, 30);

        jb.addActionListener(this);

        jb.setLocation(100, 200);

        c.add(jb);

        setVisible(true);

        setSize(800, 500);

    }

 

    public void actionPerformed(ActionEvent aq) {

         try {

 

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

 

            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/collegedb", "root", "");

 

 

 

            if (aq.getSource() == jb) {

 

                st = con.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_READ_ONLY);

 

 

                rs = st.executeQuery("Select * from Student;");

 

                rs.first();

                t1.setText(Integer.toString(rs.getInt(1)));

                t2.setText(rs.getString(2));

                t3.setText(Integer.toString(rs.getInt(3)));

 

            }

 

            con.close();

 

        } catch (Exception ex) {

 

        }

    }

 

 

 

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

        new Slip19();

    }

 

 

}