Changeset 34894
- Timestamp:
- 2021-02-17T15:20:38+13:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gs2-extensions/tipple-bridge/trunk/src/src/main/java/sqlCli.java
r34889 r34894 1 1 import com.google.gson.JsonArray; 2 import com.mysql.cj.exceptions.MysqlErrorNumbers; 2 3 import picocli.CommandLine; 3 4 import picocli.CommandLine.Command; … … 27 28 //our cli arguments 28 29 29 @Option(names = {"-u", "--user"}, description = "The user name") 30 @Option(names = {"-u", "--user"}, description = "The user name", 31 required = true) 30 32 String userName; 31 33 … … 37 39 echo back) but on the IDE it will echo back. 38 40 */ 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) 40 43 String password; 41 44 … … 46 49 String dbName; 47 50 51 //project id - it should be required right? 52 //if they omit, the query would come up empty. 48 53 @Option(names = {"--projectID"}, description = "ID number of the project") 49 54 String projectID; 50 55 51 56 //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 52 59 @Option(names = {"--fileName"}, description = "Name of output JSON file", 53 required = true , defaultValue = "myData.json")60 required = true) 54 61 String fileName; 55 62 … … 70 77 71 78 private static void databaseConnect(String user, String pass, int port, String dbName, 72 String projectID, String file name, String host){79 String projectID, String fileName, String host){ 73 80 Connection conn; 74 81 Statement stmt; … … 82 89 // FileWriter file = new FileWriter("tippleData.json") 83 90 System.out.println("Saving output to:"); 84 System.out.println(" " + file name);91 System.out.println(" " + fileName); 85 92 86 try (FileWriter file = new FileWriter(file name)){93 try (FileWriter file = new FileWriter(fileName)){ 87 94 //register the jdbc driver 88 95 Class.forName("com.mysql.cj.jdbc.Driver"); 89 96 //opening the connection 90 97 System.out.println("Connecting to database:"); 91 System.out.println(" " + dbUrl);98 System.out.println(" " + dbUrl); 92 99 93 100 conn = DriverManager.getConnection(dbUrl, user, pass); … … 96 103 stmt = conn.createStatement(); 97 104 //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 98 114 String sql = "SELECT name, id, typeId, created from td_ContentItem where projectId="+projectID; 99 115 /* … … 110 126 System.out.println(" Number of columns in table retrieved: " + dbColumn); 111 127 128 //create gson object and have the file write the opening bracket for valid json 112 129 Gson myGSon = new Gson(); 113 130 file.write("["); 114 131 115 132 //does nothing, assuming the try catch picks up the error first, … … 122 139 HashMap<String, Object> dbRow = new HashMap<String, Object>(dbColumn); 123 140 124 //maybe use some kind of arrayList?125 // ArrayList<Map<String, Object>> dbRowArray = new ArrayList<>(dbColumn);126 127 141 for(int i = 1; i <= dbColumn; i++){ 128 142 /* 129 doing checking this way would mean we have to have a case143 doing checking this way could mean we have to have a case 130 144 for every data type, 131 145 if a data type changes in the database that's probably fine, but … … 135 149 Without this kind of check though, the output would throw a pretty nasty 136 150 warning about illegal reflective access. Though it would still print the 137 date out fine. Th ink it switched to using some other java option?151 date out fine. The warning: 138 152 139 153 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.date141 WARNING: Please consider reporting this to the maintainers of com.google.gson.internal.bind.ReflectiveTypeAdapterFactory142 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations143 WARNING: All illegal access operations will be denied in a future release154 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 144 158 */ 145 159 if(resultMetaData.getColumnTypeName(i).equals("DATETIME")){ 146 160 dbRow.put(resultMetaData.getColumnName(i), myResult.getDate(i)); 147 //System.out.println("has a datetime");148 149 161 } 150 151 162 else{ 152 163 dbRow.put(resultMetaData.getColumnName(i), myResult.getObject(i)); 153 // System.out.println("no datetime datetime");154 164 } 155 165 } 156 //JsonObject jsonObject = myGSon.toJsonTree(dbRow).getAsJsonObject();157 158 // using a hashmap wont work with this as its not an array159 // JsonArray jsonArray = myGSon.toJsonTree(dbRow).getAsJsonArray();160 166 String dbRowAsJson = myGSon.toJson(dbRow); 161 167 … … 168 174 169 175 file.write(dbRowAsJson); 170 //file.write(jsonArray.toString()); 176 171 177 //Adding commas between each row that we write to our file, as we need it to be valid json 172 178 if(!myResult.isLast()){ … … 183 189 stmt.close(); 184 190 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 185 205 } 186 206 catch(Exception e){ … … 189 209 //print out wrong username or password instead of the stack trace? 190 210 System.out.println("encountered error"); 211 191 212 } 192 213 }
Note:
See TracChangeset
for help on using the changeset viewer.