Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package model;
  6.  
  7. // Import all necessary packages
  8. import java.sql.*;
  9.  
  10. /**
  11.  * Class that implements data base and all operations that can be made with it.
  12.  * @author kuba
  13.  */
  14. public class DataBase
  15. {
  16.     public DataBase()
  17.     {
  18.         try
  19.         {
  20.             Class.forName("oracle.jdbc.driver.OracleDriver");
  21.         }
  22.         catch(ClassNotFoundException e )
  23.         {
  24.             System.err.print("ERROR: Could not load database driver -> ");
  25.             System.err.println(e.getMessage());
  26.             System.exit(1);
  27.         }
  28.        
  29.         String uri = "jdbc:oracle:thin:@//ikar.elka.pw.edu.pl:1521/elka.elka.pw.edu.pl";
  30.         String user = "jsejdak";
  31.         String password = "jsejdak";
  32.        
  33.         try
  34.         {
  35.             dbConnection = DriverManager.getConnection(uri, user, password);
  36.         }
  37.         catch(SQLException e)
  38.         {
  39.             System.err.print("ERROR: Could not connect to database -> ");
  40.             System.err.println(e.getMessage());
  41.             System.exit(1);
  42.         }
  43.     }
  44.    
  45.     /**
  46.      * Creates connection and logs into data base
  47.      * @return 0 - success, 1 - failure
  48.      */
  49.     public int logIn(String login, String password) throws SQLException
  50.     {
  51.         Statement s = dbConnection.createStatement();
  52.        
  53.         ResultSet rset = s.executeQuery("SELECT haslo FROM handlowcy "
  54.                                           + "WHERE id_pracownika = "
  55.                                           + login);
  56.        
  57.         rset.next();
  58.         String storedPassword =  rset.getString(1);
  59.        
  60.         rset.close();
  61.         s.close();
  62.        
  63.         if(password.equals(storedPassword))
  64.             return 0;
  65.         else
  66.             return 1;
  67.     }
  68.    
  69.     public int logOut()
  70.     {
  71.         try
  72.         {
  73.             if(dbConnection != null)
  74.                 dbConnection.close();
  75.         }
  76.         catch(SQLException e)
  77.         {
  78.             System.err.print("ERROR: could not disconnect from database -> ");
  79.             System.err.println(e.getMessage());
  80.         }
  81.         return 0;
  82.     }
  83.    
  84.     private Connection dbConnection = null;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement