Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.sql.*;
- import java.util.Scanner;
- public class Day27UpdateV2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Day27UpdateV2 callMethods = new Day27UpdateV2();
- int pid;
- String pName;
- double pPrice;
- String pCat;
- String pManufacturer;
- String[] originalValue;
- String userInput;
- callMethods.getRows();
- System.out.print("Enter id of row to be updated >> ");
- pid = sc.nextInt();
- sc.nextLine();
- // gagamitin natin yung String[] getRows(int searchId) para
- // may pang puno tayo ng field kapag nag skip si user
- originalValue = callMethods.getRows(pid);
- System.out.print("Enter Updated Name: ");
- userInput = sc.nextLine();
- pName = callMethods.checkString(userInput, originalValue[1]);
- userInput = "";
- System.out.print("Enter Updated Price: ");
- userInput = sc.nextLine();
- pPrice = callMethods.checkDouble(userInput, originalValue[2]);
- userInput = "";
- System.out.print("Enter Updated Category: ");
- userInput = sc.nextLine();
- pCat = callMethods.checkString(userInput, originalValue[3]);
- userInput = "";
- System.out.print("Enter Updated Manufacturer: ");
- userInput = sc.nextLine();
- pManufacturer = callMethods.checkString(userInput, originalValue[4]);
- userInput = "";
- System.out.println(pName + ":" + originalValue[1]);
- System.out.println(pPrice + ":" + originalValue[2]);
- System.out.println(pCat + ":" + originalValue[3]);
- System.out.println(pManufacturer + ":" + originalValue[4]);
- // uncomment nyo yung nasa baba pag talagang napipilian na nya yung skip ninyo
- //int result = callMethods.updateRow(pName, pPrice, pCat, pManufacturer, pid);
- //System.out.println(result + " row(s) affected");
- }
- public int updateRow(String pName, double pPrice
- , String pCat, String pManufacturer, int pid) {
- int rowsAffected = 0;
- String sqlQuery = "UPDATE tbl_price_list SET fld_pname = ? "
- + ",fld_price = ? ,fld_pcategory = ? ,fld_manufacturer = ? "
- + "WHERE fld_pid = ?;";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setString(1, pName);
- stmt.setDouble(2, pPrice);
- stmt.setString(3, pCat);
- stmt.setString(4, pManufacturer);
- stmt.setInt(5, pid);
- rowsAffected = stmt.executeUpdate();
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- return rowsAffected;
- }
- // mga support ni UPDATE
- 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 String[] getRows(int searchId) {
- String[] resultArr = {"0","name","0.0","cat","manufacturer"};
- String sqlQuery = "SELECT * FROM tbl_price_list WHERE fld_pid = ? LIMIT 1";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setInt(1, searchId);
- ResultSet rs = stmt.executeQuery();
- rs.next();
- resultArr[0] = rs.getString(1);
- resultArr[1] = rs.getString(2);
- resultArr[2] = rs.getString(3);
- resultArr[3] = rs.getString(4);
- resultArr[4] = rs.getString(5);
- conn.close();
- } catch (Exception e) {
- }
- return resultArr;
- }
- public String checkString(String newValue, String oldValue) {
- String resStr = newValue;
- if (newValue.trim().isEmpty()) {
- resStr = oldValue;
- }
- return resStr;
- }
- public double checkDouble(String newValue, String oldValue) {
- double resDbl = 0.0;
- try {
- resDbl = Double.parseDouble(newValue);
- } catch (Exception e) {
- resDbl = Double.parseDouble(oldValue);
- }
- return resDbl;
- }
- public int checkInt(String newValue, String oldValue) {
- int resInt = 0;
- try {
- resInt = Integer.parseInt(newValue);
- } catch (Exception e) {
- resInt = Integer.parseInt(oldValue);
- }
- return resInt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment