HarrJ

Day 20 Connection

Nov 30th, 2023 (edited)
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. package week3;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Scanner;
  7.  
  8. public class Day20C {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         String fName, lName, jobTitle, gender;
  12.         int age, deptID;
  13.        
  14.         System.out.print("Enter first name: ");
  15.         fName = sc.nextLine();
  16.         System.out.print("Enter last name: ");
  17.         lName = sc.nextLine();
  18.         System.out.print("Enter Age: ");
  19.         age = sc.nextInt();
  20.         sc.nextLine();
  21.         System.out.print("Enter job title: ");
  22.         jobTitle = sc.nextLine();
  23.         System.out.print("Enter Department ID: ");
  24.         deptID = sc.nextInt();
  25.         sc.nextLine();
  26.         System.out.print("Enter gender(M/F): ");
  27.         gender = sc.nextLine();
  28.        
  29.         addNewRowTest(fName, lName, age, jobTitle, deptID, gender);
  30.        
  31.     }
  32.    
  33.     static void addNewRowTest(String fName, String lName, int age, String jobTitle, int deptID, String gender) {
  34.         String connectionString = "jdbc:mysql://localhost:3306/db_mng_b11";
  35.         String userName = "mngb11";
  36.         String passWord = "2023";
  37.         try {
  38.             Connection conn = DriverManager.getConnection(connectionString, userName, passWord);  
  39.             Statement stmt = conn.createStatement();
  40.             String sqlQuery = "INSERT INTO tbl_employee(fld_fname, fld_lname, fld_age"
  41.                     + ", fld_position, fld_did, fld_gender)"
  42.                     + " VALUES ('" + fName + "','" + lName + "','" + age + "'"
  43.                     + ",'" + jobTitle + "','" + deptID + "','" + gender + "');";
  44.            
  45.             System.out.println(sqlQuery);
  46.             int rowAffected = stmt.executeUpdate(sqlQuery);
  47.             System.out.println(rowAffected + " row(s) affected");
  48.             conn.close();
  49.         }   catch (SQLException ex) {
  50.             System.out.println("error: " + ex.toString());
  51.         }
  52.     }
  53. }
  54.  
  55. //--- table query------------------
  56. CREATE TABLE tbl_employee
  57. (fld_fname VARCHAR(50)
  58. , fld_lname VARCHAR(50)
  59. , fld_age INT
  60. , fld_position VARCHAR(50)
  61. , fld_did INT
  62. , fld_gender VARCHAR(1)
  63. );
  64.  
  65. // --- EXECUTE UPDATE INSERT INTO ---------------------------
  66. package week3;
  67. import java.sql.Connection;
  68. import java.sql.DriverManager;
  69. import java.sql.PreparedStatement;
  70. import java.sql.SQLException;
  71. import java.util.Scanner;
  72.  
  73. public class Day20D {
  74.     public static void main(String[] args) {
  75.         Scanner sc = new Scanner(System.in);
  76.         String fName = "", lName = "", jobTitle = "", gender = "";
  77.         int age = 0, deptID = 0;
  78.         boolean canContinue = true;
  79.        
  80.         try {
  81.             System.out.print("Enter first name: ");
  82.             fName = sc.nextLine();
  83.             System.out.print("Enter last name: ");
  84.             lName = sc.nextLine();
  85.             System.out.print("Enter Age: ");
  86.             age = sc.nextInt();
  87.             sc.nextLine();
  88.             System.out.print("Enter job title: ");
  89.             jobTitle = sc.nextLine();
  90.             System.out.print("Enter Department ID: ");
  91.             deptID = sc.nextInt();
  92.             sc.nextLine();
  93.             System.out.print("Enter gender(M/F): ");
  94.             gender = sc.nextLine();
  95.         } catch (Exception e) {
  96.             System.out.println("One of user input is invalid");
  97.             canContinue = false;
  98.         }
  99.         if (canContinue) {
  100.             int result = addNewRow(fName, lName, age, jobTitle, deptID, gender);
  101.             System.out.println(result + " row(s) affected");
  102.         }
  103.     }
  104.    
  105.     static int addNewRow(String fName, String lName, int age, String jobTitle, int deptID, String gender) {
  106.         String connectionString = "jdbc:mysql://localhost:3306/db_mng_b11";
  107.         String userName = "mngb11";
  108.         String passWord = "2023";
  109.         int rowAffected = 0;
  110.         try {
  111.             Connection conn = DriverManager.getConnection(connectionString, userName, passWord);
  112.            
  113.             String sqlQuery = "INSERT INTO tbl_employee "
  114.                     + "(fld_fname, fld_lname, fld_age"
  115.                     + ", fld_position, fld_did, fld_gender)"
  116.                     + " VALUES ( ?, ?, ?, ?, ?, ?);";
  117.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  118.             stmt.setString(1, fName);
  119.             stmt.setString(2, lName);
  120.             stmt.setInt(3, age);
  121.             stmt.setString(4, jobTitle);
  122.             stmt.setInt(5, deptID);
  123.             stmt.setString(6, gender);
  124.            
  125. //            System.out.println(sqlQuery); // nandito lang to pang test
  126.             rowAffected = stmt.executeUpdate();
  127.             conn.close();
  128.         }   catch (SQLException ex) {
  129.             System.out.println("error: " + ex.toString());
  130.         }
  131.         return rowAffected;
  132.     }
  133. }
  134.  
  135. // --- EXECUTE QUERY SELECT---------------------------
  136. package week3;
  137. import java.sql.Connection;
  138. import java.sql.DriverManager;
  139. import java.sql.PreparedStatement;
  140. import java.sql.ResultSet;
  141. import java.sql.SQLException;
  142.  
  143. public class Day20E {
  144.     public static void main(String[] args) {
  145.         getAllRows();
  146.     }
  147.    
  148.     static void getAllRows() {
  149.         String connectionString = "jdbc:mysql://localhost:3306/db_mng_b11";
  150.         String userName = "mngb11";
  151.         String passWord = "2023";
  152.         try {
  153.             Connection conn = DriverManager.getConnection(connectionString, userName, passWord);
  154.            
  155.             String sqlQuery = "SELECT * FROM tbl_employee;";
  156.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  157.             ResultSet rs = stmt.executeQuery();
  158.             while (rs.next()) {
  159.                 System.out.print(rs.getString(1) + " | ");
  160.                 System.out.print(rs.getString(2) + " | ");
  161.                 System.out.println(rs.getString(3));
  162.             }
  163.             System.out.println(rs);
  164. //            System.out.println(sqlQuery); // nandito lang to pang test
  165.  
  166.             conn.close();
  167.         }   catch (SQLException ex) {
  168.             System.out.println("error: " + ex.toString());
  169.         }
  170.     }
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment