MySQL Class für Java Anwendungen
Die MySQL Verbindungsklasse steuert in all meinen Java Programmen die was mit MySQL zu tun haben die Verbindung mit dem MySQL Server. Sie Arbeitet mit dem JDBC Treiber, der auch eingebunden werden muss, damit die Klasse richtig funktioniert.
Die Klasse mit samt einer Beschreibung was welche Methode tut, kannst du dir hier anschauen.
Beispielaufruf
Hier ist mal ein Beispielaufruf der Klasse.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | MySQL_Class sql = new MySQL_Class(); try { sql.mysqlConnect("user", "password", "localhost", "3306"); Object[][] result = sql.mysqlArray("select * from table"); for(int i=0; i<result.length; i ) { for(int j=0; j<result[i].length; j ) { System.out.println(result[i][j]); } } sql.mysqlDisconnect(); } catch(SQLException e) { System.out.println(e.toString()); } |
In der ersten Zeile wird die Klasse initialisiert.
In Zeile 3 wird Verbindung zum MySQL Server aufgebaut. Der Methode muss dabei Username, Password, den Hostnamen und den Port übergeben werden. Danach geht es in der darauf folgenden Zeile mit der Methode mysqlArray() weiter. Diese liefert das Ergebnis eines SQL Statements als zweidimensionales Array.
Als nächstes wird das Array mit zwei for-Schleifen ausgelesen. Zuletzt wird in Zeile 10 noch die Verbindung zum SQL Server wieder getrennt.
Das ganze Spielt sich in einem try-catch Block ab, um eventuelle MySQL Fehler abzufangen und auszugeben.
Die komplette Klasse
Hier nun noch mal der Code der kompletten Klasse.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JTable; /** * MySQL Verbindungsklasse * @author Christian Blechert * @since 2008 */ public class MySQL_Class { // Attribute fuer externe Klassen private Connection connection = null; private Statement statement = null; private ResultSet result = null; private ResultSetMetaData meta = null; /** * Verbindung zum MySQL Server aufbauen * @param user Benutzername * @param pass Password zum Benutzernamen * @param host Name/IP Adresse des MySQL Servers * @param port Port auf den der Server horcht. Default: 3306 * @throws java.sql.SQLException */ public void mysqlConnect(String user, String pass, String host, String port) throws Exception { if(this.connection!=null) this.mysqlDisconnect(); String conn = "jdbc:mysql://" host ":" port; Class.forName("com.mysql.jdbc.Driver").newInstance(); this.connection = DriverManager.getConnection(conn, user, pass); this.statement = this.connection.createStatement(); } /** * Trennt die Verbindung * Gibt bei Erfolg true zurueck * @throws java.sql.SQLException */ public boolean mysqlDisconnect() throws SQLException { if(this.connection!=null) { if(this.result!=null) { this.result.close(); this.result=null; } if(this.statement!=null) { this.statement.close(); this.statement=null; } this.connection.close(); this.connection=null; return true; } return false; } /** * Auswaehlen einer Datenbank * @param db Datenbankname * @throws java.sql.SQLException */ public void mysqlSelectDB(String db) throws SQLException { this.statement.execute("use `" db "`"); } /** * Absenden eines MySQL Statements * @param query SQL Statement * @return Das ResultSet zu dem Statement * @throws java.sql.SQLException */ public ResultSet mysqlQuery(String query) throws SQLException { this.result = this.statement.executeQuery(query); this.meta = this.result.getMetaData(); return this.result; } /** * Absenden eines MySQL Updates * Zum veraendern und neu Erstellen von Daten * @param query MySQL Statement * @throws java.sql.SQLException */ public void mysqlUpdate(String query) throws SQLException { this.statement.executeUpdate(query); } /** * Liefert das Result eines MySQL Statements als Array zurueck * @param query MySQL Statement * @return Result als Array * @throws java.sql.SQLException */ public Object[][] mysqlArray(String query) throws SQLException { ResultSet res = this.mysqlQuery(query); int i=0; Object[][] result; try { result = new String[this.mysqlGetRows()][this.mysqlGetFields()]; } catch(Exception e) { return null; } while(res.next()) { if(Runtime.getRuntime().freeMemory()<1000000) { res.close(); res = null; return null; } for(int j=0; j<this.mysqlGetFields(); j ) { result[i][j] = res.getString(j 1); } i ; } return result; } /** * Daten aus einem JTable bei veraenderung in eine MySQL Datenbank schreiben * @param newValue neuer Wert der veraenderten Zelle * @param oldValue alter Wert der veranderten Zelle * @param x X-Koordinate der Zelle * @param y Y-Koordinate der Zelle * @param object Object des JTable * @throws java.sql.SQLException */ public void updateJTable(Object newValue, Object oldValue, int x, int y, JTable object) throws SQLException { if(!newValue.equals(oldValue)) { String[] fn = this.mysqlGetFN(); String query = "update `" this.mysqlGetTable(y 1) "` set `" fn[y] "` = '" newValue.toString() "' where"; for(int i=0; i<this.mysqlGetFields(); i ) { if(i>0) query = " and"; if(i==y) { if(oldValue==null) { query = " `" fn[i] "` is NULL"; } else { query = " `" fn[i] "`='" oldValue "'"; } } else { if(object.getValueAt(x, i)==null) { query = " `" fn[i] "` is NULL"; } else { query = " `" fn[i] "`='" object.getValueAt(x, i) "'"; } } } this.mysqlUpdate(query); } } /** * Verfuegbare MySQL Datenbanken als Array auslesen * @return Array mit den Datenbanknamen * @throws java.sql.SQLException */ public String[] mysqlGetDBs() throws SQLException { this.mysqlQuery("show databases"); String[] Array = new String[this.mysqlGetRows()]; int i=0; while(this.result.next()) { Array[i] = this.result.getString(1); i ; } return Array; } /** * Liefert alle Tabellen der aktuell ausgewaehlten Datenbank * @return Array mit den Tabellennamen * @throws java.sql.SQLException */ public String[] mysqlGetTables() throws SQLException { this.mysqlQuery("show tables"); String[] Array = new String[this.mysqlGetRows()]; int i=0; while(this.result.next()) { Array[i] = this.result.getString(1); i ; } return Array; } /** * Liefert den Tabellennamen einer Spalte aus dem aktuellen ResultSet * @param column Spaltennummer * @return Tabellennamen * @throws java.sql.SQLException */ public String mysqlGetTable(int column) throws SQLException { return this.meta.getTableName(column); } /** * Liefert den Benutzernamen mit dem man am MySQL Server angemeldet ist * @return Benutzername * @throws java.sql.SQLException */ public String mysqlGetUser() throws SQLException { String user = this.connection.getMetaData().getUserName(); user = user.substring(0, user.indexOf("@")); return user; } /** * Liefert den Hostnamen des MySQL Servers * @return Hostname * @throws java.sql.SQLException */ public String mysqlGetHost() throws SQLException { String host = this.connection.getMetaData().getURL(); host = host.substring(host.indexOf("//") 2); return host; } /** * Liefert alle Feldnamen des Aktuellen ResultSets * @return Array mit den Feldnamen * @throws java.sql.SQLException */ public String[] mysqlGetFN() throws SQLException { String[] fns = new String[this.mysqlGetFields()]; for(int i=0; i<this.mysqlGetFields(); i ) { fns[i] = this.meta.getColumnName(i 1); } return fns; } /** * Liefert die Feldnamen der angegebenen Tabelle * @param table Name der Tabelle * @return Array mit den Feldnamen * @throws java.sql.SQLException */ public String[] mysqlGetFNfrom(String table) throws SQLException { this.mysqlQuery("desc `" table "`"); String[] Array = new String[this.mysqlGetRows()]; int i=0; while(this.result.next()) { Array[i] = this.result.getString(1); i ; } return Array; } /** * Liefert naechsten Datensatz * @param col Spalte * @return Inhalt der Spalte als String * @throws java.sql.SQLException */ public String mysqlGetNext(int col) throws SQLException { this.result.next(); return this.result.getString(col); } /** * Anzahl der Datensaetze des ResultSets * @return Anzahl der Datensaetze * @throws java.sql.SQLException */ public int mysqlGetRows() throws SQLException { this.result.last(); int rows = this.result.getRow(); this.result.beforeFirst(); return rows; } /** * Anzahl der Spalten des ResultSets * @return Anzahl der Spalten * @throws java.sql.SQLException */ public int mysqlGetFields() throws SQLException { return this.meta.getColumnCount(); } /** * Gibt das aktuelle ResultSet zurueck * @return ResultSet */ public ResultSet mysqlGetResult() { return this.result; } /** * Liefert die MetaData des aktuellen ResultSets zurueck * @return ResultSetMetaData */ public ResultSetMetaData mysqlGetMeta() { return this.meta; } /** * Setzt ein neues ResultSet * @param res ResultSet * @throws java.sql.SQLException */ public void mysqlSetResult(ResultSet res) throws SQLException { this.result = res; this.meta = res.getMetaData(); } } |




