package com.github.javadev.orderdatabase;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatabaseService {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/orderdb";

    static final String USER = "root";
    static final String PASS = "";

    public List<Map<String, Object>> readAll() {
        List<Map<String, Object>> result = Collections.emptyList();
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            String sql = "SELECT id, first, last, age FROM orderdata";
            try (ResultSet resultSet = stmt.executeQuery(sql)) {
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    int age = resultSet.getInt("age");
                    String first = resultSet.getString("first");
                    String last = resultSet.getString("last");
                    
                    //Display values
                    System.out.print("ID: " + id);
                    System.out.print(", Age: " + age);
                    System.out.print(", First: " + first);
                    System.out.println(", Last: " + last);
                }
            }
        } catch (SQLException | ClassNotFoundException  se) {
            if (se instanceof MySQLSyntaxErrorException) {
                String detailMessage = ((MySQLSyntaxErrorException) se).getMessage();
                if (detailMessage.contains("orderdata' doesn't exist")) {
//                    createTable();
                    Logger.getLogger(DatabaseService.class.getName()).log(Level.SEVERE, "I am here");
                }
            }
            Logger.getLogger(DatabaseService.class.getName()).log(Level.SEVERE, null, se);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se) {
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
            }
        }
        return result;
    }
}

@@ -1180,19 +1180,22 @@
         
+Str
 in
-t
+g
  id = re
@@ -1205,21 +1205,25 @@
 tSet.get
-Int
+String
 (%22
+_
 id%22);%0A  
@@ -1244,14 +1244,23 @@
     
-int ag
+String firstNam
 e = 
@@ -1276,15 +1276,24 @@
 .get
-Int(%22ag
+String(%22firstNam
 e%22);
@@ -1320,21 +1320,26 @@
  String 
-first
+middleName
  = resul
@@ -1354,21 +1354,26 @@
 String(%22
-first
+middleName
 %22);%0A    
@@ -1395,20 +1395,23 @@
  String 
-last
+surname
  = resul
@@ -1426,20 +1426,23 @@
 String(%22
-last
+surname
 %22);%0A    
@@ -1462,45 +1462,8 @@
    %0A
-                    //Display values%0A
     
@@ -1553,19 +1553,27 @@
 (%22, 
-Age
+First
 : %22 + 
-ag
+firstNam
 e);%0A
@@ -1612,32 +1612,38 @@
 rint(%22, 
-First: %22 + first
+Middle: %22 + middleName
 );%0A     
@@ -1683,22 +1683,28 @@
 (%22, 
-Last: %22 + last
+surname: %22 + surname
 );%0A 
@@ -1789,17 +1789,16 @@
 ception 
- 
 se) %7B%0A  
@@ -2018,12 +2018,34 @@
 ) %7B%0A
-//
   
+                  try %7B%0A
     
@@ -2058,16 +2058,22 @@
         
+      
 createTa
@@ -2076,19 +2076,75 @@
 teTable(
+stmt
 );%0A
+                    %7D catch (SQLException ex) %7B%0A    
         
@@ -2227,19 +2227,16 @@
 RE, 
-%22I am here%22
+null, ex
 );%0A 
@@ -2242,32 +2242,36 @@
                 
+    
 %7D%0A            %7D%0A
@@ -2260,34 +2260,53 @@
   %7D%0A            
+ 
-%7D%0A
+   %7D else %7B%0A        
             Logg
@@ -2379,16 +2379,68 @@
 ll, se);
+                    %0A                %7D%0A            %7D
 %0A       
@@ -2811,10 +2811,958 @@
 ;%0A    %7D%0A
+    %0A    private void createTable(Statement stmt) throws SQLException %7B%0A        String sql = %22CREATE TABLE orderdata %22%0A                   + %22(_id VARCHAR(16) not NULL, %22%0A                   + %22 firstName VARCHAR(255), %22%0A                   + %22 middleName VARCHAR(255), %22%0A                   + %22 surname VARCHAR(255), %22%0A                   + %22 PRIMARY KEY ( _id ))%22; %0A        stmt.executeUpdate(sql);%0A        String restrictionUpdate =%0A            %22CREATE TRIGGER orderdata_upd BEFORE UPDATE ON orderdata FOR EACH ROW%5Cn%22%0A            + %22  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot update record';%5Cn%22%0A            + %22;%22;%0A        stmt.executeUpdate(restrictionUpdate);%0A        String restrictionDelete =%0A            %22CREATE TRIGGER orderdata_del BEFORE DELETE ON orderdata FOR EACH ROW%5Cn%22%0A            + %22  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Cannot delete record';%5Cn%22%0A            + %22;%22;%0A        stmt.executeUpdate(restrictionDelete);%0A    %7D%0A
 %7D%0A

Add restrictions to update/delete records.