•
•
•
•
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
![]() |
Inside a class declare the variable:
In the same class:
Method for opening connection. Uses the myssql driver. download the jar and add it to the jars of your project
Closing connection:
Use the above methods to open the conn and use it to run queries:
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
•
•
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation:
Rep Power: 0
Solved Threads: 10
> 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.
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.
>
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.
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+"'";
>
u.equals(user))&&(p.equals(pass))
I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
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.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- "net user [username [password | *]" (Windows NT / 2000 / XP / 2003)
- parents forgot username and password (Windows NT / 2000 / XP / 2003)
- Missing username and password field - Novell (Novell)
- Login - Get the Correct Username and Password in ASP.NET Using VB (ASP.NET)
- Username / password using C# (C#)
- Have IE prompt for username/password?? (Web Browsers)
- Require Username/password?? (Web Browsers)
- IE6 Problem Username/Password Link Error (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Drag n Drop in same JList
- Next Thread: need help with applet calling another applet



Linear Mode