public class SQLiteJDBCDriverConnection {
Этот блок предназначен для подключения к базе данных sqlite и создания таблицы «склады» с тремя столбцами.
public static Connection connect() {
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:chinook.db";
String sql = "CREATE TABLE IF NOT EXISTS warehouses (\n"
+ " id integer PRIMARY KEY,\n"
+ " name text NOT NULL,\n"
+ " capacity real\n"
+ ")";
// create a connection to the database
conn = DriverManager.getConnection(url);
//Create table
Statement state = conn.createStatement();
state.executeUpdate(sql);
System.out.println("Connection to SQLite has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
return conn;
}
Этот блок предназначен для создания объекта с тремя параметрами для вставки новых записей для трех столбцов.
public static void newItem(int id, String name, int capacity) {
Connection con = connect();
PreparedStatement state;
String sql = "INSERT INTO warehouses(id,name,capacity) VALUES(?,?,?)";
try {
state = con.prepareStatement(sql);
state.executeUpdate();
}catch(Exception e) {
}
}
Этот блок выполняет функцию newItem.
public static void main(String[] args) {
newItem(4009,"plywood",5000);
}
}