<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.47.1.0</version>
</dependency>
</dependencies>
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class AuthorDatabaseHelper {
public static String TABLE_NAME = "authors";
private Connection connection;
public AuthorDatabaseHelper(Connection connection) {
this.connection = connection;
}
public ArrayList<Author> getAuthors() throws SQLException {
ArrayList<Author> authors = new ArrayList<>();
Statement statement = this.connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM " + TABLE_NAME);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String fullName = resultSet.getString("full_name");
authors.add(new Author(id, fullName));
}
statement.close();
return authors;
}
public void addAuthor(String fullName) throws SQLException {
Statement statement = this.connection.createStatement();
statement.execute("INSERT INTO " + TABLE_NAME + " (full_name) VALUES ('" + fullName +"')");
statement.close();
}
public void deleteAuthor(int id) throws SQLException {
Statement statement = this.connection.createStatement();
statement.execute("DELETE FROM " + TABLE_NAME + " WHERE id = " + id);
statement.close();
}
public Author getAuthor(int id) throws SQLException {
Statement statement = this.connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM " + TABLE_NAME + " WHERE id = " + id);
if (resultSet.next()){
int authorId = resultSet.getInt("id");
String fullName = resultSet.getString("full_name");
statement.close();
return new Author(authorId, fullName);
}
statement.close();
return null;
}
public void update(int id, String fullName) throws SQLException {
Statement statement = this.connection.createStatement();
statement.execute("UPDATE " + TABLE_NAME + " SET full_name = '" + fullName + "' WHERE id = " + id);
statement.close();
}
}
public class Book {
private int id;
private String title;
private int year;
private int count;
private int authorId;
public Book(int id, String title, int year, int count, int authorId) {
this.id = id;
this.title = title;
this.year = year;
this.count = count;
this.authorId = authorId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
}
public class Author {
private int id;
private String fullName;
public Author(int id, String fullName) {
this.id = id;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@Override
public String toString() {
return "Author{" +
"id=" + id +
", fullName='" + fullName + '\'' +
'}';
}
}
import java.sql.*;
import java.util.ArrayList;
public class Main {
public static final String DB_URL = "jdbc:sqlite:C:/Users/Admin/Desktop/mydatabase.db";
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection(DB_URL);
AuthorDatabaseHelper authorDatabaseHelper = new AuthorDatabaseHelper(connection);
System.out.println(authorDatabaseHelper.getAuthors());
authorDatabaseHelper.addAuthor("Никита!!!");
ArrayList<Author> authors = authorDatabaseHelper.getAuthors();
System.out.println(authors);
int maxId = 0;
for(Author author : authors){
if (maxId > author.getId()) maxId = author.getId();
}
authorDatabaseHelper.deleteAuthor(maxId);
}
}