-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.java
More file actions
37 lines (23 loc) · 898 Bytes
/
Copy pathDatabaseConnection.java
File metadata and controls
37 lines (23 loc) · 898 Bytes
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3307/trackify";
String user = "root";
String password = "";
try {
Connection conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO expenses(amount, category, expense_date) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setDouble(1, 500);
pstmt.setString(2, "Food");
pstmt.setString(3, "2026-05-22");
pstmt.executeUpdate();
System.out.println("Expense inserted successfully!");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}