<dependency>
<groupId>com.squareup.sqldelight</groupId>
<artifactId>sqldelight</artifactId>
<version>1.5.1</version>
</dependency>
groovy
implementation 'com.squareup.sqldelight:sqldelight:1.5.1'
sql
-- example.sq
CREATE TABLE User (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
);
INSERT INTO User (name, age) VALUES (?, ?);
SELECT * FROM User WHERE age > ?;
import com.example.User;
import com.squareup.sqldelight.runtime.SqlDelightQuery;
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver;
import org.sqlite.JDBC;
import java.sql.*;
public class DatabaseExample {
public static void main(String[] args) {
Connection connection = null;
try {
JdbcSqliteDriver driver = new JdbcSqliteDriver(JDBC.createContext("jdbc:sqlite:example.db"));
connection = driver.getConnection();
Statement statement = connection.createStatement();
statement.execute(User.CREATE_TABLE);
PreparedStatement insertStatement = connection.prepareStatement(User.INSERT);
insertStatement.setString(1, "John Doe");
insertStatement.setInt(2, 30);
insertStatement.execute();
PreparedStatement selectStatement = connection.prepareStatement(User.SELECT_AGE_GREATER_THAN);
selectStatement.setInt(1, 25);
ResultSet resultSet = selectStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt(User.ID);
String name = resultSet.getString(User.NAME);
int age = resultSet.getInt(User.AGE);
System.out.println("User ID: " + id + ", Name: " + name + ", Age: " + age);
}
resultSet.close();
selectStatement.close();
insertStatement.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}