-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionMYSQL.java
More file actions
178 lines (151 loc) · 7.12 KB
/
ConnectionMYSQL.java
File metadata and controls
178 lines (151 loc) · 7.12 KB
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
package javaCycle;
import java.sql.*;
import java.io.*;
import java.util.*;
//import stringModifications.StringModifications; // doesn't need the import, if included, then compilation of Fahrrad.java fails, probably as in a folder
// javac -d . -classpath /usr/share/java/mysql-connector-java-8.0.27.jar:. ConnectionMYSQL.java
public class ConnectionMYSQL {
Dictionary mysqlSettings = new Hashtable();
static final String mysqlSettingsFile = "fahrrad_mysql.params";
//static final Connection conn;
Connection conn;
String DB_URL;
{ // This code is executed before every constructor.
// fill array with dummy values
mysqlSettings.put("host", "localhost");
mysqlSettings.put("user", "yourusername");
mysqlSettings.put("password", "yourpassword");
mysqlSettings.put("db", "");
}
static final String QUERY = "SELECT Date, DayKM, DaySeconds, TotalKM, TotalSeconds FROM fahrrad_rides";
public List<String> Load_File(String filename){
File file = new File(filename);
List<String> content = new ArrayList<String>();
try (FileInputStream input = new FileInputStream(file)) {
// create an empty string builder to store string
// create an object of bufferedReader to read lines from file
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String line;
// store each line one by one until reach end of file
while ((line = br.readLine()) != null) {
// append string builder with line and with '/n' or '/r' or EOF
content.add(line);
}
//System.out.println(content); // print string builder object i.e content
}
// Catch block to handle exceptions
catch (IOException e) {
e.printStackTrace();
}
return content;
}
public void LoadSettings(String filename){
List<String> content = Load_File(filename);
List<ArrayList<String>> contentSplit = StringModifications.SplitStringTrim(content, "=");
for (int ii=0; ii<contentSplit.size(); ii++){
ArrayList<String> key_value = contentSplit.get(ii);
this.mysqlSettings.put(key_value.get(0).trim(), key_value.get(1).trim()); // trim(): remove leading and trailing whitespace
}
}
public Connection ConnectionMYSQL_aa() // was a try that didn't work
{
DB_URL = "jdbc:mysql://" + this.mysqlSettings.get("host");
if (this.mysqlSettings.get("db") != "") {DB_URL += "/" + this.mysqlSettings.get("db");}
try(Connection temp_conn = DriverManager.getConnection(DB_URL, (String)mysqlSettings.get("user"), (String)mysqlSettings.get("password"));
Statement stmt = temp_conn.createStatement();
//return temp_conn; // illegal
) {
return temp_conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public Connection InitialiseConnectionMYSQL()
{
DB_URL = "jdbc:mysql://" + this.mysqlSettings.get("host");
if (this.mysqlSettings.get("db") != "") {DB_URL += "/" + this.mysqlSettings.get("db");}
try {
Connection temp_conn = DriverManager.getConnection(DB_URL, (String)mysqlSettings.get("user"), (String)mysqlSettings.get("password"));
Statement stmt = temp_conn.createStatement();
return temp_conn;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public ResultSet MYSQLQuery(Statement stmt, String QUERY){
try (ResultSet rs = stmt.executeQuery(QUERY);) {
return rs;
} catch (SQLException e) {
System.out.println("The following command failed: "+QUERY);
System.out.println("with:");
e.printStackTrace();
return null;
}
}
public Boolean MYSQLExecute(Statement stmt, String QUERY){
// stmt.executeUpdate(QUERY);
return true;
/* // Below doesn't work:
// error: the try-with-resources resource must either be a variable declaration or an expression denoting a reference to a final or effectively final variable
final Statement stmt1 = stmt;
final String QUERY_l = QUERY;
try (stmt1.executeUpdate(QUERY_l);) {
return true;
} catch (SQLException e) {
System.out.println("The following command failed: "+QUERY);
System.out.println("with:");
e.printStackTrace();
return false;
}*/
}
public static void main (String[] args)
{
try
{
ConnectionMYSQL obj = new ConnectionMYSQL (); // initialise
obj.run (args);
}
catch (Exception e)
{
e.printStackTrace ();
}
}
public void run (String[] args) throws Exception
{
LoadSettings(mysqlSettingsFile); // will be stored in mysqlSettings and replace standard values
/*Enumeration enu = mysqlSettings.keys(); // Creating an empty enumeration to store
while (enu.hasMoreElements()) {
String key = (String)enu.nextElement(); // otherwise it would call nextElement twice when printing key and value
System.out.println( key + ":" + mysqlSettings.get(key) );
}*/
// Open a connection
//Class.forName("com.mysql.jdbc.Driver"); // depreciation warning
Class.forName("com.mysql.cj.jdbc.Driver"); // java -classpath /usr/share/java/mysql-connector-java-8.0.27.jar ConnectionMYSQL.java
// 202202020: trying to get conn back from ConnectionMYSQL();
/*Connection conn = ConnectionMYSQL_aa();
Statement stmt = conn.createStatement(); // fails, as the connection is already closed */
// 202202020: trying to get conn back from ConnectionMYSQL();
Connection conn = InitialiseConnectionMYSQL();
Statement stmt = conn.createStatement();
System.out.print("222: ");
// ConnectionMYSQL(); // this could replace the try - catch below
DB_URL = "jdbc:mysql://" + mysqlSettings.get("host");
if (mysqlSettings.get("db") != "") {DB_URL += "/" + mysqlSettings.get("db");}
try(Connection conn1 = DriverManager.getConnection(DB_URL, (String)mysqlSettings.get("user"), (String)mysqlSettings.get("password"));
Statement stmt1 = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}