-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.java
More file actions
51 lines (39 loc) · 1.35 KB
/
Log.java
File metadata and controls
51 lines (39 loc) · 1.35 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//displays the users winning details
public class Log extends TravelGame {
public static void userStats() {
String nameText = name.getText();
if (name.getText().contentEquals("Type Your Name"))
nameText = "Winner";
String moneyText = moneyField.getText();
String timeText = timeField.getText();
// Create a database connection
String dbUrl = "jdbc:mysql://localhost:3306/travelgame";
String user = "root";
String pass = "root";
try {
// 1. Get a connection to database
Connection myConn = DriverManager.getConnection(dbUrl, user, pass);
// 2.Create a statement
Statement myStmt = myConn.createStatement();
// 3. Execute SQL query
String sql = String.format("insert into winners"
+ " values ('%s', '%s', '%s')", nameText, moneyText,
timeText);
myStmt.executeUpdate(sql);
// 4. Print winners from database.
ResultSet myRs = myStmt.executeQuery("select * from winners");
System.out.println("---------------WINNERS---------------");
while (myRs.next()) {
System.out.println(myRs.getString("name") + "\t"
+ myRs.getString("moneyleft") + "\t"
+ myRs.getString("timeleft"));
}
} catch (Exception e) {
System.out.println("Unable to coonnect to database.");
}
}
}