Changeset 34894


Ignore:
Timestamp:
2021-02-17T15:20:38+13:00 (3 years ago)
Author:
su41
Message:

Added in some error handling messages for mysql.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs2-extensions/tipple-bridge/trunk/src/src/main/java/sqlCli.java

    r34889 r34894  
    11import com.google.gson.JsonArray;
     2import com.mysql.cj.exceptions.MysqlErrorNumbers;
    23import picocli.CommandLine;
    34import picocli.CommandLine.Command;
     
    2728    //our cli arguments
    2829
    29     @Option(names = {"-u", "--user"}, description = "The user name")
     30    @Option(names = {"-u", "--user"}, description = "The user name",
     31            required = true)
    3032    String userName;
    3133
     
    3739    echo back) but on the IDE it will echo back.
    3840     */
    39     @Option(names = {"-p", "--password"}, description = "User's password", interactive = true)
     41    @Option(names = {"-p", "--password"}, description = "User's password", interactive = true,
     42            required = true)
    4043    String password;
    4144
     
    4649    String dbName;
    4750
     51    //project id - it should be required right?
     52    //if they omit, the query would come up empty.
    4853    @Option(names = {"--projectID"}, description = "ID number of the project")
    4954    String projectID;
    5055
    5156    //defaultValue in case the user doesn't enter in an option for it
     57    //make this option so that the program can read in without typing in --filename everytime
     58    //read this option in last
    5259    @Option(names = {"--fileName"}, description = "Name of output JSON file",
    53             required = true, defaultValue = "myData.json")
     60            required = true)
    5461    String fileName;
    5562
     
    7077
    7178    private static void databaseConnect(String user, String pass, int port, String dbName,
    72                                         String projectID, String filename, String host){
     79                                        String projectID, String fileName, String host){
    7380        Connection conn;
    7481        Statement stmt;
     
    8289        // FileWriter file = new FileWriter("tippleData.json")
    8390    System.out.println("Saving output to:");
    84     System.out.println("  " + filename);
     91    System.out.println("  " + fileName);
    8592   
    86         try (FileWriter file = new FileWriter(filename)){
     93        try (FileWriter file = new FileWriter(fileName)){
    8794            //register the jdbc driver
    8895            Class.forName("com.mysql.cj.jdbc.Driver");
    8996            //opening the connection
    9097            System.out.println("Connecting to database:");
    91         System.out.println("  " + dbUrl);
     98            System.out.println("  " + dbUrl);
    9299       
    93100            conn = DriverManager.getConnection(dbUrl, user, pass);
     
    96103            stmt = conn.createStatement();
    97104            //remember to put in the projectId parameter instead of hardcoding it
     105
     106            /*
     107            if we get up to the point where we allow boundary setting, i.e  user specifies
     108            between x and y number of records
     109            could use an if statement to check
     110            if the paramter exists or not, if it does then run a statement with those x and y
     111            if not, then run a different statement without them
     112             */
     113
    98114            String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId="+projectID;
    99115/*
     
    110126            System.out.println("  Number of columns in table retrieved: " + dbColumn);
    111127
     128            //create gson object and have the file write the opening bracket for valid json
    112129            Gson myGSon = new Gson();
    113         file.write("[");
     130            file.write("[");
    114131
    115132            //does nothing, assuming the try catch picks up the error first,
     
    122139                HashMap<String, Object> dbRow = new HashMap<String, Object>(dbColumn);
    123140
    124                 //maybe use some kind of arrayList?
    125                 // ArrayList<Map<String, Object>> dbRowArray = new ArrayList<>(dbColumn);
    126 
    127141                for(int i = 1; i <= dbColumn; i++){
    128142                    /*
    129                     doing checking this way would mean we have to have a case
     143                    doing checking this way could mean we have to have a case
    130144                    for every data type,
    131145                    if a data type changes in the database that's probably fine, but
     
    135149                    Without this kind of check though, the output would throw a pretty nasty
    136150                    warning about illegal reflective access. Though it would still print the
    137                     date out fine. Think it switched to using some other java option?
     151                    date out fine. The warning:
    138152
    139153                    WARNING: An illegal reflective access operation has occurred
    140 WARNING: Illegal reflective access by com.google.gson.internal.bind.ReflectiveTypeAdapterFactory (file:/home/su41/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.7/751f548c85fa49f330cecbb1875893f971b33c4e/gson-2.7.jar) to field java.time.LocalDateTime.date
    141 WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.bind.ReflectiveTypeAdapterFactory
    142 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    143 WARNING: All illegal access operations will be denied in a future release
     154                    WARNING: Illegal reflective access by com.google.gson.internal.bind.ReflectiveTypeAdapterFactory (file:/home/su41/.gradle/caches/modules-2/files-2.1/com.google.code.gson/gson/2.7/751f548c85fa49f330cecbb1875893f971b33c4e/gson-2.7.jar) to field java.time.LocalDateTime.date
     155                    WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.bind.ReflectiveTypeAdapterFactory
     156                    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
     157                    WARNING: All illegal access operations will be denied in a future release
    144158                     */
    145159                    if(resultMetaData.getColumnTypeName(i).equals("DATETIME")){
    146160                        dbRow.put(resultMetaData.getColumnName(i), myResult.getDate(i));
    147                         //System.out.println("has a datetime");
    148 
    149161                    }
    150 
    151162                    else{
    152163                        dbRow.put(resultMetaData.getColumnName(i), myResult.getObject(i));
    153                        // System.out.println("no datetime datetime");
    154164                    }
    155165                }
    156                 //JsonObject jsonObject = myGSon.toJsonTree(dbRow).getAsJsonObject();
    157 
    158                 // using a hashmap wont work with this as its not an array
    159                // JsonArray jsonArray = myGSon.toJsonTree(dbRow).getAsJsonArray();
    160166               String dbRowAsJson = myGSon.toJson(dbRow);
    161167
     
    168174
    169175                file.write(dbRowAsJson);
    170                 //file.write(jsonArray.toString());
     176
    171177                //Adding commas between each row that we write to our file, as we need it to be valid json
    172178                if(!myResult.isLast()){
     
    183189            stmt.close();
    184190            conn.close();
     191            System.out.println("Connection closed");
     192        }
     193        catch(SQLException sqlError){
     194            System.out.println("MySQL error code: " + sqlError.getErrorCode());
     195            System.out.println(sqlError.getMessage());
     196            //System.out.println(MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR);
     197           if(sqlError.getErrorCode() == MysqlErrorNumbers.ER_ACCESS_DENIED_ERROR){
     198               System.out.println("Error with database credentials. Double check your: ");
     199               System.out.println("    -'Username'");
     200               System.out.println("    -'Password' ");
     201               System.out.println("    -'Database name' ");
     202            }
     203            sqlError.printStackTrace();
     204
    185205        }
    186206        catch(Exception e){
     
    189209            //print out wrong username or password instead of the stack trace?
    190210            System.out.println("encountered error");
     211
    191212        }
    192213    }
Note: See TracChangeset for help on using the changeset viewer.