Skip to main content

JDBC Project - Log in Project With Mysql In Eclipse

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






Step 3: Go to Eclipse and download swing related components
Install Swing On Eclipse

click on help at top of eclipse IDE



after that click on install new software



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


expand click on general purpose tools and check the all tool from swing designer to end and after

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

for connection method::

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

Popular posts from this blog

Servlet Project Book Shop Application in eclipse

 Servlet Project Book Shop Application in eclipse  For explanation watch video::: Note :: In this Project you must configure web server (for example tomcat) with eclipse ide Download Bootstrap  from ::         https://getbootstrap.com/docs/4.3/getting-started/download/ Download mysql jar file from :: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.22 adding MySQL Connector/J jar file in eclipse ide for jdbc video :: video link : https://youtu.be/4Fyd-k3eG_I Directory Structure:: web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">   <welcome-file-list>     <welcome-file>home.html</welcome-file>   </welcome-file-list>   <display-...

JDBC basic example For Select Query

JDBC basic example For Select Query  For explanation watch video:  For Creating Table:: SQL> create table emp60(srno int,fname varchar2(10)); Table created. SQL> desc emp60;  Name                                      Null?    Type  ----------------------------------------- -------- ----------------------------  SRNO                                               NUMBER(38)  FNAME                                              VARCHAR2(10) SQL> insert into emp60 values(1,'allu'); 1 row created. SQL> insert into emp60 values(2,'vijay'); 1 row created. SQL> insert into emp60 values(3,'rajni'); 1 row created. SQL...

JDBC Program to access table data from mysql database

 import java.sql.*; class MysqlCon  { public static void main(String[] args)  { try{ Connection con = DriverManager.getConnection("jdbc:mysql:///new","root","root"); Statement st = con.createStatement(); String query = "select * from login"; ResultSet rs = st.executeQuery(query); while(rs.next()){ System.out.println(rs.getString(1)+" "+rs.getString(2)); } con.close(); }catch(SQLException e){ System.out.println("Error"); }catch(Exception e){ } } }