Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.sql.*;
- //demonstrating SELECT and SELECT with ORDER BY
- public class Day27A {
- private final String address = "jdbc:mysql://localhost:3306/mr_batch8_db";
- private final String userName = "root";
- private final String passWord = "";
- public static void main(String[] args) {
- Day26Methods callMethods = new Day26Methods();
- callMethods.getRows();
- System.out.println("-----~~~--Sorted by Category and Price--~~~-----");
- callMethods.getRowsByCatPrice();
- System.out.println("----~~~--Other Sorting Option--~~~----");
- callMethods.getRows("7", "11");
- }
- // dito na lang kayo gawa ng method ok lang yun kung ayaw nyo na ng extra file
- public void getRows() {
- String sqlQuery = "SELECT * FROM tbl_price_list ";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- System.out.print(String.format("%3d | ", rs.getInt(1)));
- System.out.print(String.format("%-30s | ", rs.getString(2)));
- System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
- System.out.print(String.format("%-18s | ", rs.getString(4)));
- System.out.println(String.format("%-35s | ", rs.getString(5)));
- }
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- public void getRowsByCatPrice() {
- String sqlQuery = "SELECT * FROM tbl_price_list "
- + "ORDER BY fld_pcategory,fld_price;";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- System.out.print(String.format("%3d | ", rs.getInt(1)));
- System.out.print(String.format("%-30s | ", rs.getString(2)));
- System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
- System.out.print(String.format("%-18s | ", rs.getString(4)));
- System.out.println(String.format("%-35s | ", rs.getString(5)));
- }
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- public void getRows(String fldSort, String sortOp) {
- //fldSort = sort by name(1), price(2), category(3), manufacturer(4)
- //sortOp = ASC(1), DESC(2)
- String orderByFld = "fld_pname ";
- String sortOrder = "ASC";
- switch (fldSort) { // di na kasama yung name(1)
- // kasi default naman na and fld_pname
- case "2":
- orderByFld = "fld_price ";
- break;
- case "3":
- orderByFld = "fld_pcategory ";
- break;
- case "4":
- orderByFld = "fld_manufacturer ";
- break;
- }
- if (sortOp.equals("2")) {
- sortOrder = "DESC";
- }
- String sqlQuery = "SELECT * FROM tbl_price_list ORDER BY "
- + orderByFld + sortOrder;
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- System.out.print(String.format("%3d | ", rs.getInt(1)));
- System.out.print(String.format("%-30s | ", rs.getString(2)));
- System.out.print(String.format("%7.2f | ", rs.getDouble(3)));
- System.out.print(String.format("%-18s | ", rs.getString(4)));
- System.out.println(String.format("%-35s | ", rs.getString(5)));
- }
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement