This Maven project shows how to run SQL statements to perform CRUD (Create, Read, Update, Delete) operations.
Create (INSERT
), update (UPDATE
), and delete (DELETE
):
try (PreparedStatement statement = connection.prepareStatement("""
INSERT INTO table_name(column1, column2)
VALUES (?, ?)
""")) {
statement.setString(1, someString);
statement.setInt(2, someInteger);
int rowsInserted = statement.executeUpdate();
}
Read (SELECT
):
try (PreparedStatement statement = connection.prepareStatement("""
SELECT column1, column2
FROM table_name
""")) {
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String val1 = resultSet.getString(1); // by column index
int val2 = resultSet.getInt("column2"); // by column name
// ... use val1 and val2 ...
}
}
- Java 21 or later. Previous versions should work (update the version in the pom.xml file).
- Apache Maven.
- MariaDB server.
- An SQL client tool like
mariadb
, DBeaver, or an SQL integration for your IDE.
See the instructions here.
Run the following from the command line:
git clone git@github.com:mariadb-developers/java-quickstart.git
cd java-quickstart/jdbc/part2/
mvn package
java -jar java -jar target/jdbc-demo-1.0-SNAPSHOT.jar
You should see the output in the terminal.
You can also connect to the database and see the data in the programming_language
table:
mariadb-shell --dsn mariadb://user:'Password123!'@127.0.0.1
Run the following query:
SELECT * FROM demo.programming_languages;
Read the tutorial or watch the video for detailed steps on how to implement this example from scratch: