In this example, we'll use H2 Database to execute the 10 most used SQL commands from within a Java application. We will use java.sql package to interact with the database.
- First, add the H2 Dependency to your project. If you're using Maven, add the following dependency to your
pom.xml file:
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
</dependencies>
- Create a utility class called
DatabaseUtil for running SQL commands:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseUtil {
private static final String CONNECTION_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
public static Connection connect() throws SQLException {
return DriverManager.getConnection(CONNECTION_URL);
}
}
- Create the
Main class with examples of the 10 most used SQL commands:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
try (Connection connection = DatabaseUtil.connect()) {
// 1. CREATE TABLE
String createTableCommand = "CREATE TABLE employee (" +
"id INT AUTO_INCREMENT PRIMARY KEY," +
"name VARCHAR(255)," +
"position VARCHAR(255))";
executeUpdate(connection, createTableCommand);
// 2. INSERT
String insertCommand = "INSERT INTO employee (name, position) VALUES ('John Doe', 'Software Engineer')";
executeUpdate(connection, insertCommand);
// 3. SELECT
String selectCommand = "SELECT * FROM employee";
ResultSet resultSet = executeQuery(connection, selectCommand);
System.out.println("Employee records:");
while (resultSet.next()) {
System.out.printf("%d, %s, %s%n", resultSet.getInt("id"), resultSet.getString("name"),
resultSet.getString("position"));
}
// 4. UPDATE
String updateCommand = "UPDATE employee SET position = 'Senior Software Engineer' WHERE id = 1";
executeUpdate(connection, updateCommand);
// 5. DELETE
String deleteCommand = "DELETE FROM employee WHERE id = 1";
executeUpdate(connection, deleteCommand);
// 6. ALTER TABLE (Add new column)
String alterTableCommand = "ALTER TABLE employee ADD COLUMN email VARCHAR(255)";
executeUpdate(connection, alterTableCommand);
// 7. DROP TABLE
String dropTableCommand = "DROP TABLE employee";
executeUpdate(connection, dropTableCommand);
// Note: The following three commands are H2-specific and might not be applicable to other databases.
// 8. CREATE ALIAS (functions)
String createAliasCommand = "CREATE ALIAS MY_CONCAT FOR \"org.h2.api.AggregateFunction$concat\";";
executeUpdate(connection, createAliasCommand);
// 9. CREATE TRIGGER
createTableCommand = "CREATE TABLE test_trigger(id INT PRIMARY KEY, value INT);";
executeUpdate(connection, createTableCommand);
String createTriggerCommand = "CREATE TRIGGER my_trigger before INSERT ON test_trigger CALL \"org.h2.samples.Trigger\";";
executeUpdate(connection, createTriggerCommand);
// 10. CREATE SEQUENCE
String createSequenceCommand = "CREATE SEQUENCE test_sequence;";
executeUpdate(connection, createSequenceCommand);
// 10. SELECT NEXT VALUE from SEQUENCE
String getNextValueFromSequenceCommand = "SELECT NEXT VALUE FOR test_sequence";
resultSet = executeQuery(connection, getNextValueFromSequenceCommand);
if (resultSet.next()) {
System.out.printf("Next value for test_sequence: %d%n", resultSet.getInt(1));
}
} catch (SQLException e) {
System.err.println("Error executing SQL commands");
e.printStackTrace();
}
}
private static void executeUpdate(Connection connection, String command) throws SQLException {
Statement statement = connection.createStatement();
statement.executeUpdate(command);
}
private static ResultSet executeQuery(Connection connection, String command) throws SQLException {
Statement statement = connection.createStatement();
return statement.executeQuery(command);
}
}
In the Main class, we execute 10 most used SQL commands:
- CREATE TABLE
- INSERT
- SELECT
- UPDATE
- DELETE
- ALTER TABLE (for adding new columns)
- DROP TABLE
- CREATE ALIAS (H2-specific, for creating a function alias)
- CREATE TRIGGER (H2-specific)
- CREATE SEQUENCE/SELECT NEXT VALUE FOR (H2-specific, creating and using sequences)
Keep in mind that the last three SQL commands in this example are H2-specific and might not work directly with other databases.
When running the Main class, it will connect to the in-memory H2 database and execute SQL commands, interacting with the database.