Advertisement
HarrJ

B8 Day 27 SELECT with ORDER BY

Oct 11th, 2022 (edited)
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. package mattroseb8wk5;
  2. import java.sql.*;
  3.  
  4. //demonstrating SELECT and SELECT with ORDER BY
  5. public class Day27A {
  6.     private final String address = "jdbc:mysql://localhost:3306/mr_batch8_db";
  7.     private final String userName = "root";
  8.     private final String passWord = "";
  9.  
  10.     public static void main(String[] args) {
  11.         Day26Methods callMethods = new Day26Methods();
  12.        
  13.         callMethods.getRows();
  14.         System.out.println("-----~~~--Sorted by Category and Price--~~~-----");
  15.         callMethods.getRowsByCatPrice();
  16.         System.out.println("----~~~--Other Sorting Option--~~~----");
  17.         callMethods.getRows("7", "11");
  18.     }
  19.    
  20.     // dito na lang kayo gawa ng method ok lang yun kung ayaw nyo na ng extra file
  21.     public void getRows() {
  22.         String sqlQuery = "SELECT * FROM tbl_price_list ";
  23.        
  24.         try {
  25.             Connection conn = DriverManager.getConnection(
  26.                     address, userName, passWord);
  27.            
  28.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  29.            
  30.             ResultSet rs = stmt.executeQuery();
  31.            
  32.             while (rs.next()) {      
  33.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  34.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  35.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  36.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  37.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  38.             }
  39.            
  40.             conn.close();
  41.         } catch (Exception e) {
  42.             System.out.println(e);
  43.         }
  44.     }
  45.  
  46.     public void getRowsByCatPrice() {
  47.         String sqlQuery = "SELECT * FROM tbl_price_list "
  48.                 + "ORDER BY fld_pcategory,fld_price;";
  49.        
  50.         try {
  51.             Connection conn = DriverManager.getConnection(
  52.                     address, userName, passWord);
  53.            
  54.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  55.            
  56.             ResultSet rs = stmt.executeQuery();
  57.            
  58.             while (rs.next()) {      
  59.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  60.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  61.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  62.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  63.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  64.             }
  65.            
  66.             conn.close();
  67.         } catch (Exception e) {
  68.             System.out.println(e);
  69.         }
  70.     }
  71.  
  72.    
  73.     public void getRows(String fldSort, String sortOp) {
  74.         //fldSort = sort by name(1), price(2), category(3), manufacturer(4)
  75.         //sortOp = ASC(1), DESC(2)
  76.         String orderByFld = "fld_pname ";
  77.         String sortOrder = "ASC";
  78.         switch (fldSort) { // di na kasama yung name(1)
  79.             // kasi default naman na and fld_pname
  80.             case "2":
  81.                 orderByFld = "fld_price ";
  82.                 break;
  83.             case "3":
  84.                 orderByFld = "fld_pcategory ";
  85.                 break;
  86.             case "4":
  87.                 orderByFld = "fld_manufacturer ";
  88.                 break;
  89.         }
  90.         if (sortOp.equals("2")) {
  91.             sortOrder = "DESC";
  92.         }
  93.        
  94.         String sqlQuery = "SELECT * FROM tbl_price_list ORDER BY "
  95.                 + orderByFld + sortOrder;
  96.        
  97.         try {
  98.             Connection conn = DriverManager.getConnection(
  99.                     address, userName, passWord);
  100.            
  101.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  102.            
  103.             ResultSet rs = stmt.executeQuery();
  104.            
  105.             while (rs.next()) {      
  106.                 System.out.print(String.format("%3d | ", rs.getInt(1)));
  107.                 System.out.print(String.format("%-30s | ", rs.getString(2)));
  108.                 System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
  109.                 System.out.print(String.format("%-18s | ", rs.getString(4)));
  110.                 System.out.println(String.format("%-35s | ", rs.getString(5)));
  111.             }
  112.            
  113.             conn.close();
  114.         } catch (Exception e) {
  115.             System.out.println(e);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement