SlideShare a Scribd company logo
Connectivity
Swing Database(mysql)
public class Login extends JFrame {
JTextField textField;
JPasswordField passwordField;
JButton btnNewButton;
JLabel label;
JPanel contentPane;
Login()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(450, 190, 1014, 597);
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Login");
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));
lblNewLabel.setBounds(423, 13, 273, 93);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 32));
textField.setBounds(481, 170, 281, 68);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));
passwordField.setBounds(481, 286, 281, 68);
contentPane.add(passwordField);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBackground(Color.BLACK);
lblUsername.setForeground(Color.BLACK);
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblUsername.setBounds(250, 166, 193, 52);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setForeground(Color.BLACK);
lblPassword.setBackground(Color.CYAN);
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31));
lblPassword.setBounds(250, 286, 193, 52);
contentPane.add(lblPassword);
btnNewButton = new JButton("Login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26));
btnNewButton.setBounds(545, 392, 162, 73);
contentPane.add(btnNewButton);
}
public static void main(String[] args) {
Login frame = new Login();
frame.setVisible(true);
}
}
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = textField.getText();
String password = passwordField.getText();
try {
Connection connection = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo",
"root", "root");
PreparedStatement st = (PreparedStatement) connection
.prepareStatement("Select name, password from student where name=? and password=?");
st.setString(1, userName);
st.setString(2, password);
ResultSet rs = st.executeQuery();
if (rs.next()) {
dispose();
// UserHome ah = new UserHome(userName);
//ah.setTitle("Welcome");
// ah.setVisible(true);
JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged in");
} else {
JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password");
}
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
});
contentPane.add(btnNewButton);
class Form3 extends JFrame
{
private static final long serialVersionUID = 1L;
JButton ADD;
JPanel panel;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;
Form3(){
label1 = new JLabel();
label1.setText("UserID:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("First Name:");
text2 = new JTextField(20);
label3 = new JLabel();
label3.setText("Last Name:");
text3 = new JTextField(20);
label4 = new JLabel();
label4.setText("ADDRESS:");
text4 = new JTextField(20);
label5 = new JLabel();
label5.setText("Email:");
text5 = new JTextField(20);
ADD=new JButton("ADD");
panel=new JPanel(new GridLayout(6,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
setTitle("FORM");
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.getText();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xyz1", "root", "root"
);
Statement st=conn.createStatement();
st.executeUpdate("insert into data(id,fname,lname,address,email)
values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"')");
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch(Exception e){}
}});}}
class FormDemo
{
public static void main(String arg[])
{
try
{
Form3 frame=new Form3();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e){
}}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class StudentReg extends JPanel
implements ActionListener {
JButton b1;
JTextField t1,t2,t3;
JLabel l1,l2,l3,l4;
JCheckBox c1,c2,c3;
JRadioButton r1,r2;
ButtonGroup bg1;
JComboBox cb;
String str="Game : ";
JCheckBox c[] = new JCheckBox[3];
JPanel p1;
JFrame f1;
public StudentReg()
{
b1 = new JButton("Submit");
t1 = new JTextField(10);
t2 = new JTextField(10);
t3 = new JTextField(10);
l1 = new JLabel("Name");
l2 = new JLabel("Branch");
l3 = new JLabel("Address");
l4 = new JLabel("Car");
r1 = new JRadioButton("Male");
r1.setActionCommand("male");
r2 = new
JRadioButton("Female");
r2.setActionCommand("female");
c1 = new JCheckBox("Cricket");
c2 = new
JCheckBox("FootBall");
c3 = new
JCheckBox("VollyBall");
cb = new JComboBox();
bg1 = new ButtonGroup();
cb.addItem("BMW");
cb.addItem("AUDI");
cb.addItem("AMW");
b1.addActionListener(this);
bg1.add(r1);
bg1.add(r2);
p1= new JPanel (new GridLayout(14,1));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(t3);
p1.add(l4);
p1.add(cb);
p1.add(r1);
p1.add(r2);
p1.add(c1);
p1.add(c2);
p1.add(c3);
p1.add(b1);
f1= new JFrame();
p1.setBackground(Color.GREEN);
p1.setSize(300,300);
f1.add(p1);
f1.add(p1,BorderLayout.CENTER);
f1.setTitle("FORM");
f1.setSize(500,500);
f1.setVisible(true);
f1.setLayout(null);
f1.addWindowListener(new
WindowAdapter()
{
public void
windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{
String name = t1.getText();
System.out.println("Name : "+name);
String branch = t2.getText();
System.out.println("Branch : "+branch);
String add = t3.getText();
System.out.println("Address : "+add);
String car = cb.getSelectedItem().toString();
System.out.println("Car : "+car);
if(bg1.getSelection().getActionCommand().e
quals("male"))
{
System.out.println("Gender : Male");
}
else
{
System.out.println("Gender : Female");
}
if(c1.isSelected())
{
str = str +"Cricket ";
}
if(c2.isSelected())
{
str = str+"+ FootBall";
}
if(c3.isSelected())
{
str = str+ "+ VollyBall ";
}
System.out.println(" "+str);
}
public static void main(String args[])
{
StudentReg r1=new StudentReg();
}
}
Java Database Connectivity with
MySQL
To connect Java application with the MySQL database, we need to follow 5
following steps.
In this example we are using MySql as the database. So we need to know
following informations for the mysql database:
Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the
database, localhost is the server name on which mysql is running, we may also
use IP address, 3306 is the port number and sonoo is the database name. We
may use any database, in such case, we need to replace the sonoo with our
database name.
Username: The default username for the mysql database is root.
Password: It is the password given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need
to create database first
 create database abc;
 use abc;
 create table emp(id int(10),name varchar(40),ag
e int(3));
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/abc","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getStrin
g(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
Connecting two forms
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class LoginDemo extends JFrame{
JButton SUBMIT;
JLabel label1,label2;
final JTextField text1,text2;
LoginDemo(){
setTitle("Login Form");
setLayout(null);
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
label1.setBounds(350,100,100,20);
text1.setBounds(450,100,200,20);
label2.setBounds(350,130,100,20);
text2.setBounds(450,130,200,20);
SUBMIT.setBounds(450,160,100,20);
add(label1);
add(text1);
add(label2);
add(text2);
add(SUBMIT);
setVisible(true);
setSize(1024,768);
SUBMIT.addActionListener(new
ActionListener(){
public void
actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
try{
Class.forName("com.mysql.jdbc.Dri
ver");
Connection con =
DriverManager.getConnection("jdbc:
mysql://localhost:3306/test", "root",
"root");
Statement
st=con.createStatement();
ResultSet
rs=st.executeQuery("select * from
login where username='"+value1+"'
and password='"+value2+"'");
String uname="",pass="";
if(rs.next()){
if(value1.equals("") && value2.equals("")) {
JOptionPane.showMessageDialog(null,"Enter
login name or
password","Error",JOptionPane.ERROR_MESS
AGE);
}
else if(value1.equals(uname) &&
value2.equals(pass)) {
NextPage page=new NextPage(uname);
page.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null,"Invalid
login name or
password","Error",JOptionPane.ERROR_MESS
AGE);
text1.setText("");
text2.setText("");
}
}
catch(Exception e){}
}
});
}
public static void main(String arg[]){
new LoginDemo();
}
Next Page
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class NextPage extends JFrame
{
NextPage(String st)
{ setLayout(null);
setDefaultCloseOperation(javax.swing.
WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Welcome");
JLabel lab=new JLabel("Welcome "+st);
lab.setBounds(10,10,500,20);
add(lab);
setSize(1024, 768); } }
FIRST Frame
import javax.swing.*;
import java.awt.event.*;
public class MyLogin {
private JFrame f = new JFrame("Login");
private JButton bok = new JButton("OK");
public MyLogin() {
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLO
SE);
f.getContentPane().add(bok);
bok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent
ae) {
f.dispose();
new SecondFrame();
}
});
f.setSize(100,100);
f.setVisible(true);
}
public static void main(String[] args) {
new MyLogin();
}
}
Second frame
import javax.swing.*;
public class SecondFrame {
private JFrame f = new JFrame("Second");
public SecondFrame()
{
f.setDefaultCloseOperation(JFrame.EXIT_ON
_CLOSE);
f.setSize(300,300);
f.setVisible(true);
}
}

More Related Content

What's hot

PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
José Lorenzo Rodríguez Urdaneta
 
Stored Procedure
Stored ProcedureStored Procedure
Stored Procedure
NidiaRamirez07
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
Gaetano Causio
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
Paul King
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
PrinceGuru MS
 
Connectivity coding for java and mysql
Connectivity coding for java and mysqlConnectivity coding for java and mysql
Connectivity coding for java and mysql
Fahad Ali Khan
 
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
PHP - PDO Objects
PHP - PDO ObjectsPHP - PDO Objects
PHP - PDO Objects
AJINKYA N
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
Rifat Nabi
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
Nicolas Leroy
 
Php mysq
Php mysqPhp mysq
Php mysq
prasanna pabba
 
Sequelize
SequelizeSequelize
Sequelize
Tarek Raihan
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
Jeremy Kendall
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
G Woo
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Prashant Marathe
 
Lodash js
Lodash jsLodash js
Lodash js
LearningTech
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
Nate Abele
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 

What's hot (19)

PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
Stored Procedure
Stored ProcedureStored Procedure
Stored Procedure
 
Everything About PowerShell
Everything About PowerShellEverything About PowerShell
Everything About PowerShell
 
concurrency with GPars
concurrency with GParsconcurrency with GPars
concurrency with GPars
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Connectivity coding for java and mysql
Connectivity coding for java and mysqlConnectivity coding for java and mysql
Connectivity coding for java and mysql
 
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه چهارم کلاس پایتون برای هکرهای قانونی
 
PHP - PDO Objects
PHP - PDO ObjectsPHP - PDO Objects
PHP - PDO Objects
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Drupal7 dbtng
Drupal7  dbtngDrupal7  dbtng
Drupal7 dbtng
 
Php mysq
Php mysqPhp mysq
Php mysq
 
Sequelize
SequelizeSequelize
Sequelize
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
PHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php frameworkPHP 5.3 and Lithium: the most rad php framework
PHP 5.3 and Lithium: the most rad php framework
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Lodash js
Lodash jsLodash js
Lodash js
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
veracruz
veracruzveracruz
veracruz
 

Similar to Swing database(mysql)

Fahad project java connectivity
Fahad project java connectivityFahad project java connectivity
Fahad project java connectivity
Fahad Ali Khan
 
Wwe Management System
Wwe Management SystemWwe Management System
Wwe Management System
NeerajMudgal1
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
Alexandre Morgaut
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
Naresh Chintalcheru
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
Lukas Ruebbelke
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
hameedkhan2017
 
jrubykaigi2010-lt-rubeus
jrubykaigi2010-lt-rubeusjrubykaigi2010-lt-rubeus
jrubykaigi2010-lt-rubeus
Takeshi AKIMA
 
Deeply Declarative Data Pipelines
Deeply Declarative Data PipelinesDeeply Declarative Data Pipelines
Deeply Declarative Data Pipelines
HostedbyConfluent
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
Java File
Java FileJava File
Java File
Archita Misra
 
Java
JavaJava
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
Stéphane Wirtel
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql
JOYITAKUNDU1
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
Zianed Hou
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
Felix Geisendörfer
 
Introduccion a Jasmin
Introduccion a JasminIntroduccion a Jasmin
Introduccion a Jasmin
Rodrigo Quelca Sirpa
 
I need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdfI need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdf
jibinsh
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
Johannes Hoppe
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
Johannes Hoppe
 
Java and xml
Java and xmlJava and xml
Java and xml
info_zybotech
 

Similar to Swing database(mysql) (20)

Fahad project java connectivity
Fahad project java connectivityFahad project java connectivity
Fahad project java connectivity
 
Wwe Management System
Wwe Management SystemWwe Management System
Wwe Management System
 
NoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love StoryNoSQL and JavaScript: a Love Story
NoSQL and JavaScript: a Love Story
 
Java7 New Features and Code Examples
Java7 New Features and Code ExamplesJava7 New Features and Code Examples
Java7 New Features and Code Examples
 
Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015Impress Your Friends with EcmaScript 2015
Impress Your Friends with EcmaScript 2015
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
jrubykaigi2010-lt-rubeus
jrubykaigi2010-lt-rubeusjrubykaigi2010-lt-rubeus
jrubykaigi2010-lt-rubeus
 
Deeply Declarative Data Pipelines
Deeply Declarative Data PipelinesDeeply Declarative Data Pipelines
Deeply Declarative Data Pipelines
 
MySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of ThingsMySQL flexible schema and JSON for Internet of Things
MySQL flexible schema and JSON for Internet of Things
 
Java File
Java FileJava File
Java File
 
Java
JavaJava
Java
 
Python postgre sql a wonderful wedding
Python postgre sql   a wonderful weddingPython postgre sql   a wonderful wedding
Python postgre sql a wonderful wedding
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql
 
Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1Tomcat连接池配置方法V2.1
Tomcat连接池配置方法V2.1
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
 
Introduccion a Jasmin
Introduccion a JasminIntroduccion a Jasmin
Introduccion a Jasmin
 
I need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdfI need to adjust this Huffman code so that it asks for the user to i.pdf
I need to adjust this Huffman code so that it asks for the user to i.pdf
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
Java and xml
Java and xmlJava and xml
Java and xml
 

More from vishal choudhary

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
vishal choudhary
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
vishal choudhary
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
vishal choudhary
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
vishal choudhary
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
vishal choudhary
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
vishal choudhary
 
XML.pptx
XML.pptxXML.pptx
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
vishal choudhary
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
vishal choudhary
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
vishal choudhary
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
vishal choudhary
 
SE1.ppt
SE1.pptSE1.ppt
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
vishal choudhary
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
vishal choudhary
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
vishal choudhary
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
vishal choudhary
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
vishal choudhary
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
vishal choudhary
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
vishal choudhary
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
vishal choudhary
 

More from vishal choudhary (20)

SE-Lecture1.ppt
SE-Lecture1.pptSE-Lecture1.ppt
SE-Lecture1.ppt
 
SE-Testing.ppt
SE-Testing.pptSE-Testing.ppt
SE-Testing.ppt
 
SE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.pptSE-CyclomaticComplexityand Testing.ppt
SE-CyclomaticComplexityand Testing.ppt
 
SE-Lecture-7.pptx
SE-Lecture-7.pptxSE-Lecture-7.pptx
SE-Lecture-7.pptx
 
Se-Lecture-6.ppt
Se-Lecture-6.pptSe-Lecture-6.ppt
Se-Lecture-6.ppt
 
SE-Lecture-5.pptx
SE-Lecture-5.pptxSE-Lecture-5.pptx
SE-Lecture-5.pptx
 
XML.pptx
XML.pptxXML.pptx
XML.pptx
 
SE-Lecture-8.pptx
SE-Lecture-8.pptxSE-Lecture-8.pptx
SE-Lecture-8.pptx
 
SE-coupling and cohesion.ppt
SE-coupling and cohesion.pptSE-coupling and cohesion.ppt
SE-coupling and cohesion.ppt
 
SE-Lecture-2.pptx
SE-Lecture-2.pptxSE-Lecture-2.pptx
SE-Lecture-2.pptx
 
SE-software design.ppt
SE-software design.pptSE-software design.ppt
SE-software design.ppt
 
SE1.ppt
SE1.pptSE1.ppt
SE1.ppt
 
SE-Lecture-4.pptx
SE-Lecture-4.pptxSE-Lecture-4.pptx
SE-Lecture-4.pptx
 
SE-Lecture=3.pptx
SE-Lecture=3.pptxSE-Lecture=3.pptx
SE-Lecture=3.pptx
 
Multimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptxMultimedia-Lecture-Animation.pptx
Multimedia-Lecture-Animation.pptx
 
MultimediaLecture5.pptx
MultimediaLecture5.pptxMultimediaLecture5.pptx
MultimediaLecture5.pptx
 
Multimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptxMultimedia-Lecture-7.pptx
Multimedia-Lecture-7.pptx
 
MultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptxMultiMedia-Lecture-4.pptx
MultiMedia-Lecture-4.pptx
 
Multimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptxMultimedia-Lecture-6.pptx
Multimedia-Lecture-6.pptx
 
Multimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptxMultimedia-Lecture-3.pptx
Multimedia-Lecture-3.pptx
 

Recently uploaded

Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
blueshagoo1
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 

Recently uploaded (20)

Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
CIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdfCIS 4200-02 Group 1 Final Project Report (1).pdf
CIS 4200-02 Group 1 Final Project Report (1).pdf
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 

Swing database(mysql)

  • 2.
  • 3. public class Login extends JFrame { JTextField textField; JPasswordField passwordField; JButton btnNewButton; JLabel label; JPanel contentPane; Login() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(450, 190, 1014, 597); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("Login"); lblNewLabel.setForeground(Color.BLACK); lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46)); lblNewLabel.setBounds(423, 13, 273, 93); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setFont(new Font("Tahoma", Font.PLAIN, 32)); textField.setBounds(481, 170, 281, 68); contentPane.add(textField); textField.setColumns(10); passwordField = new JPasswordField(); passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32)); passwordField.setBounds(481, 286, 281, 68); contentPane.add(passwordField); JLabel lblUsername = new JLabel("Username"); lblUsername.setBackground(Color.BLACK); lblUsername.setForeground(Color.BLACK); lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31)); lblUsername.setBounds(250, 166, 193, 52); contentPane.add(lblUsername); JLabel lblPassword = new JLabel("Password"); lblPassword.setForeground(Color.BLACK); lblPassword.setBackground(Color.CYAN); lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31)); lblPassword.setBounds(250, 286, 193, 52); contentPane.add(lblPassword); btnNewButton = new JButton("Login"); btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26)); btnNewButton.setBounds(545, 392, 162, 73); contentPane.add(btnNewButton); } public static void main(String[] args) { Login frame = new Login(); frame.setVisible(true); } }
  • 4. btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String userName = textField.getText(); String password = passwordField.getText(); try { Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root", "root"); PreparedStatement st = (PreparedStatement) connection .prepareStatement("Select name, password from student where name=? and password=?"); st.setString(1, userName); st.setString(2, password); ResultSet rs = st.executeQuery(); if (rs.next()) { dispose(); // UserHome ah = new UserHome(userName); //ah.setTitle("Welcome"); // ah.setVisible(true); JOptionPane.showMessageDialog(btnNewButton, "You have successfully logged in"); } else { JOptionPane.showMessageDialog(btnNewButton, "Wrong Username & Password"); } } catch (SQLException sqlException) { sqlException.printStackTrace(); } } }); contentPane.add(btnNewButton);
  • 5.
  • 6. class Form3 extends JFrame { private static final long serialVersionUID = 1L; JButton ADD; JPanel panel; JLabel label1,label2,label3,label4,label5; final JTextField text1,text2,text3,text4,text5; Form3(){ label1 = new JLabel(); label1.setText("UserID:"); text1 = new JTextField(20); label2 = new JLabel(); label2.setText("First Name:"); text2 = new JTextField(20); label3 = new JLabel(); label3.setText("Last Name:"); text3 = new JTextField(20); label4 = new JLabel(); label4.setText("ADDRESS:"); text4 = new JTextField(20); label5 = new JLabel(); label5.setText("Email:"); text5 = new JTextField(20); ADD=new JButton("ADD"); panel=new JPanel(new GridLayout(6,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(text4); panel.add(label5); panel.add(text5); panel.add(ADD); add(panel,BorderLayout.CENTER); setTitle("FORM"); ADD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); String value3=text3.getText(); String value4=text4.getText(); String value5=text5.getText(); try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/xyz1", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(id,fname,lname,address,email) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"')"); JOptionPane.showMessageDialog(null,"Inserted Successfully!"); } catch(Exception e){} }});}} class FormDemo { public static void main(String arg[]) { try { Form3 frame=new Form3(); frame.setSize(300,300); frame.setVisible(true); } catch(Exception e){ }}}
  • 7.
  • 8. import java.awt.*; import java.awt.event.*; import javax.swing.*; class StudentReg extends JPanel implements ActionListener { JButton b1; JTextField t1,t2,t3; JLabel l1,l2,l3,l4; JCheckBox c1,c2,c3; JRadioButton r1,r2; ButtonGroup bg1; JComboBox cb; String str="Game : "; JCheckBox c[] = new JCheckBox[3]; JPanel p1; JFrame f1; public StudentReg() { b1 = new JButton("Submit"); t1 = new JTextField(10); t2 = new JTextField(10); t3 = new JTextField(10); l1 = new JLabel("Name"); l2 = new JLabel("Branch"); l3 = new JLabel("Address"); l4 = new JLabel("Car"); r1 = new JRadioButton("Male"); r1.setActionCommand("male"); r2 = new JRadioButton("Female"); r2.setActionCommand("female"); c1 = new JCheckBox("Cricket"); c2 = new JCheckBox("FootBall"); c3 = new JCheckBox("VollyBall"); cb = new JComboBox(); bg1 = new ButtonGroup();
  • 9. cb.addItem("BMW"); cb.addItem("AUDI"); cb.addItem("AMW"); b1.addActionListener(this); bg1.add(r1); bg1.add(r2); p1= new JPanel (new GridLayout(14,1)); p1.add(l1); p1.add(t1); p1.add(l2); p1.add(t2); p1.add(l3); p1.add(t3); p1.add(l4); p1.add(cb); p1.add(r1); p1.add(r2); p1.add(c1); p1.add(c2); p1.add(c3); p1.add(b1); f1= new JFrame(); p1.setBackground(Color.GREEN); p1.setSize(300,300); f1.add(p1); f1.add(p1,BorderLayout.CENTER); f1.setTitle("FORM"); f1.setSize(500,500); f1.setVisible(true); f1.setLayout(null); f1.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); }
  • 10. public void actionPerformed(ActionEvent e) { String name = t1.getText(); System.out.println("Name : "+name); String branch = t2.getText(); System.out.println("Branch : "+branch); String add = t3.getText(); System.out.println("Address : "+add); String car = cb.getSelectedItem().toString(); System.out.println("Car : "+car); if(bg1.getSelection().getActionCommand().e quals("male")) { System.out.println("Gender : Male"); } else { System.out.println("Gender : Female"); } if(c1.isSelected()) { str = str +"Cricket "; } if(c2.isSelected()) { str = str+"+ FootBall"; } if(c3.isSelected()) { str = str+ "+ VollyBall "; } System.out.println(" "+str); } public static void main(String args[]) { StudentReg r1=new StudentReg(); } }
  • 11.
  • 12. Java Database Connectivity with MySQL To connect Java application with the MySQL database, we need to follow 5 following steps. In this example we are using MySql as the database. So we need to know following informations for the mysql database: Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver. Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name. We may use any database, in such case, we need to replace the sonoo with our database name. Username: The default username for the mysql database is root. Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password. Let's first create a table in the mysql database, but before creating table, we need to create database first
  • 13.  create database abc;  use abc;  create table emp(id int(10),name varchar(40),ag e int(3));
  • 14. import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/abc","root","root"); //here sonoo is database name, root is username and password Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getStrin g(3)); con.close(); }catch(Exception e){ System.out.println(e);} } }
  • 15. Connecting two forms import java.sql.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class LoginDemo extends JFrame{ JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2; LoginDemo(){ setTitle("Login Form"); setLayout(null); label1 = new JLabel(); label1.setText("Username:"); text1 = new JTextField(15); label2 = new JLabel(); label2.setText("Password:"); text2 = new JPasswordField(15); SUBMIT=new JButton("SUBMIT"); label1.setBounds(350,100,100,20); text1.setBounds(450,100,200,20); label2.setBounds(350,130,100,20); text2.setBounds(450,130,200,20); SUBMIT.setBounds(450,160,100,20); add(label1); add(text1); add(label2); add(text2); add(SUBMIT); setVisible(true); setSize(1024,768);
  • 16. SUBMIT.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); try{ Class.forName("com.mysql.jdbc.Dri ver"); Connection con = DriverManager.getConnection("jdbc: mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'"); String uname="",pass=""; if(rs.next()){ if(value1.equals("") && value2.equals("")) { JOptionPane.showMessageDialog(null,"Enter login name or password","Error",JOptionPane.ERROR_MESS AGE); } else if(value1.equals(uname) && value2.equals(pass)) { NextPage page=new NextPage(uname); page.setVisible(true); } else{ JOptionPane.showMessageDialog(null,"Invalid login name or password","Error",JOptionPane.ERROR_MESS AGE); text1.setText(""); text2.setText(""); } } catch(Exception e){} } }); } public static void main(String arg[]){ new LoginDemo(); }
  • 17. Next Page import javax.swing.*; import java.awt.*; import java.awt.event.*; class NextPage extends JFrame { NextPage(String st) { setLayout(null); setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE); setTitle("Welcome"); JLabel lab=new JLabel("Welcome "+st); lab.setBounds(10,10,500,20); add(lab); setSize(1024, 768); } }
  • 18. FIRST Frame import javax.swing.*; import java.awt.event.*; public class MyLogin { private JFrame f = new JFrame("Login"); private JButton bok = new JButton("OK"); public MyLogin() { f.setDefaultCloseOperation(JFrame.HIDE_ON_CLO SE); f.getContentPane().add(bok); bok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { f.dispose(); new SecondFrame(); } }); f.setSize(100,100); f.setVisible(true); } public static void main(String[] args) { new MyLogin(); } }
  • 19. Second frame import javax.swing.*; public class SecondFrame { private JFrame f = new JFrame("Second"); public SecondFrame() { f.setDefaultCloseOperation(JFrame.EXIT_ON _CLOSE); f.setSize(300,300); f.setVisible(true); } }