JDBC Project - Log in Project With Mysql In Eclipse
For explanation Watch Video :
Step 1: Create table 'login' in mysql
step 2:: Insert Records Into 'login' table
Install Swing On Eclipse
after that click on install new software
after that
for connection method::
click on help at top of eclipse IDE
after that
paste link in work with : 2020-12 - http://download.eclipse.org/releases/2020-12
i am using 2020-12 eclipse version at place of 2020-12 you can use your version name
ex::
if your version of eclipse is oxygen then
link : oxygen - http://download.eclipse.org/releases/oxygen
that click on finish. After downloading all tools restart eclipse IDE.
Download MySQL Jar File From here:
https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.25 |
Connection con = null;
PreparedStatement ps = null;
public void connect() {
try {
con = DriverManager.getConnection("jdbc:mysql:///new","root","root");
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
For Log In Button actionPerformed method Code::
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String uname,pass;
uname = textField.getText();
pass = textField_1.getText();
String query = "select count(*) from login1 where username=? and password=?";
ps = con.prepareStatement(query);
ps.setString(1, uname);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
rs.next();
int count = rs.getInt(1);
if(count==0) {
JOptionPane.showMessageDialog(null, "InValid Credentials");
}else {
JOptionPane.showMessageDialog(null, "Valid Credentials");
}
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception ee) {
ee.printStackTrace();
}
}
});
Full Code:::
package com.test;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Login1 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login1 window = new Login1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Login1() {
initialize();
connect();
}
Connection con = null;
PreparedStatement ps = null;
public void connect() {
try {
con = DriverManager.getConnection("jdbc:mysql:///new","root","root");
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 719, 560);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("Log In");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel.setBounds(289, 39, 317, 52);
frame.getContentPane().add(lblNewLabel);
JPanel panel = new JPanel();
panel.setBounds(89, 114, 501, 286);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("Username");
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel_1.setBounds(78, 71, 104, 32);
panel.add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(247, 71, 197, 28);
panel.add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("Password");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.BOLD, 16));
lblNewLabel_1_1.setBounds(78, 151, 104, 32);
panel.add(lblNewLabel_1_1);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(247, 155, 197, 28);
panel.add(textField_1);
JButton btnNewButton = new JButton("Log In");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String uname,pass;
uname = textField.getText();
pass = textField_1.getText();
String query = "select count(*) from login1 where username=? and password=?";
ps = con.prepareStatement(query);
ps.setString(1, uname);
ps.setString(2, pass);
ResultSet rs = ps.executeQuery();
rs.next();
int count = rs.getInt(1);
if(count==0) {
JOptionPane.showMessageDialog(null, "InValid Credentials");
}else {
JOptionPane.showMessageDialog(null, "Valid Credentials");
}
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception ee) {
ee.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 16));
btnNewButton.setBounds(286, 444, 149, 43);
frame.getContentPane().add(btnNewButton);
}
}
Comments
Post a Comment