Guest User

DBConnection.java

a guest
Jul 20th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.prgguru.jersey;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class DBConnection {
  10.     /**
  11.      * Method to create DB Connection
  12.      *
  13.      * @return
  14.      * @throws Exception
  15.      */
  16.     @SuppressWarnings("finally")
  17.     public static Connection createConnection() throws Exception {
  18.         Connection con = null;
  19.         try {
  20.             Class.forName(Constants.dbClass);
  21.             con = DriverManager.getConnection(Constants.dbUrl, Constants.dbUser, Constants.dbPwd);
  22.         } catch (Exception e) {
  23.             throw e;
  24.         } finally {
  25.             return con;
  26.         }
  27.     }
  28.     /**
  29.      * Method to check whether uemail and pwd combination are correct
  30.      *
  31.      * @param uemail
  32.      * @param pwd
  33.      * @return
  34.      * @throws Exception
  35.      */
  36.     public static boolean checkLogin(String uemail, String pwd) throws Exception {
  37.         boolean isUserAvailable = false;
  38.         Connection dbConn = null;
  39.         try {
  40.             try {
  41.                 dbConn = DBConnection.createConnection();
  42.             } catch (Exception e) {
  43.                 // TODO Auto-generated catch block
  44.                 e.printStackTrace();
  45.             }
  46.             Statement stmt = dbConn.createStatement();
  47.             String query = "SELECT * FROM usuario WHERE email = '" + uemail
  48.                     + "' AND senha=" + "'" + pwd + "'";
  49.             //System.out.println(query);
  50.             ResultSet rs = stmt.executeQuery(query);
  51.             while (rs.next()) {
  52.                 //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
  53.                 isUserAvailable = true;
  54.             }
  55.         } catch (SQLException sqle) {
  56.             throw sqle;
  57.         } catch (Exception e) {
  58.             // TODO Auto-generated catch block
  59.             if (dbConn != null) {
  60.                 dbConn.close();
  61.             }
  62.             throw e;
  63.         } finally {
  64.             if (dbConn != null) {
  65.                 dbConn.close();
  66.             }
  67.         }
  68.         return isUserAvailable;
  69.     }
  70.     /**
  71.      * Method to insert uemail and pwd in DB
  72.      *
  73.      * @param name
  74.      * @param uname
  75.      * @param pwd
  76.      * @return
  77.      * @throws SQLException
  78.      * @throws Exception
  79.      */
  80.    
  81. }
Add Comment
Please, Sign In to add comment