Guest User

Untitled

a guest
Dec 25th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Locale;
  3.  
  4.  
  5. public class MyDBConnection {
  6.     String url = "jdbc:oracle:thin:@localhost:1521:xe";
  7.     String login = "system";
  8.     String pass = "1";
  9.  
  10.     Connection connection;
  11.     PreparedStatement preparedStatement;
  12.  
  13.     public MyDBConnection(){
  14.         try {
  15.             Locale.setDefault(Locale.ENGLISH);
  16.  
  17.             connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "1");
  18.  
  19.         } catch (SQLException e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     public User checkRegistration(String login, String pass) throws SQLException {
  25.  
  26.         preparedStatement = connection.prepareStatement(
  27.                 "SELECT * " +
  28.                 "FROM tourists " +
  29.                 "WHERE login = ? AND pass = ?"
  30.         );
  31.  
  32.         preparedStatement.setString(1, login);
  33.         preparedStatement.setString(2, pass);
  34.  
  35.         ResultSet result = preparedStatement.executeQuery();
  36.  
  37.         if (result.next()) {
  38.             User user = new User(result.getString("login"));
  39.             return user;
  40.         }
  41.  
  42.         return null;
  43.     }
  44.  
  45.     public void registerUser(String login, String pass, String email) throws SQLException {
  46.  
  47.         preparedStatement = connection.prepareStatement(
  48.                 "INSERT INTO tourists VALUES (?, ?, ?)"
  49.         );
  50.  
  51.         preparedStatement.setString(1, login);
  52.         preparedStatement.setString(2, pass);
  53.         preparedStatement.setString(3, email);
  54.  
  55.         preparedStatement.executeUpdate();
  56.     }
  57.  
  58.     public void getFriendListForUser(User user) throws SQLException {
  59.  
  60.         preparedStatement = connection.prepareStatement(
  61.                 "SELECT friend " +
  62.                 "FROM friends " +
  63.                 "WHERE login = ?"
  64.         );
  65.  
  66.         preparedStatement.setString(1, user.name);
  67.  
  68.         ResultSet result = preparedStatement.executeQuery();
  69.  
  70.         if (result.next()) {
  71.             user.addFriend(new User(result.getString("friend")));
  72.         }
  73.     }
  74.  
  75.     public void getTripListForUser(User user) throws SQLException {
  76.  
  77.         preparedStatement = connection.prepareStatement(
  78.                 "SELECT trips.trip_id, trips.trip_name " +
  79.                 "FROM trips JOIN tourist_link_trip ON trips.trip_id = tourist_link_trip.trip_id" +
  80.                 "WHERE tourist_link_trip = ?"
  81.         );
  82.  
  83.         preparedStatement.setString(1, user.name);
  84.  
  85.         ResultSet result = preparedStatement.executeQuery();
  86.  
  87.         if (result.next()) {
  88.             user.addTrip(new Trip(result.getInt("trip_id") ,result.getString("trip_name"), null, null));
  89.         }
  90.     }
  91.  
  92.     public void delTrip(Trip trip) throws SQLException {
  93.  
  94.         preparedStatement = connection.prepareStatement(
  95.                 "DELETE FROM trips WHERE trips.trip_id = ?"
  96.         );
  97.  
  98.         preparedStatement.setInt(1, trip.id);
  99.  
  100.         preparedStatement.executeUpdate();
  101.  
  102.  
  103.         preparedStatement = connection.prepareStatement(
  104.                 "DELETE FROM tourist_link_trip WHERE tourist_link_trip.trip_id = ?"
  105.         );
  106.  
  107.         preparedStatement.setInt(1, trip.id);
  108.  
  109.         preparedStatement.executeUpdate();
  110.     }
  111.  
  112.     public void addNewTripByUser(User user, String trip_name, String description) throws SQLException {
  113.  
  114.         CallableStatement cst = connection.prepareCall(
  115.                 "begin " +
  116.                 "? := insert_trip_and_return_id(?, ?, ?); " +
  117.                 "end;"
  118.         );
  119.  
  120.         cst.registerOutParameter(1, Types.INTEGER);
  121.         cst.setString(2, null);
  122.         cst.setString(3, trip_name);
  123.         cst.setString(4, description);
  124.  
  125.         cst.executeUpdate();
  126.  
  127.         Integer id = cst.getInt(1);
  128.  
  129.         preparedStatement = connection.prepareStatement(
  130.                 "INSERT INTO tourist_link_trip VALUES " +
  131.                         "(?, ?)"
  132.         );
  133.  
  134.         preparedStatement.setString(1, user.name);
  135.         preparedStatement.setInt(2, id);
  136.  
  137.         preparedStatement.executeUpdate();
  138.     }
  139.  
  140.     public void addNewItemToTrip(Trip trip, TripElement item) throws SQLException {
  141.  
  142.         preparedStatement = connection.prepareStatement(
  143.                 "INSERT INTO items VALUES " +
  144.                         "(?, ?, ?, ?, ?, ?)"
  145.         );
  146.  
  147.         preparedStatement.setString(1, null);
  148.         preparedStatement.setInt(2, trip.id);
  149.         preparedStatement.setString(3, item.name);
  150.         preparedStatement.setInt(4, item.number);
  151.         preparedStatement.setString(5, item.description);
  152.         preparedStatement.setString(6, item.handler);
  153.  
  154.         preparedStatement.executeUpdate();
  155.     }
  156.  
  157.     public void getItemListForTrip(Trip trip) throws SQLException {
  158.  
  159.         preparedStatement = connection.prepareStatement(
  160.                 "SELECT * " +
  161.                 "FROM items " +
  162.                 "WHERE trip_id = ?"
  163.         );
  164.  
  165.         preparedStatement.setInt(1, trip.id);
  166.  
  167.         ResultSet result = preparedStatement.executeQuery();
  168.  
  169.         if (result.next()) {
  170.             TripElement item = new TripElement(result.getString("item_name"));
  171.             item.number = result.getInt("item_number");
  172.             item.id = result.getInt("item_id");
  173.             item.description = result.getString("desctription");
  174.             item.handler = result.getString("handler_name");
  175.  
  176.             trip.addThing(item);
  177.         }
  178.     }
  179. }
Add Comment
Please, Sign In to add comment