Browse Source

Dodanie klas do obsługi sqlite

master
Grzegorz 10 years ago
parent
commit
91b6743ea6
2 changed files with 148 additions and 0 deletions
  1. +96
    -0
      src/main/java/db/Cookbook.java
  2. +52
    -0
      src/main/java/db/Recipe.java

+ 96
- 0
src/main/java/db/Cookbook.java View File

@ -0,0 +1,96 @@
package db;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import db.Recipe;
public class Cookbook {
public static final String DRIVER = "org.sqlite.JDBC";
public static final String DB_URL = "jdbc:sqlite:cookbook";
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)";
try {
stat.execute(createRecipes);
} catch (SQLException e) {
System.err.println("Blad przy tworzeniu tabeli");
e.printStackTrace();
return false;
}
return true;
}
public boolean insertRecipe(String name, String ingredients, String description, String tags) {
try {
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.execute();
} catch (SQLException e) {
System.err.println("Blad przy wstawianiu przepisu");
e.printStackTrace();
return false;
}
return true;
}
public List<Recipe> selectRecipes() {
List<Recipe> recipes = new LinkedList<Recipe>();
try {
ResultSet result = stat.executeQuery("SELECT * FROM recipes");
int id;
String name, ingredients, description, tags;
while(result.next()) {
id = result.getInt("id");
name = result.getString("name");
ingredients = result.getString("ingredients");
description = result.getString("description");
tags = result.getString("tags");
recipes.add(new Recipe(id, name, ingredients, description, tags));
}
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return recipes;
}
public void closeConnection() {
try {
conn.close();
} catch (SQLException e) {
System.err.println("Problem z zamknieciem polaczenia");
e.printStackTrace();
}
}
}

+ 52
- 0
src/main/java/db/Recipe.java View File

@ -0,0 +1,52 @@
package db;
public class Recipe {
private int id;
private String name;
private String ingredients;
private String description;
private String tags;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setImie(String name) {
this.name = name;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public Recipe() {}
public Recipe(int id, String name, String ingredients, String description, String tags) {
this.id = id;
this.name = name;
this.ingredients = ingredients;
this.description = description;
this.tags = tags;
}
}

Loading…
Cancel
Save