0% found this document useful (0 votes)
33 views

Pra 3 (Java)

This document describes a Java program that creates a student profile page using Swing components and connects to a MySQL database. The program creates text fields to input a student's name, enrollment number, mobile number, address, and gender. When the insert button is clicked, it connects to the database, creates a table if it doesn't exist, and inserts the input data into the table. The output shows a message that the data was successfully inserted.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Pra 3 (Java)

This document describes a Java program that creates a student profile page using Swing components and connects to a MySQL database. The program creates text fields to input a student's name, enrollment number, mobile number, address, and gender. When the insert button is clicked, it connects to the database, creates a table if it doesn't exist, and inserts the input data into the table. The output shows a message that the data was successfully inserted.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

100510107038

Batch No-B2

Practical No-3

Practical - 03
Aim :- Database Connectivity.
(a) Create a student profile page using swing component and do database
connectivity with mysql.(create db,table,insert data)
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class SwingDB extends JFrame implements ActionListener
{
public static void main(String[] args)
{
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cn=jf.getContentPane();
cn.setLayout(new GridLayout(9,2));
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
JPanel p6=new JPanel();
final JLabel l2=new JLabel( Name :");
final JTextField tf=new JTextField("",5);
final JLabel l3=new JLabel("Enrollment No :");
final JTextField tf1=new JTextField("",5);
final JLabel l4=new JLabel("Mobile No :");
final JTextField tf2=new JTextField("",5);
final JLabel l5=new JLabel(Address :");
final JTextField tf3=new JTextField("",5);
final JLabel l6=new JLabel("Gender:");

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-3

final JRadioButton r1=new JRadioButton("Male",true);


final JRadioButton r2=new JRadioButton("Female",true);
ButtonGroup bt1=new ButtonGroup();
bt1.add(r1);
bt1.add(r2);
final JButton b1=new JButton("Insert");
p1.add(l2);
p1.add(tf);
p2.add(l3);
p2.add(tf1);
p3.add(l4);
p3.add(tf2);
p4.add(l5);
p4.add(tf3);
p5.add(l6);
p5.add(r1);
p5.add(r2);
p6.add(b1);
cn.add(p1);
cn.add(p2);
cn.add(p3);
cn.add(p4);
cn.add(p5);
cn.add(p6);
ActionListener lst = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Connection c;
try
{

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-3

Class.forName("com.mysql.jdbc.Driver");
c=DriverManager.getConnection("jdbc:mysql://localhost:3306","root","");
Statement s=c.createStatement();
s.executeUpdate("create database if not exists Student");
c.close();
Connection c2;
c2=DriverManager.getConnection("jdbc:mysql://localhost:3306/Student","root","");
Statement s2=c2.createStatement();
s2.executeUpdate("create table reg (name varchar(20),en varchar(20),
mo varchar(10),address varchar(50),gender varchar(10))");
String n=tf.getText();
String n2=tf1.getText();
String n3=tf2.getText();
String n4=tf3.getText();
String n5;
if(r1.isSelected())
n5=r1.getText();
else
n5=r2.getText();
s2.executeUpdate("insert
values('"+n+"','"+n2+"','"+n3+"','"+n4+"','"+n5+"')");

into

reg

JOptionPane.showMessageDialog(null, "Data inserted successfully...");


}
catch (ClassNotFoundException | SQLException | HeadlessException ex)
{
JOptionPane.showMessageDialog(null,ex);
}
}
};
b1.addActionListener(lst);
jf.pack();
jf.show();
}
}

Alpha College of Engineering & Technology

Page

100510107038

Batch No-B2

Practical No-3

Output :-

Alpha College of Engineering & Technology

Page

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy