Cook book written in Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

170 lines
5.3 KiB

package db;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import db.Recipe;
public class Cookbook {
public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:cookbook.db";
private Connection conn;
private Statement stat;
public Cookbook() {
try {
Class.forName(Cookbook.DRIVER);
} catch (ClassNotFoundException e) {
System.err.println("Brak sterownika JDBC");
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DB_URL);
stat = conn.createStatement();
} catch (SQLException e) {
System.err.println("Problem z otwarciem polaczenia");
e.printStackTrace();
}
createTables();
}
public boolean createTables() {
String createRecipes = "CREATE TABLE IF NOT EXISTS recipes (id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar, ingredients varchar, description varchar, tags varchar, img blob)";
try {
stat.execute(createRecipes);
} catch (SQLException e) {
System.err.println("Blad przy tworzeniu tabeli");
e.printStackTrace();
return false;
}
return true;
}
/* funkcja przestarzała (deprecated) - do usunięcia w bliskiej przyszłości - PioDer */
public boolean insertRecipe(String name, String ingredients, String description, String tags, ImageIcon img) {
try {
BufferedImage bImg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
bImg.getGraphics().drawImage(img.getImage(), 0,0, null);
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
ImageIO.write( bImg, "png", bStream );
bStream.flush();
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
prepStmt.setString(1, name);
prepStmt.setString(2, ingredients);
prepStmt.setString(3, description);
prepStmt.setString(4, tags);
prepStmt.setBytes(5, bStream.toByteArray());
prepStmt.execute();
bStream.close();
}
catch (SQLException e) {
System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace();
return false;
}
catch (IOException e) {
System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace();
return false;
}
return true;
}
public boolean insertRecipe(Recipe ob) {
try {
PreparedStatement prepStmt = conn.prepareStatement(
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);");
prepStmt.setString(1, ob.getName());
prepStmt.setString(2, ob.getIngredients());
prepStmt.setString(3, ob.getDescription());
prepStmt.setString(4, ob.getTags());
prepStmt.setBytes(5, ob.getImageArr());
prepStmt.execute();
}
catch (SQLException e) {
System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace();
return false;
}
return true;
}
public Recipe selectRecipe(String name) {
Recipe selectedRecipe;
try {
PreparedStatement query = conn.prepareStatement("SELECT * FROM recipes WHERE name=?");
query.setString(1, name);
ResultSet result = query.executeQuery();
int id;
String ingredients, description, tags;
ImageIcon pict;
id = result.getInt("id");
ingredients = result.getString("ingredients");
description = result.getString("description");
tags = result.getString("tags");
pict = new ImageIcon(result.getBytes("img"));
selectedRecipe = new Recipe(id, name, ingredients, description, tags, pict);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return selectedRecipe;
}
public boolean deleteRecipe(String name){
try {
PreparedStatement query = conn.prepareStatement("DELETE FROM recipes WHERE name=?");
query.setString(1, name);
query.execute();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
public List<String> selectNames(){
List<String> names = new LinkedList<String>();
try {
ResultSet result = stat.executeQuery("SELECT name FROM recipes");
String name;
while(result.next()){
name = result.getString("name");
names.add(name);
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return names;
}
public void closeConnection() {
try {
conn.close();
} catch (SQLException e) {
System.err.println("Problem z zamknieciem polaczenia");
e.printStackTrace();
}
}
}