HarrJ

Day 27

Jul 19th, 2024 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4.  
  5. public class Day27B {
  6.     public static void main(String[] args) {
  7.         testConnection();
  8.         System.out.println("--------");
  9.         insertRow1("Lily's");
  10.     }
  11.    
  12.     static void testConnection() {
  13.         String connString = "jdbc:mysql://localhost:3306/db_sg_b2_24";
  14.         String userName = "root";
  15.         String passWord = "";
  16.         String sqlQuery = "SELECT COUNT(*)";
  17.        
  18.         try {
  19.             Connection conn = DriverManager.getConnection(connString, userName, passWord);
  20.             Statement stmt = conn.createStatement();
  21.             boolean result = stmt.execute(sqlQuery);
  22.            
  23.             if (result) {
  24.                 System.out.println("SQL Query executed");
  25.             }
  26.            
  27.             conn.close();
  28.         } catch (Exception e) {
  29.             System.out.println("error: " + e.toString());
  30.         }
  31.     }
  32.    
  33.     static void insertRow1(String txtIn) {
  34.         String connString = "jdbc:mysql://localhost:3306/db_sg_b2_24";
  35.         String userName = "root";
  36.         String passWord = "";
  37.         String sqlQuery = "INSERT INTO tbl_word_list(fld_word) VALUES ('" + txtIn + "')";
  38.        
  39.         try {
  40.             Connection conn = DriverManager.getConnection(connString, userName, passWord);
  41.            
  42.             Statement stmt = conn.createStatement();
  43.            
  44.             int rowsAffected = stmt.executeUpdate(sqlQuery);
  45.            
  46.             if (rowsAffected == 1) {
  47.                 System.out.println("New Row Added");
  48.             }
  49.            
  50.             conn.close();
  51.         } catch (Exception e) {
  52.             System.out.println("error: " + e.toString());
  53.         }
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment