Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.util.Scanner;
- import java.sql.*;
- public class Day25A {
- 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) {
- Scanner sc = new Scanner(System.in);
- Day25Methods callMe = new Day25Methods();
- int pid;
- String pName;
- double pPrice;
- String pCat;
- String pManufacturer;
- callMe.getRows();
- System.out.print("Enter id of row to be updated");
- pid = sc.nextInt();
- sc.nextLine();
- System.out.print("Enter Updated Name: ");
- pName = sc.nextLine();
- System.out.print("Enter Updated Price: ");
- pPrice = sc.nextDouble();
- sc.nextLine();
- System.out.print("Enter Updated Category: ");
- pCat = sc.nextLine();
- System.out.print("Enter Updated Manufacturer: ");
- pManufacturer = sc.nextLine();
- int result = callMe.updateRow(pName, pPrice, pCat, pManufacturer, pid);
- System.out.println(result + " row(s) affected");
- }
- public void getRows(String searchVal) {
- searchVal = "%".concat(searchVal).concat("%");
- String sqlQuery = "SELECT * FROM tbl_price_list WHERE fld_pname LIKE ?";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setString(1, searchVal);
- 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 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment