|
|
@ -54,18 +54,8 @@ public class Cookbook { |
|
|
|
} |
|
|
|
|
|
|
|
/* 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 { |
|
|
|
//przekształcam ImageIcon -> BuforowanyImg |
|
|
|
BufferedImage bImg = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_ARGB); |
|
|
|
|
|
|
|
bImg.getGraphics().drawImage(img.getImage(), 0,0, null); |
|
|
|
//tworzę strumień bajtowy |
|
|
|
ByteArrayOutputStream bStream = new ByteArrayOutputStream(); |
|
|
|
//zapisuję do strumienia bufor obrazka |
|
|
|
ImageIO.write( bImg, "png", bStream ); |
|
|
|
bStream.flush(); |
|
|
|
|
|
|
|
public boolean insertRecipe(String name, String ingredients, String description, String tags, byte[] imgArr) { |
|
|
|
try { |
|
|
|
|
|
|
|
PreparedStatement prepStmt = conn.prepareStatement( |
|
|
|
"INSERT INTO recipes VALUES (NULL, ?, ?, ?, ?, ?);"); |
|
|
@ -73,20 +63,14 @@ public class Cookbook { |
|
|
|
prepStmt.setString(2, ingredients); |
|
|
|
prepStmt.setString(3, description); |
|
|
|
prepStmt.setString(4, tags); |
|
|
|
prepStmt.setBytes(5, bStream.toByteArray()); |
|
|
|
prepStmt.setBytes(5, imgArr); |
|
|
|
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; |
|
|
|
} |
|
|
@ -147,7 +131,7 @@ public class Cookbook { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public List<String> selectNames(){ |
|
|
|
public List<String> obtainNames(){ |
|
|
|
List<String> names = new LinkedList<String>(); |
|
|
|
try { |
|
|
|
ResultSet result = stat.executeQuery("SELECT name FROM recipes"); |
|
|
@ -163,6 +147,39 @@ public class Cookbook { |
|
|
|
return names; |
|
|
|
} |
|
|
|
|
|
|
|
public List<String> obtainSearchedNames(String n, String t){ |
|
|
|
List<String> names = new LinkedList<String>(); |
|
|
|
try { |
|
|
|
String whereClause = ""; |
|
|
|
|
|
|
|
if (n != "") |
|
|
|
whereClause += "name LIKE ?"; |
|
|
|
|
|
|
|
if (t != "") |
|
|
|
{ |
|
|
|
if (n != "") |
|
|
|
whereClause += " OR "; |
|
|
|
whereClause += "tags LIKE ?"; |
|
|
|
} |
|
|
|
|
|
|
|
PreparedStatement query = conn.prepareStatement("SELECT name FROM recipes WHERE " + whereClause); |
|
|
|
if (n != "") |
|
|
|
query.setString(1, "%"+n+"%"); |
|
|
|
if (t != "") |
|
|
|
query.setString(((n!="")? 2 : 1), "%"+t+"%"); |
|
|
|
ResultSet result = query.executeQuery(); |
|
|
|
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(); |
|
|
|