-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabase.java
More file actions
65 lines (58 loc) · 2.43 KB
/
Copy pathDatabase.java
File metadata and controls
65 lines (58 loc) · 2.43 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
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class Database {
private final String url;
private final String user;
private final String password;
public Database(String propertiesPath) {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(propertiesPath)) {
props.load(fis);
} catch (IOException e) {
throw new RuntimeException("Failed to load DB properties from " + propertiesPath + ": " + e.getMessage(), e);
}
this.url = props.getProperty("db.url");
this.user = props.getProperty("db.user");
this.password = props.getProperty("db.password");
if (this.url == null || this.url.isBlank() || this.user == null || this.user.isBlank() || this.password == null) {
throw new RuntimeException("Database configuration missing. Please fill db.properties with db.url, db.user, and db.password.");
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException("MySQL JDBC Driver not found. Add mysql-connector-j to classpath.", e);
}
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public String getUrl() {
return url;
}
public String getUsername() {
return user;
}
public Integer getUserIdByEmail(String email) throws SQLException {
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT id FROM users WHERE email = ?")) {
ps.setString(1, email);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) return rs.getInt(1);
return null;
}
}
}
public void saveLocation(String email, double lat, double lon) {
String sql = "INSERT INTO locations(user_id, latitude, longitude) SELECT id, ?, ? FROM users WHERE email = ?";
try (Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setDouble(1, lat);
ps.setDouble(2, lon);
ps.setString(3, email);
ps.executeUpdate();
} catch (SQLException e) {
System.err.println("Failed to save location: " + e.getMessage());
}
}
}