25 thoughts on “Login form in Java using JDBC connectivity

  1. Compiled successfully. . .bt it didn’t run, cause i dnt knw how to create database using ms access. .can u tell me step by step? I am new in java. . . . Any help would be greatly appreciated

  2. This program will only work with MySQL databases. If you want to use MS access then you will have to load the respective drivers.

  3. String query = “SELECT pass FROM info where username=’”+user+”‘”;

    why have u given +user+ i didn’t get that. I am working with where clause first time in jdbc.

  4. The value of textfield ‘name’ is stored in the variable ‘user’. We have concatinated this variable with the sql query. So basically at runtime if you enter ‘tom’ in the ‘name’ textfield, the query would be

    SELECT pass FROM info where username=’tom‘

  5. The program doesn’t enter in to the If loop! My connection is correct!

    check the following code:

    // TODO Auto-generated method stub
    String user = username.getText();
    String strpass = password.getText();

    try {
    Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);
    Statement st = con.createStatement();
    String query = “SELECT password FROM login where username=’”+user+”‘”;
    System.out.println(query);
    ResultSet rs = st.executeQuery(query);

    while(rs.next())
    {
    String dbpass = rs.getString(2);
    if(dbpass.equals(strpass)){
    System.out.println(“inside while”);
    JOptionPane.showMessageDialog(this,”correct”);

    }
    }

    }
    catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }*/

    }

  6. doesnt work!!! please help i have a project to do in one week! its not going in the if loop… db connection successful.

    package com.project.jdbc;

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;

    @SuppressWarnings(“serial”)
    public class jdbc extends JFrame implements ActionListener {

    JTextField username, password;
    JButton login;

    public jdbc()
    {
    setLayout(new FlowLayout());
    username= new JTextField(20);
    password= new JTextField(20);
    login= new JButton(“Login”);

    add(username);
    add(password);
    add(login);

    login.addActionListener(this);

    }

    public static void main(String[] args) {

    jdbc login = new jdbc();
    login.setVisible(true);
    login.setSize(400, 400);
    login.setDefaultCloseOperation(EXIT_ON_CLOSE);

    /* try {
    Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
    Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);

    Statement stmt = con.createStatement();
    ResultSet res = stmt.executeQuery(“select * from pets”);
    System.out.println(“PID \t PNAME \t PTYPE”);
    while(res.next())
    {

    System.out.println(res.getInt(1)+”\t”+res.getString(2)+”\t”+res.getString(3));
    }
    res.close();
    stmt.close();
    con.close();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }*/

    }

    @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String user = username.getText();
    String strpass = password.getText();

    try {
    Connection con = DriverManager.getConnection(“jdbc:odbc:MyDB”,”system”,”tiger”);
    Statement st = con.createStatement();
    String query = “SELECT pass FROM login where username=’”+user+”‘”;
    System.out.println(query);
    ResultSet rs = st.executeQuery(query);

    if(rs.next())
    {

    String dbpass = rs.getString(“password”);
    if(dbpass.equals(strpass)){
    JOptionPane.showMessageDialog(null,”Login Successful!”);
    }
    else
    {
    JOptionPane.showMessageDialog(null,”Login Unsuccessful!”);
    }
    }
    else
    {
    JOptionPane.showMessageDialog(null,”No Record Found”);
    }

    } catch (SQLException ex) {
    System.out.println(e);
    }
    }
    }

  7. The program seems to be fine, what does this line display in the output screen.
    String query = “SELECT pass FROM login where username=’”+user+”‘”;
    System.out.println(query);

    You can try this,
    String query = “SELECT pass FROM login where username= ”+user+””;

  8. This is totally working … by the way, i’m using NetBeans 7.1.1 and a WAMPSERER help by MYSQL Workbench….

    the problem by me when trying to attempt to make this login form is the connection between the database and the program itself, but with the help of NetBeans it really solve the problem…

    Thank You Very Much… :)

  9. Create three button on the JSP which are namely Add product, edit product and delete product.
    A div would be rendered on the screen on click of add product and edit product containing all the relevant fields.
    On edit product, the system would render a text box where the user would be asked for the product id and then on tab of it the product details would be retrieved from the database. For delete, the on tab event would prompt for user confirmation and then on confirmation the product would be deleted from the database. You are free to opt for a better UI design than the one mentioned above
    The Add product and edit product would contain the product id and legal address details like Address line1, Address line2, City, State, Post Code and Country.
    On click of submit button in case of add product, edit product and edit product, appropriate user data would be maintained by the system.
    Success message needs to be shown for all the 3 operations which are namely Add product, Edit product and delete product.

    how to strat with m new to java

    • You could use a session variable to check if the user has logged in or not. Depending on the value of the session variable, you can display/hide the link for registration.

  10. Hi
    Sir ,
    I am in 3rd year of IT engg. I am really weak in swing components in java as compare to J2EE and any. Please tell me any way to improve it.
    Please reply

  11. hello, i have created a login page using java swings, login page should have another button

    called “Register” once that Register button clicked means related Register form should open (For Registration i have written a code). How can i do that?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">