KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > Auth


1 package edu.rice.rubis.servlets;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 public class Auth
9 {
10
11   //private Context servletContext;
12
private Connection JavaDoc conn = null;
13   private ServletPrinter sp;
14
15   public Auth(Connection JavaDoc connect, ServletPrinter printer)
16   {
17     conn = connect;
18     sp = printer;
19   }
20
21   public int authenticate(String JavaDoc name, String JavaDoc password)
22   {
23     int userId = -1;
24     ResultSet JavaDoc rs = null;
25     PreparedStatement JavaDoc stmt = null;
26
27     // Lookup the user
28
try
29     {
30       stmt =
31         conn.prepareStatement(
32           "SELECT users.id FROM users WHERE nickname=? AND password=?");
33       stmt.setString(1, name);
34       stmt.setString(2, password);
35       rs = stmt.executeQuery();
36       if (!rs.first())
37       {
38         sp.printHTML(
39           " User " + name + " does not exist in the database!<br><br>");
40         return userId;
41       }
42       userId = rs.getInt("id");
43     }
44     catch (SQLException JavaDoc e)
45     {
46       sp.printHTML("Failed to executeQuery " + e);
47       return userId;
48     }
49     finally
50     {
51       try
52       {
53         if (stmt != null)
54           stmt.close(); // close statement
55
}
56       catch (Exception JavaDoc ignore)
57       {
58       }
59       return userId;
60     }
61   }
62
63 }
64
Popular Tags