Guest User

Untitled

a guest
Apr 19th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package soft234.rental;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DatabaseConnector {
  6.  
  7.     Statement selectStmt;
  8.     Connection myConnection;
  9.  
  10.     public DatabaseConnector() {
  11.     }
  12.  
  13.     public void connect() throws Exception {
  14.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  15.         myConnection = DriverManager.getConnection("jdbc:odbc:Resources_Rental", "", "");
  16.         selectStmt = myConnection.createStatement();
  17.     }
  18.  
  19.     public void registerUser(String surname, String forename,
  20.             String phone, String email, String password) throws SQLException {
  21.  
  22.         selectStmt.executeUpdate("INSERT INTO Member "
  23.                 + "(Surname, Forenames, Phone, Email, Password)\n"
  24.                 + "VALUES ('" + surname
  25.                 + "', '" + forename
  26.                 + "', '" + phone
  27.                 + "', '" + email
  28.                 + "', '" + password
  29.                 + "')");
  30.  
  31.     }
  32.  
  33.     public ResultSet getDvds() throws SQLException {
  34.         return selectStmt.executeQuery("SELECT *\n"
  35.                 + "FROM DVD");
  36.     }
  37.  
  38.     public ResultSet getEmailAddresses() throws SQLException {
  39.         return selectStmt.executeQuery("SELECT Email\nFROM Member");
  40.     }
  41.  
  42.     public ResultSet getUser(String usr, String pw) throws SQLException {
  43.         return selectStmt.executeQuery("SELECT Email, Forenames\n"
  44.                 + "FROM Member\n"
  45.                 + "WHERE StrComp(Password,'" + pw + "',0) = 0\n"
  46.                 + "AND Email = '" + usr + "'");
  47.     }
  48.  
  49.     public ResultSet getUserInfoByEmail(String email) throws SQLException {
  50.         return selectStmt.executeQuery("SELECT *\n"
  51.                 + "FROM Member\n"
  52.                 + "WHERE Email = '" + email + "'");
  53.     }
  54.  
  55.     public ResultSet getCurrentRentals(String email) throws SQLException {
  56.         return selectStmt.executeQuery(
  57.                 "SELECT RentalID, DVD.Title, Date\n"
  58.                 + "FROM Rental, DVD, Member\n"
  59.                 + "WHERE Rental.DVDID = DVD.DVDID\n"
  60.                 + "AND Rental.MemberID = Member.MemberID\n"
  61.                 + "AND Member.Email = '" + email + "'\n"
  62.                 + "AND Current = 1");
  63.     }
  64.  
  65.     public ResultSet getReturnedRentals(String email) throws SQLException {
  66.         return selectStmt.executeQuery(
  67.                 "SELECT RentalID, DVD.Title, Date\n"
  68.                 + "FROM Rental, DVD, Member\n"
  69.                 + "WHERE Rental.DVDID = DVD.DVDID\n"
  70.                 + "AND Rental.MemberID = Member.MemberID\n"
  71.                 + "AND Member.Email = '" + email + "'\n"
  72.                 + "AND Current = 0\n"
  73.                 + "ORDER BY Date DESC");
  74.     }
  75.  
  76.     public ResultSet getDvdById(int id) throws SQLException {
  77.         return selectStmt.executeQuery("SELECT *\n"
  78.                 + "FROM DVD\n"
  79.                 + "WHERE DVDID = " + id);
  80.     }
  81.  
  82.     public ResultSet getCheapDvds() throws SQLException {
  83.         return selectStmt.executeQuery(
  84.                 "SELECT *\n"
  85.                 + "FROM DVD\n"
  86.                 + "WHERE Cost <= 2");
  87.     }
  88.  
  89.     public ResultSet getSearchResults(String search) throws SQLException {
  90.         return selectStmt.executeQuery(
  91.                 "SELECT *\n"
  92.                 + "FROM DVD\n"
  93.                 + "WHERE Title LIKE '%" + search + "%'");
  94.     }
  95.  
  96.     public void closeConnection() throws SQLException {
  97.         myConnection.close();
  98.     }
  99. }
Add Comment
Please, Sign In to add comment