User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,523 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,858 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 673 | Replies: 9 | Solved
Reply
Join Date: Nov 2007
Location: Nigeria
Posts: 49
Reputation: tactfulsaint is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tactfulsaint tactfulsaint is offline Offline
Light Poster

username@password

  #1  
Jul 22nd, 2008
hello everyone
I still need code snippet help with authentication of username and password on both java and web application..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 553
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 63
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: username@password

  #2  
Jul 22nd, 2008
The code will be the same. It doesn't matter for what you are going to use it. I will post the code soon, after I finish some other issues I have.
I AM the 12th CYLON
Reply With Quote  
Join Date: Nov 2007
Location: Nigeria
Posts: 49
Reputation: tactfulsaint is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tactfulsaint tactfulsaint is offline Offline
Light Poster

Re: username@password

  #3  
Jul 22nd, 2008
Originally Posted by javaAddict View Post
The code will be the same. It doesn't matter for what you are going to use it. I will post the code soon, after I finish some other issues I have.
ok will be waiting
thanks
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 553
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 63
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: username@password

  #4  
Jul 22nd, 2008
Inside a class declare the variable:

Connection conn=null;

In the same class:

Method for opening connection. Uses the myssql driver. download the jar and add it to the jars of your project

protected void openConnection() throws Exception {
        if ((conn==null)||(conn.isClosed())) {

String ip = "10.23.14.5"; //the ip of the database, in your case it would be localhost
String db_name="name_of_the_DB";
String user="username"; //the username that you use to login to the database
String pass="password"; //the password you use to login to the datatabase
 
            Class.forName("com.mysql.jdbc.Driver").newInstance();            conn=DriverManager.getConnection("jdbc:mysql://"+ip+"/"+db_name,user,pass);
        }
    }

Closing connection:
protected void closeConnection() throws Exception {
        if ((conn!=null)&&(!conn.isClosed()))
            conn.close();
    }

Use the above methods to open the conn and use it to run queries:

public boolean validatePassword(String user, String pass) throws Exception {
        this.openConnection();
        
        String query="select username, password from users where username='"+user+"' and password='"+pass+"'";
        Statement st=null;
        ResultSet rs=null;
        String u=null;
String p=null;
        
        try {
            st=conn.createStatement();
            rs=st.executeQuery(query);

                if (rs.next()) {
                    u=rs.getString(1);
p=rs.getString(2);
                }
                rs.close();
                rs=null;
                st.close();
                st=null;
this.closeConnection();
return ((u.equals(user))&&(p.equals(pass)));
        } catch (Exception e) {
            System.out.println(e.getMessage());
this.closeConnection();
        }
        return false;
    }
I AM the 12th CYLON
Reply With Quote  
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation: ChaseVoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 10
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: username@password

  #5  
Jul 22nd, 2008
Amazing, This code is really helpful.
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 553
Reputation: javaAddict will become famous soon enough javaAddict will become famous soon enough 
Rep Power: 3
Solved Threads: 63
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Pro

Re: username@password

  #6  
Jul 23rd, 2008
Originally Posted by ChaseVoid View Post
Amazing, This code is really helpful.


It can be found in any book and tutorial. And I wrote this a long time ago, meaning that there are better versions of that
I AM the 12th CYLON
Reply With Quote  
Join Date: Nov 2007
Location: Nigeria
Posts: 49
Reputation: tactfulsaint is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tactfulsaint tactfulsaint is offline Offline
Light Poster

Re: username@password

  #7  
Jul 24th, 2008
thanks man, i found what i need your the best !!!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: username@password

  #8  
Jul 24th, 2008
> String ip = "10.23.14.5"; //the ip of the database, in your case it would be localhost

Prefer configuration over code; the URL for the database along with the database name almost always comes from a configuration file. Also for database intensive applications/operations, consider using a Connection Pool instead of grabbing raw connections. Connection object creation is an expensive activity giving the fact that your application will perform a lot many short lived Database activities. Connection pools give your the power of configuring a variety of parameters like the pool size, the connection purge policy etc.

> String query="select username, password from users where username='"+user+"' and 
> password='"+pass+"'";
Vulnerable to SQL Injection. Using Prepared Statements prevents this as well as the Statement pooling offered by a lot many Type 4 drivers reduces the execution time.

>
u.equals(user))&&(p.equals(pass))
Though not applicable to this discussion maybe, password are almost always never stored in plain text format. Consider encrypting your passwords and salting them. Industry strength J2EE applications also make use of a LDAP for authentication instead of storing passwords in plain old database tables. They provide you the flexibility of organizing the users of your application in groups and hierarchies and their easy management.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Nov 2007
Location: Nigeria
Posts: 49
Reputation: tactfulsaint is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
tactfulsaint tactfulsaint is offline Offline
Light Poster

Re: username@password

  #9  
Aug 14th, 2008
got any idea on how to encript the datas instead of storinr it in a plain text. give us a code snipet. thank y'all for your contributions.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: username@password

  #10  
Aug 14th, 2008
A simple google search should get you going.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 6:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC