*
The moose likes Servlets and the fly likes Servlet and JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet and JDBC" Watch "Servlet and JDBC" New topic
Author

Servlet and JDBC

ppavya india
Greenhorn

Joined: Sep 27, 2004
Posts: 19
Hi all,
I am new to servlet technology, I have created one table in SQL as "users" with column name as login, password, username and I have already added 5 records, I have also a html page where I have only two fields i.e. login and password. What I want to do, to ask user to put input as login and password and if the user enters same user name and password which I have already created in users table, then user'll get complete username, if he enters wrong input then he'll get an error message.
How to create servlet for these things. Any help would be appreciated
Thanx in advance
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Second Notice

"ppavya ppavya"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
First, you need to install a jdbc driver which can be accessed by your server.

Second, modify the servlet code to access database.
e.g.,

import java.sql.*;

public LoginServlet extends HttpServlet{

doGet(request,response){
String account= request.getParameter("account");
String password = request.getParameter("password");
//connect to database

Class.forName(jdbcDriver name);
Connection con = DriverManager.getConnection(connection string);
PreparedStatement st = con.prepareStatement("select * from users " +
"where account=? and password=?");
st.setString(1,account);
st.setString(2,password);
ResultSet rsQuery = st.executeQuery();
if (rsQuery.next()){
//show welcome message
}
else{
//show error message
}
}


}
[ December 23, 2004: Message edited by: danny liu ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet and JDBC