TTT_MySQL.java (5643B)
1 /* 2 Implements access to the MySQL database "tictactoe" 3 */ 4 package com.tttws; 5 6 import static java.lang.System.exit; 7 import java.sql.Connection; 8 import java.sql.DriverManager; 9 import java.sql.PreparedStatement; 10 import java.sql.ResultSet; 11 import java.sql.SQLException; 12 import java.sql.Statement; 13 14 /** 15 * 16 * @author petar 17 */ 18 public class TTT_MySQL { 19 20 private Connection connect = null; 21 private Statement statement = null; 22 private PreparedStatement preparedStatement = null; 23 private ResultSet resultSet = null; 24 25 /** 26 * Parameterized constructor to connect to any MySQL DB on any MySQL DBMS. 27 * 28 * @param host where is the DBMS? 29 * @param username who are you? 30 * @param password what is your password? 31 * @param database which database do you wish to use? 32 */ 33 public TTT_MySQL(String host, String username, String password, String database) { 34 String url = ""; 35 url = "jdbc:mysql://" + host + "/" + database; 36 try { 37 Class.forName("com.mysql.cj.jdbc.Driver"); 38 connect = DriverManager.getConnection(url, username, password); 39 } catch (SQLException | ClassNotFoundException e) { 40 exit(-1); 41 } 42 } 43 44 /** 45 * Used to retrieve information from a table. 46 * 47 * @param SQLCmd The SELECT query you wish to run. 48 * @return The result of the query encoded as a comma separated list of 49 * fields with a new line for each row. 50 */ 51 public String retrieve(String SQLCmd) { 52 String result = ""; 53 try { 54 statement = connect.createStatement(); 55 resultSet = statement.executeQuery(SQLCmd); 56 result = writeResultSet(resultSet); 57 } catch (Exception e) { 58 return "ERROR"; 59 } 60 return result; 61 } 62 63 /** 64 * Used to update information in a table. 65 * 66 * @param SQLCmd The UPDATE command you wish to run. 67 * @return either how many rows were updated or the text string "ERROR". 68 */ 69 public String update(String SQLCmd) { 70 try { 71 preparedStatement = connect.prepareStatement(SQLCmd); 72 int rows = preparedStatement.executeUpdate(); 73 return "UPDATED " + rows + " ROWS."; 74 } catch (Exception e) { 75 return "ERROR"; 76 } 77 } 78 79 /** 80 * Used to insert information into a table. 81 * 82 * @param SQLCmd The INSERT query you wish to run. 83 * @return either how many rows were inserted or the text string "ERROR". 84 */ 85 public String insert(String SQLCmd) { 86 try { 87 preparedStatement = connect.prepareStatement(SQLCmd); 88 int rows = preparedStatement.executeUpdate(); 89 return "INSERTED " + rows + " ROWS."; 90 } catch (Exception e) { 91 return "ERROR"; 92 } 93 } 94 95 /** 96 * Used to remove data from a table. 97 * 98 * @param SQLCmd The DELETE query you wish to run. 99 * @return either how many rows were removed or the text string "ERROR". 100 */ 101 public String remove(String SQLCmd) { 102 try { 103 preparedStatement = connect.prepareStatement(SQLCmd); 104 int rows = preparedStatement.executeUpdate(); 105 return "REMOVED " + rows + " ROWS."; 106 } catch (Exception e) { 107 return "ERROR"; 108 } 109 } 110 111 /** 112 * Used to get the fields names of a resultset. 113 * 114 * @param resultSet the resultset you wish to get the field names of. 115 * @return the metadata of the resultset. 116 * @throws SQLException 117 */ 118 private String writeMetaData(ResultSet resultSet) throws SQLException { 119 String result = ""; 120 result += "The columns in the table are: "; 121 result += "Table: " + resultSet.getMetaData().getTableName(1); 122 for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) { 123 result += "Column " + i + " " + resultSet.getMetaData().getColumnName(i); 124 } 125 return result; 126 } 127 128 /** 129 * Used to turn a resultset into a digestible form. 130 * 131 * @param resultSet the resultset you wish to consume. 132 * @return the resultset encoded as fields separated by commas and rows 133 * separated by the new line character (\n). 134 * @throws SQLException 135 */ 136 private String writeResultSet(ResultSet resultSet) throws SQLException { 137 String result = ""; 138 /*switch(query) { 139 case "login": 140 while(resultSet.next()) { 141 int userID = resultSet.getInt("autokey"); 142 result += userID + "\n"; 143 } 144 break; 145 146 } */ 147 int cols = resultSet.getMetaData().getColumnCount(); 148 int rows = 0; 149 while (resultSet.next()) { 150 if (rows > 0) { 151 result += "\n"; 152 } 153 for (int i = 1; i <= cols; i++) { 154 result += resultSet.getObject(i).toString(); 155 result += ","; 156 } 157 int len = result.length(); 158 result = result.substring(0, (len - 1)); 159 rows++; 160 } 161 return result; 162 } 163 164 /** 165 * Used to close all objects used to talk with the DB and DBMS. 166 */ 167 public void close() { 168 try { 169 if (resultSet != null) { 170 resultSet.close(); 171 } 172 173 if (statement != null) { 174 statement.close(); 175 } 176 177 if (connect != null) { 178 connect.close(); 179 } 180 } catch (SQLException e) { 181 System.out.println("Exception: " + e.getMessage()); 182 } 183 } 184 }