import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseExample {
public static void main(String[] args) {
// Connection details
String url = "jdbc:regatta:aaa.bbb.ccc.ddd:ppp";
String user = "MyUserName";
String password = "SomeSophisticatedPassword";
try {
// Establish the connection to the database
try (Connection conn = DriverManager.getConnection(url, user, password)) {
// Create a statement to execute SQL queries
try (Statement stmt = conn.createStatement()) {
// Create table
String createTableSQL = "CREATE TABLE employees (
employee_key INT PRIMARY KEY INDEX WITH (devices = (m10d1)),
employee_name VARCHAR(40) NOT NULL,
employee_salary INT,
employee_department VARCHAR(50) NOT NULL
) WITH (devices = (m10d1))";
stmt.executeUpdate(createTableSQL);
// Insert values
String insertValuesSQL = "INSERT INTO employees (
employee_key,
employee_name,
employee_salary,
employee_department)
VALUES (1,'John Doe', 10932, 'DevOps'),
(2,'Richard Roe', 18324, 'Legal'),
(3,'Jane Roe', 20411, 'SoftwareDev'),
(4,'Rachel Roe', 19555, 'Support')";
stmt.executeUpdate(insertValuesSQL);
// Fetch data
String fetchSQL = "SELECT * FROM employees";
ResultSet rs = stmt.executeQuery(fetchSQL);
// Print the results
while (rs.next()) {
// Retrieve data from each row
int id = rs.getInt("employee_key");
String name = rs.getString("employee_name");
int salary = rs.getInt("employee_salary");
String department = rs.getString("employee_department");
// Print the data
System.out.println("Employee Key: " + id);
System.out.println("Employee Name: " + name);
System.out.println("Employee Salary: " + salary);
System.out.println(
"Employee Department: " + department);
System.out.println();
}
}
}
} catch (SQLException e) {
System.out.println("Database access error.");
e.printStackTrace();
}
}
}