Создать Maven-проект.
Добавить в pom.xml зависимость на нужный драйвер:
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
</dependencies>
Добавить в проект класс App. Изменить класс App, что бы он выглядел:
import java.sql.*;
public class App {
public static void main( String[] args ) throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement command = connection.createStatement()) {
command.execute("create table token (id,name)"); // создать таблицу
}
catch (SQLException e){
System.out.println(e.getMessage());
e.getErrorCode();
}
}
}
IntelliJ IDEA будет отображать ошибку "Try-with-resources are not supported at language level 5". Надо зайти в "File \ Project Stucture ..." и на закладке "Modules" для "Languige level:" установить "7 - Diamonds, ARM, multi-catch etc." или выше.
После запуска в папке проекта (рядом с pom.xml) появится файл sample.db.
Что бы команда mvn install не выдавала ошибку "try-with-resources is not supported in -source 1.5" необходимо в pom.xml добавить:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.7</source> <!-- или выше -->
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
Добавить записи в таблицу и найти запись в таблице:
import java.sql.*;
public class App {
public static void main( String[] args ) throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
try (Connection connection = DriverManager.getConnection("jdbc:sqlite:sample.db")) {
try (PreparedStatement command = connection.prepareStatement("insert into token (id,name) values (?,?)")){ // добавить в таблицу запись
command.setLong(1, 1);
command.setString(2, "один");
command.executeUpdate();
}
try (PreparedStatement command = connection.prepareStatement("select name from token where id=?")){ // считать данные
command.setLong(1, 1);
ResultSet resultSet = command.executeQuery();
if (resultSet.next()) {
System.out.println(resultSet.getString("name"));
} else {
System.out.println("Не могу найти запись");
}
}
}
catch (SQLException e){
System.out.println(e.getMessage());
e.getErrorCode();
}
}
}
java.sql.Statement —выполнить команду;
java.sql.PreparedStatement — выполненить команду или запросс параметрами;
java.sql.CallableStatement — выполнить хранимую процедуру.
https://svn.apache.org/repos/asf/karaf/site/production/manual/latest/jdbc.html#_datasources_jdbc