Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package dbConnection;
  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. import shoppingCart.Product;
  10.  
  11. public class DbConnector {
  12.  
  13.     Connection connection = null;
  14.     public static void main(String[] args) {
  15.        
  16.         DbConnector dbConnector = new DbConnector();
  17.         dbConnector.connectToDb();
  18. //      dbConnector.createTable();
  19. //      dbConnector.insertSampleData();
  20.         dbConnector.getAllProducts();
  21.     }
  22.  
  23.     public Connection connectToDb() {
  24.         try {
  25.             connection = DriverManager.getConnection("jdbc:postgresql://localhost:5433/CORBA_2", "postgres",
  26.                     "P@ss_to_maciek_85");
  27.         } catch (SQLException e) {
  28.             // TODO Auto-generated catch block
  29.             e.printStackTrace();
  30.         }
  31.         return connection;
  32.     }
  33.  
  34.     public void closeConnection() {
  35.         try {
  36.             if (!connection.isClosed()) {
  37.                 connection.close();
  38.             }
  39.         } catch (SQLException e) {
  40.             // TODO Auto-generated catch block
  41.             e.printStackTrace();
  42.         }
  43.         System.out.println("Db connection closed");
  44.     }
  45.  
  46.     public void createTable() {
  47.         String createTableProducts = "CREATE TABLE PRODUCTS " + "(idProduct SERIAL PRIMARY KEY,  "
  48.                 + "productName varchar(255) unique not null, " + "productPrice  decimal not null,"
  49.                 + "productQuantity INTEGER not null) ";
  50.         try {
  51.             Statement statement = connection.createStatement();
  52.             statement.executeUpdate(createTableProducts);
  53.         } catch (SQLException e) {
  54.             // TODO Auto-generated catch block
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.  
  59.     public void insertSampleData() {
  60.         String insert = "INSERT INTO PRODUCTS VALUES(1,'BEER', 2.5, 10) ";
  61.         String insert1 = "INSERT INTO PRODUCTS VALUES(2,'WATER', 3.0, 10) ";
  62.         String insert2 = "INSERT INTO PRODUCTS VALUES(3, 'BREAD', 2.5, 15) ";
  63.         String insert3 = "INSERT INTO PRODUCTS VALUES(4, 'BUTTER', 5.0, 30) ";
  64.  
  65.         try {
  66.             Statement statement = connection.createStatement();
  67.             statement.executeUpdate(insert);
  68.             statement.executeUpdate(insert1);
  69.             statement.executeUpdate(insert2);
  70.             statement.executeUpdate(insert3);
  71.         } catch (SQLException e) {
  72.             // TODO Auto-generated catch block
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     public Product[] getAllProducts() {
  78.         ResultSet resultSet = null;
  79.         Product[] productTable = null;
  80.         String selectStatement = "Select idProduct, productName, productPrice, productQuantity from PRODUCTS";
  81.         try {
  82.             Statement statement = connection.createStatement();
  83.             resultSet = statement.executeQuery(selectStatement);
  84.            
  85.             productTable = new Product[4];
  86.             int i = 0;
  87.             while(resultSet.next()) {
  88.                 Product p = new Product(resultSet.getInt("idProduct"), resultSet.getString("productName"), resultSet.getDouble("productPrice"), resultSet.getInt("productQuantity"));
  89.                 productTable[i] = p;
  90.                 i++;
  91.             }
  92.             resultSet.close();
  93.         } catch (SQLException e) {
  94.             // TODO Auto-generated catch block
  95.             e.printStackTrace();
  96.         }
  97.         return productTable;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement