Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. package exercise3Lib_8140058;
  2.  
  3. import java.sql.*;
  4. import java.io.*;
  5.  
  6. public class UserDAO_EX3_8140058 {
  7.  
  8.     private String errorMessages = "";
  9.  
  10.     private PreparedStatement stmt = null;
  11.  
  12.     private ResultSet rs = null;
  13.  
  14.     private Connection con = null;
  15.  
  16.     private final String userQuery = "select username from user_8xxxxxx where username =? and password=?";
  17.  
  18.     private String usern, passw, email;
  19.  
  20.     public User authenticateUser(String username, String password) throws Exception {
  21.  
  22.         DB_EX3_8140058 dataB = new DB_EX3_8140058();
  23.  
  24.         dataB.open();
  25.         con = dataB.getConnection();
  26.  
  27.         try {
  28.  
  29.             stmt = con.prepareStatement(userQuery);
  30.             // replacing the first ? with username and the second ? with
  31.             // password
  32.             stmt.setString(1, username);
  33.             stmt.setString(2, password);
  34.             // execute query
  35.             rs = stmt.executeQuery();
  36.  
  37.             int counter = 0;
  38.  
  39.             while (rs.next())
  40.                 counter++;
  41.  
  42.             if (counter == 1) {
  43.                 User userVal = new User(rs.getString("username"),rs.getString("password"),rs.getString("email"));
  44.                 stmt.close();
  45.                 rs.close();
  46.                 con.close();
  47.                 dataB.close();
  48.                 return userVal;
  49.             } else {
  50.                 stmt.close();
  51.                 rs.close();
  52.                 con.close();
  53.                 dataB.close();
  54.                 throw new Exception("Wrong username or password");
  55.             }
  56.         } catch (SQLException ex)  {
  57.             ex.printStackTrace();
  58.         }
  59.     }
  60. }
  61.  
  62.  
  63. Compile Error:
  64. C:\Users\JIM\Desktop\exercise3Lib_8140058\UserDAO_EX3_8140058.java:59: error: missing return statement
  65.     }
  66.     ^
  67. 1 error
  68.  
  69. Tool completed with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement