Please click on ads

Please click on ads

Saturday, 6 November 2021

create a student registration form in java that connected with database by using jswing

 package jdbc;


import java.awt.EventQueue;

import java.awt.Frame;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;


import javax.swing.JFrame;

import javax.swing.JTextField;

import java.awt.BorderLayout;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;


public class jdbc2 {


private JFrame frame;

private JTextField roll;

private JTextField first;

private JTextField last;

private JTextField mob;

private JTextField search1;


/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

jdbc2 window = new jdbc2();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}


/**

* Create the application.

*/

public jdbc2() {

initialize();

}


/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame("JDBC project 1905701");

frame.setBounds(100, 100, 450, 407);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

roll = new JTextField();

roll.setBounds(123, 24, 86, 20);

frame.getContentPane().add(roll);

roll.setColumns(10);

JLabel lblNewLabel = new JLabel("roll");

lblNewLabel.setBounds(24, 27, 65, 14);

frame.getContentPane().add(lblNewLabel);

first = new JTextField();

first.setBounds(123, 67, 86, 20);

frame.getContentPane().add(first);

first.setColumns(10);

last = new JTextField();

last.setBounds(123, 111, 86, 20);

frame.getContentPane().add(last);

last.setColumns(10);

mob = new JTextField();

mob.setBounds(123, 151, 86, 20);

frame.getContentPane().add(mob);

mob.setColumns(10);

JLabel lblNewLabel_1 = new JLabel("first name");

lblNewLabel_1.setBounds(24, 70, 65, 14);

frame.getContentPane().add(lblNewLabel_1);

JLabel lblNewLabel_2 = new JLabel("last name");

lblNewLabel_2.setBounds(24, 114, 73, 14);

frame.getContentPane().add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("mobile");

lblNewLabel_3.setBounds(24, 154, 46, 14);

frame.getContentPane().add(lblNewLabel_3);

JButton sub = new JButton("add");

sub.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

    if(e.getSource()==sub)

        {

        try {

                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jdbcproject", "root", "@nkush1905701");


                PreparedStatement Pstatement=connection.prepareStatement("insert into student values(?,?,?,?)");

                Pstatement.setString(1,roll.getText());

                Pstatement.setString(2,first.getText());

                Pstatement.setString(3,last.getText());

                Pstatement.setString(4,mob.getText());

                

                


                connection.close();

            } catch (Exception exception) {

                exception.printStackTrace();

            }


        }}

}

);

sub.setBounds(64, 187, 89, 23);

frame.getContentPane().add(sub);

search1 = new JTextField();

search1.setBounds(152, 256, 86, 20);

frame.getContentPane().add(search1);

search1.setColumns(10);

JLabel search = new JLabel("search");

search.setBounds(43, 259, 46, 14);

frame.getContentPane().add(search);

JButton sear1 = new JButton("search");

sear1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

try {  

            String str = sear1.getText();  

 

            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/jdbcproject", "root", "@nkush1905701");  

            PreparedStatement st = con.prepareStatement("select * from student where last_name=?");  

            st.setString(1, str);  

             

            ResultSet rs = st.executeQuery();  

            if (rs.next()) {  

                String s = rs.getString(1);  

                String s1 = rs.getString(2);  

                String s2 = rs.getString(3);  

                String s3 = rs.getString(4);  

                  

                roll.setText(s);  

                first.setText(s1);  

                last.setText(s2);  

                mob.setText(s3);  

            } else {  

                JOptionPane.showMessageDialog(null, "Name not Found");  

            }  

              

        } catch (Exception ex) {  

            System.out.println(ex);  

        }  

    }  

});

sear1.setBounds(90, 295, 89, 23);

frame.getContentPane().add(sear1);

}


}


No comments:

Post a Comment