DriverManager:
скачать драйвер JDBC
поместите его в свой проект JAVA
определить путь к Водителю фляги в свойствах проекта
Пример подключения:
public static void exampleOfConnection() {
try {
/* How to instanciate a connection with a specific driver manager */
Class.forName("org.sqlite.JDBC");
Connection connectionDB = DriverManager.getConnection("jdbc:sqlite:~/workspace/POEC/Data/films.sqlite");
connectionDB.setAutoCommit(false);
/* Begin of statements */
/* Here are use statements */
/* End of statements*/
connectionDB.commit();
connectionDB.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
Пример выражения:
public static void exampleOfStatementTest(Connection conn) throws SQLException {
String createFilmsTableQuery = "CREATE TABLE IF NOT EXISTS films" + "("
+ " filmId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ," + " filmTitle TEXT,"
+ " filmDirector TEXT," + " filmType TEXT," + " filmYear INTEGER " + ");";
try (Statement statement = conn.createStatement()) {
statement.execute(createFilmsTableQuery);
statement.close();
}
}
Пример PrepareStatement :
private static void exampleOfPrepareStatementTest(Connection conn) throws SQLException {
String insertIntoFilmsPrepareStatementQuery = "INSERT INTO films "
+ "(filmTitle, filmDirector, filmType, filmyear) " + "VALUES (? ,? ,? ,? )";
try (Statement st = conn.createStatement()) {
PreparedStatement preparedStatement = conn.prepareStatement(insertIntoFilmsPrepareStatementQuery);
preparedStatement.setString(1, "Jackie Brown");
preparedStatement.setString(2, "Tarentino");
preparedStatement.setString(3, "detective film");
preparedStatement.setInt(4, 1997);
int filmAddedQuantity = preparedStatement.executeUpdate();
System.out.println(filmAddedQuantity + " Film(s) added");
preparedStatement.close();
st.close();
}
}