Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet and JDBC

 
ppavya india
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic