Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,739 members, 7,824,125 topics. Date: Friday, 10 May 2024 at 11:44 PM

Please Help Me On Login Code In Java. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Help Me On Login Code In Java. (1597 Views)

I Want To Develop A Software In Java To Calculate CGPA / Login Code In Vb6.0 / Marquee Code In Vb (2) (3) (4)

(1) (Reply) (Go Down)

Please Help Me On Login Code In Java. by xtianh(m): 12:19am On Oct 16, 2013
i kindly ask for help from one that can do so. I have been trying to develope a login program in java. Where d major problem is, is integrating the java and mysql database. I also needed a good illustration on hw to code the login and read frm the database thanks
Re: Please Help Me On Login Code In Java. by BetaQuick: 2:00am On Oct 16, 2013
A crude login Process

1. First you need a login form to get the username and password

<form action="/login" method="POST">
Username: <input type="text" name="username" />
Password: <input type="password" name="password />
<input type="submit" value="Submit">


2. Then you need a servlet running on your server to get the information and process it.

  
class LoginServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) {
// Get the username and password
String username = request.getParameter("username"wink;
String password = request.getParameter("password"wink;

try {
// Get the users information from the database
String user = getUserFromDB(username, password);
// create a session for your newly logged in user
HttpSession session = request.getSession();
session.setAttribute("user", user);
// send user to logged-in page
response.sendRedirect("/main"wink;

} catch(Exception e) {
// user failed login
request.setAtrribute("error", "Invalid username or password"wink;
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp"wink;
rd.forward(request, response);
}

}

private String getUserFromDB(String username, String password) throws Exception {
// This will load the MySQL driver
Class.forName("com.mysql.jdbc.Driver"wink;
// Setup the connection with the DB
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/database?user=user&password=pw"wink;
// Statements allow to issue SQL queries to the database
Statement statement = connect.createStatement();
ResultSet resultSet = statement.executeQuery("select name from User where username='"+username+"' and password = '"+password+"');

// ResultSet is initially before the first data set
resultSet.next();

String user = resultSet.getString("name"wink;

// You need to close the database connections
if (resultSet != null) {
resultSet.close();
}

if (statement != null) {
statement.close();
}

if (connect != null) {
connect.close();
}
return name;
}

}
Re: Please Help Me On Login Code In Java. by xtianh(m): 1:44am On Oct 17, 2013
thanks bro i will try it out
Re: Please Help Me On Login Code In Java. by Nov1ce(m): 5:28am On Oct 17, 2013
I think the first question to ask is are you coding for Java Web or Desktop?

2 Likes

Re: Please Help Me On Login Code In Java. by ogunayo: 11:17am On Oct 17, 2013
Goto echelontrainingportal.com, subscribe for free and start learning JAVA. All our posted answers are tested and confirm runing

2 Likes

Re: Please Help Me On Login Code In Java. by jboy01(m): 6:04pm On Oct 17, 2013
i need d one for desktop app
Re: Please Help Me On Login Code In Java. by xtianh(m): 4:17pm On Oct 18, 2013
Nov1ce: I think the first question to ask is are you coding for Java Web or Desktop?

for desktop app. A standalone program
Re: Please Help Me On Login Code In Java. by danvery2k6(m): 3:52pm On Oct 19, 2013
1. create a table in your database called user or any name of your choice.
2. create two fields of type varchar called username and password with username as the primary key.
3. create a class called UserLogin with username and password as instance variables.
4.create a constructor that initialises these variables.
5. create method login that returns a boolean value and that takes two arguments: user and pass and fetches a row from the database where username = user
6. pass the result and the arguments of the method login to another method verifyUser that returns a boolean eg verifyUser(user, pass, dbUser, dbPass). this method should verify if the database values equal the passed arguments to method login.
hope this helps. still confused? post confusion here
#teamJava
Re: Please Help Me On Login Code In Java. by xtianh(m): 7:49am On Oct 21, 2013
danvery2k6: 1. create a table in your database called user or any name of your choice.
2. create two fields of type varchar called username and password with username as the primary key.
3. create a class called UserLogin with username and password as instance variables.
4.create a constructor that initialises these variables.
5. create method login that returns a boolean value and that takes two arguments: user and pass and fetches a row from the database where username = user
6. pass the result and the arguments of the method login to another method verifyUser that returns a boolean eg verifyUser(user, pass, dbUser, dbPass). this method should verify if the database values equal the passed arguments to method login.
hope this helps. still confused? post confusion here
#teamJava

my confusion cmes in at stage 5 down could u pls explain in mre details. Thanks fr ur effort
Re: Please Help Me On Login Code In Java. by danvery2k6(m): 1:36pm On Oct 22, 2013

/*
* For this code snippet to be useful to you. you must first of all do the
* follwing
* 1. Create a table in your database called user or any name.
*
* 2. create fields username(varchar) and password(varchar). set your primary
* key to username.
*
* 3.connect your database to your application (I'd like to presume you already
* know how to do that.
*
* 4. And bingo!!!!!!!!! you are ready to work with this piece of code. everything
* works dandy fine.
*
* 5. Post your confusions here and we will help you sort them out. We presume
* this is a begginer level tutorial.
* /////////////////////////////////////////////////////////////////////////
* Note that the main method is not posted here.
* make sure mysql-connector/j is included in your project file or you will not
* able to connect to your database.
*/
package com.court.info.sys;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
*
* @author HP USER
*/
public class Login {
//the class DBConnect contains the code to connect to your database.
// My crude attemt at enforcing software reusability.
DBConnect connection = new DBConnect();//connection class.

/*
* you need to call this method in your client code.
* If it returns true. access can be granted.
* If it returns false, access shoul not be granted.
**/
public boolean userLogin(String user, String pass){
boolean message = false;
if((user != null)&&(pass != null)){
try{
Connection connect = connection.getConnection();
String sql = "SELECT username, password FROM user WHERE username"
+ "= '"+user+"'";
Statement statement = connect.createStatement();
ResultSet result = statement.executeQuery(sql);
while(result.next()){
String username = result.getString("username"wink;
String password = result.getString("password"wink;
message = verifyUser(username, password, user, pass);
}
}catch(Exception ex){
System.err.println(ex.getMessage());
}
}
return message;
}
public boolean verifyUser(String dbUser, String dbPass,String user, String pass){
return (dbUser.equals(user)&&(dbPass.equals(pass)));
}


}


Re: Please Help Me On Login Code In Java. by Danyl(m): 8:53pm On Oct 25, 2013
y dont u resort to using one of dis ORM frameworks its safer n faster..... one of such is oracle toplink n hibernate or better still go for apache derby db cos packaging mysql wt ur apl might give u problems even if it works....
Re: Please Help Me On Login Code In Java. by AZeD1(m): 5:36pm On Oct 28, 2013
xtianh:

my confusion cmes in at stage 5 down could u pls explain in mre details. Thanks fr ur effort
A boolean value can either be true or false. That is also the same in the login process i.e its either the login details supplied by the user matches a previously stored value or they don't. So in stage 5, your login in function should take the user supplied variable (username/email and password) and compare with the values present in the database. Only one value in the database should return true.


PS: its horrible writing shorthand in a public forum, its ever more horrible when the so called offender is a programmer. If you do that in forums like stackoverflow, you would not get a reply.

(1) (Reply)

Why Is Data Stored And Not Information. / Please Is There Anyone Who Does Phonegap / How Did You Get Into Tech?(what Motivated You, Downfalls, What Discourage You)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 30
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.