Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mattroseb8wk5;
- import java.util.Scanner;
- import java.sql.*;
- public class Day26A {
- 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);
- Day26A callMe = new Day26A();
- int pid;
- String confirm = "NO";
- callMe.getRows();
- System.out.print("\nEnter id of row to be DELETED >> ");
- pid = sc.nextInt();
- sc.nextLine();
- System.out.println();
- System.out.println("Are you sure you want to delete row with id "
- + pid + "?(YES/NO)");
- confirm = sc.nextLine();
- if (confirm.equals("YES")) {
- int rowAffected = callMe.deleteRow(pid);
- System.out.println(rowAffected + " row deleted");
- }
- }
- 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 int deleteRow(int pid) {
- int rowsAffected = 0;
- String sqlQuery = "DELETE FROM tbl_price_list WHERE fld_pid = ?;";
- try {
- Connection conn = DriverManager.getConnection(
- address, userName, passWord);
- PreparedStatement stmt = conn.prepareStatement(sqlQuery);
- stmt.setInt(1, pid);
- rowsAffected = stmt.executeUpdate();
- conn.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- return rowsAffected;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment