1 package edu.rice.rubis.servlets; 2 3 import java.sql.Connection ; 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 8 public class Auth 9 { 10 11 private Connection conn = null; 13 private ServletPrinter sp; 14 15 public Auth(Connection connect, ServletPrinter printer) 16 { 17 conn = connect; 18 sp = printer; 19 } 20 21 public int authenticate(String name, String password) 22 { 23 int userId = -1; 24 ResultSet rs = null; 25 PreparedStatement stmt = null; 26 27 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 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(); } 56 catch (Exception ignore) 57 { 58 } 59 return userId; 60 } 61 } 62 63 } 64 | Popular Tags |