-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertDataExample.java
More file actions
41 lines (34 loc) · 1.24 KB
/
InsertDataExample.java
File metadata and controls
41 lines (34 loc) · 1.24 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
package org.example;
import java.sql.*;
public class InsertDataExample {
public static void main(String[] args) {
// Database connection parameters
String url = "jdbc:mysql://localhost:3306/HotelBookingDB";
String username = "root";
String password = "DATA_VER_2014";
// JDBC variables
Connection conn = null;
Statement stmt = null;
try {
// Connect to the database
conn = DriverManager.getConnection(url, username, password);
// Create a statement
stmt = conn.createStatement();
// Execute query to insert data
String sqlQuery = "INSERT INTO User (userID, name, email) VALUES " +
"(UUID(), 'Jack Sparrow', 'sparrow@gmail.com')";
int rowsAffected = stmt.executeUpdate(sqlQuery);
System.out.println("Rows affected: " + rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close JDBC objects
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}