Last active
May 22, 2025 14:17
-
-
Save Njoccies/a4b7e79c3b9522e492b887c3c5fc3c67 to your computer and use it in GitHub Desktop.
SQL Hilfe Noct1337
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://claude.ai/ @ und emailbestätigungscode | |
https://www.blackbox.ai/ @ und de | |
https://deepai.org/chat @ und de | |
**BEI ALLEN BUTTONS BEACHTEN DBQueries. ZU SETZEN FÜR DIE METHODEN** | |
**1. Aufgabe Blob ersetzen: Gruppe A:** | |
In das Feld user TextField geben Sie ein Suchwort ein. Mittels „LabTest" werden alle Bilder durch Zufallsbilder (Icon Converter generatePatternIcon()) ersetzt, bei denen das Suchwort im Feld Keywords enthalten ist. Ersetzten Sie dafür das Suchwort durch | |
suchwort2 = '** + suchwort+ '' und verwenden Sie , where - | |
like - " für die String-Suche. (Bereits implementiert: Die Taste showAll zeigt eine Übersicht aller Bilder der jeweiligen users an.) | |
**Für Gruppe B nur: WHERE description LIKE ? statt WHERE keywords LIKE ?** | |
public static void replaceImages(String suchwort) { | |
String suchwort2 = "%" + suchwort + "%"; | |
String query = "UPDATE Gadgets SET cover = ? WHERE keyword LIKE ?"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
// Erstelle ein Zufallsbild | |
Icon icon = Converter.generatePatternIcon(); | |
Blob blob = Converter.icon2Blob(icon, con); | |
// Setze Parameter für das PreparedStatement | |
pst.setBlob(1, blob); | |
pst.setString(2, suchwort2); | |
// Führe das Update aus | |
pst.executeUpdate(); | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler beim Ersetzen der Bilder", e); | |
} | |
} | |
**1.1 In Button einfügen** (JButton und JTextfield sind eig gegeben) | |
JButton labTestButton = new JButton("LabTest"); | |
JTextField userTextField = new JTextField(20); | |
labTestButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String suchwort = userTextField.getText().trim(); | |
if (!suchwort.isEmpty()) { | |
replaceImages(suchwort); | |
} | |
} | |
}); | |
**2. Bestimmter Kommentar löschen:** | |
public void deleteComment(String text) { | |
String q = "Update Bewertung SET Kommentar = null WHERE Kommentar = ?"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement m = con.prepareStatement(q)) { | |
m.setString(1, text); | |
if (m.executeUpdate() > 0) { | |
System.out.println("Änderung erfolgreich"); | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
**3. Gadget löschen** | |
static public void deleteItem(String url) { | |
String query = "DELETE FROM gadgets WHERE URL = ?"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, url); | |
pst.executeUpdate(); | |
} catch (SQLException e) { | |
throw new RuntimeException("Error during delete item: " + e.getMessage(), e); | |
} | |
} | |
**3.1 Button einfügen:** | |
deleteButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
DBQueries.deleteItem(gadgetURLfield.getText()); | |
} | |
}); | |
**4. Average Rating bekommen** | |
public double getAvRating() { | |
String query = "SELECT Gefallen FROM Bewertung"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query); | |
ResultSet rs = pst.executeQuery()) { | |
double total = 0; | |
int count = 0; | |
while (rs.next()) { | |
total += rs.getInt("Gefallen"); // Spaltenname direkt nutzen | |
count++; | |
} | |
return (count == 0) ? 0 : total / count; | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler bei der Berechnung des Durchschnitts", e); | |
} | |
} | |
**4.1. In Button einfügen** | |
JButton avgButton = new JButton("Durchschnitt berechnen"); | |
avgButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
public void actionPerformed(ActionEvent e) { | |
System.out.println("Durchschnittliche Bewertung: " + DBQueries.getAvRating()); | |
} | |
} | |
}); | |
**5. Daten anhand eines Titels suchen:** (DTO und arraylist hier unnötig) | |
public static List<DTO> searchByTitle(String title) { | |
List<DTO> resultList = new ArrayList<>(); | |
String query = "SELECT * FROM Gadgets WHERE title LIKE ?"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, "%" + title + "%"); | |
ResultSet rs = pst.executeQuery(); | |
while (rs.next()) { | |
DTO dto = new DTO(); | |
dto.setUrl(rs.getString("url")); | |
dto.setEmail(rs.getString("email")); | |
dto.setTitle(rs.getString("title")); | |
dto.setDescription(rs.getString("description")); | |
dto.setComments(rs.getString("comments")); | |
dto.setRating(rs.getInt("rating")); | |
resultList.add(dto); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return resultList; | |
} | |
**6. Gadget Bewertung ändern:** | |
public static boolean updateRating(String url, int newRating) { | |
String query = "UPDATE gadgets SET rating = ? WHERE url = ?"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setInt(1, newRating); | |
pst.setString(2, url); | |
return pst.executeUpdate() > 0; | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
**7. Neues Gadget hinzufügen:** | |
public static boolean insertGadget(String url, String title, String description){ | |
String query = "INSERT INTO gadgets (url, title, description) VALUES (?, ?, ?)"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
// hier natürlich schauen was das gadget alles braucht, auch oben anzahl = ?) | |
pst.setString(1, url); | |
pst.setString(2, title); | |
pst.setString(3, description); | |
return pst.executeUpdate() > 0; | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
**8. Gadget mit Bild (Fileinputsream) einfügen:** | |
public static boolean insertGadgetWithImage(String url, String title, File imageFile) { | |
String query = "INSERT INTO gadgets (url, title, cover) VALUES (?, ?, ?)"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query); | |
FileInputStream fis = new FileInputStream(imageFile)) { | |
pst.setString(1, url); | |
pst.setString(2, title); | |
pst.setBlob(3, fis, (int) imageFile.length()); | |
return pst.executeUpdate() > 0; | |
} catch (SQLException | IOException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
**9. Bild eines Gadget ausgeben lassen:** | |
public static ImageIcon getGadgetImage(String url) { | |
String query = "SELECT cover FROM gadgets WHERE url = ?"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, url); | |
ResultSet rs = pst.executeQuery(); | |
if (rs.next()) { | |
Blob blob = rs.getBlob("cover"); | |
if (blob != null) { | |
byte[] imageBytes = blob.getBytes(1, (int) blob.length()); | |
return new ImageIcon(imageBytes); | |
} | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
**10. Das Bild eines Gadget löschen:** | |
public static boolean deleteGadgetImage(String url) { | |
String query = "UPDATE gadgets SET cover = NULL WHERE url = ?"; | |
try (Connection con = DatabaseConnection.getConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, url); | |
return pst.executeUpdate() > 0; | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
**11. Gruppe A:** Wenn Sie den button **"LabTest"** drücken wird im Feld "psswdTextField" die URL und Beschreibung desjenigen gadgets ausgegeben, welches den im Feld "userTextField" enthaltenen String in seiner Beschreibung enthält ( .. description like '%schloss%'). Existieren mehrere Beschreibungen, wird irgendeine ausgewählt.) Eingabe: "schloss" (linkes Textfeld), Ausgabe: "http://tex-lock.com/#jedes text-lock fahrradschloss steht und fällt mit seinem Seil..." (rechtes Textfeld): | |
public static String getGadgetInfo(String suchwort) { | |
String suchwort2 = "%" + suchwort + "%"; | |
String query = "SELECT URL, description FROM Gadgets WHERE description LIKE ?; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, suchwort2); | |
ResultSet rs = pst.executeQuery(); | |
if (rs.next()) { | |
return rs.getString("url") + " - " + rs.getString("description"); | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler beim Abrufen der Gadget-Info", e); | |
} | |
return "Kein passender Eintrag gefunden."; | |
} | |
**11.1. ButtonListener:** | |
JButton labTestButton = new JButton("LabTest"); | |
JTextField userTextField = new JTextField(20); | |
JTextField psswdTextField = new JTextField(40); | |
psswdTextField.setEditable(false); // Damit es nur als Ausgabe dient | |
labTestButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String suchwort = userTextField.getText().trim(); | |
psswdTextField.setText(getGadgetInfo(suchwort)); | |
} | |
}); | |
**12. Gruppe B:** Wenn Sie den button "LabTest" drücken wird im Feld "psswdTextField" die URL und Beschreibung desjenigen gadgets ausgegeben, welches den im Feld "userTextField" enthaltenen String in seinem Username enthält (... email like '%abc%'). (Existieren mehrere user, wird irgendeiner ausgewählt.) Eingabe: "abc" (linkes Textfeld), Ausgabe: "abc@web.de" (rechtes Textfeld): | |
public static List<String> getGadgetInfo(String suchwort) { | |
List<String> results = new ArrayList<>(); | |
String suchwort2 = "%" + suchwort + "%"; | |
String query = "SELECT URL, description FROM Gadgets WHERE description LIKE ?"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, suchwort2); | |
ResultSet rs = pst.executeQuery(); | |
while (rs.next()) { | |
results.add(rs.getString("url") + " - " + rs.getString("description")); | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler beim Abrufen der Gadget-Info", e); | |
} | |
return results.isEmpty() ? Collections.singletonList("Kein passender Eintrag gefunden.") : results; | |
} | |
**12.1. Buttonlistener:** | |
JButton labTestButton = new JButton("LabTest"); | |
JTextField usernameTextField = new JTextField(20); | |
JTextField passwortTextField = new JTextField(40); | |
psswdTextField.setEditable(false); // Damit es nur als Ausgabe dient | |
labTestButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String suchwort = usernameTextField.getText().trim(); | |
passwortTextField.setText(getGadgetInfo(suchwort)); | |
} | |
}); | |
**13. Blob generieren random lassen für ein gadget** | |
public static void addGadgetrandom(String usermail) { | |
// usermail für email | |
// gadgetUrl für URL | |
// icon für cover (blob) | |
Random random = new Random(); | |
int randomNumber = random.nextInt(1000); | |
String gadgetUrl = Integer.toString(randomNumber); | |
Icon icon = Converter.generatePatternIcon(); | |
String insertqueue = "INSERT INTO Gadgets (email, URL, cover) VALUES ( ?, ?, ?)"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(insertqueue)) { | |
Blob blob = Converter.icon2Blob(icon, con); | |
pst.setString(1, usermail); | |
pst.setString(2, gadgetUrl); | |
pst.setBlob(3, blob); | |
pst.executeUpdate(); | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler beim Ersetzen der Bilder", e); | |
} | |
} | |
**13.1 Den Button setzen** | |
labTestBtn.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String name = userTextField.getText(); | |
DBQueries.addGadgetrandom(name); | |
} | |
}); | |
**14 nächstes Gadget in der Liste ausgeben** | |
static public DTOGadget nextGadget(String currentUrl) { | |
String query = "SELECT * FROM gadgets WHERE URL > ? ORDER BY URL ASC FETCH FIRST 1 ROWS ONLY"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, currentUrl); | |
try (ResultSet rs = pst.executeQuery()) { | |
if (rs.next()) { | |
DTOGadget ret = new DTOGadget(); | |
ret.url = rs.getString("URL"); | |
ret.email = rs.getString("email"); | |
ret.keywords = rs.getString("keywords"); | |
ret.description = rs.getString("description"); | |
ret.cover = rs.getBlob("cover"); | |
return ret; | |
} | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Error retrieving next gadget: " + e.getMessage(), e); | |
} | |
return null; | |
} | |
**14.1 button für next** | |
nextButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
// if (gadget < DBQueries.count()) | |
DTOGadget data = DBQueries.nextGadget(gadgetURLfield.getText()); | |
gadget++; | |
assert data != null; | |
descriptionArea.setText(data.description); | |
keywordField.setText(data.keywords); | |
gadgetURLfield.setText(data.url); | |
ownerLabel.setText(data.email); | |
imageLabel.setIcon(Converter.blob2Icon(data.cover)); | |
commentsArea.setText(data.comment); | |
// ratingLabel.setText(String.format("%.1f", data.rating).replace('.', ',')); | |
} | |
}); | |
**15 überprüft ob nutzer eingelogged ist** | |
static public boolean login(String email, String password) { | |
String query = "SELECT COUNT(*) FROM users WHERE email = ? AND passwd = ?"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, email); | |
pst.setString(2, password); | |
try (ResultSet rs = pst.executeQuery()) { | |
return rs.next() && rs.getInt(1) > 0; | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Error during login: " + e.getMessage(), e); | |
} | |
} | |
**15.1 button logged in?** | |
logInButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if(DBQueries.login(usernameTextField.getText(),passwortTextField.getText())){ | |
conLabel.setText("logged in"); | |
// loggedin = true; | |
} else { | |
conLabel.setText("not logged in"); | |
// loggedin = false; | |
} | |
} | |
}); | |
**15.2 register** | |
static public void register(String email, String password) { | |
String query = "INSERT INTO users (email, passwd) VALUES (?, ?)"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query)) { | |
pst.setString(1, email); | |
pst.setString(2, password); | |
pst.executeUpdate(); | |
} catch (SQLException e) { | |
throw new RuntimeException("Error during registration: " + e.getMessage(), e); | |
} | |
} | |
**15.3 register button** | |
registerButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
String email = usernameTextField.getText().trim(); | |
String password = passwortTextField.getText().trim(); | |
try { | |
DBQueries.register(email, password); | |
} catch (RuntimeException ex) { | |
System.err.println("Fehler bei der Registrierung: " + ex.getMessage()); | |
} | |
} | |
}); | |
**16 clear button (ohne methode)** | |
clearButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
descriptionArea.setText(""); | |
keywordField.setText(""); | |
gadgetURLfield.setText(""); | |
ownerLabel.setText(""); | |
imageLabel.setIcon(null); | |
commentsArea.setText(""); | |
ratingLabel.setText(""); | |
} | |
}); | |
**17 count methode** | |
static public int count() { | |
String query = "SELECT COUNT(*) FROM gadgets"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query); | |
ResultSet rs = pst.executeQuery()) { | |
if (rs.next()) { | |
return rs.getInt(1); | |
} else { | |
return 0; | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Error during count query: " + e.getMessage(), e); | |
} | |
} | |
**17.1 count button** | |
countButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
int c = DBQueries.count(); | |
ausgabeLabel.setText("Count: " + c); | |
} | |
}); | |
**18 Zeile 1 und/oder 3 ausgeben** | |
public static String getComment(int index) { | |
String query = "SELECT kommentar FROM bewertung ORDER BY kommentar ASC"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
ResultSet rs = pst.executeQuery()) { | |
// Mit absolute(n) springen wir zur n-ten Zeile des ResultSets | |
if (rs.absolute(index)) { | |
return rs.getString("kommentar"); | |
} else { | |
return "Kein Kommentar gefunden für Index " + index; | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Fehler beim Abrufen des Kommentars", e); | |
} | |
**18.1 zugehöriger button** | |
firstButtonButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
// Lies die Benutzereingabe aus userTextField (soll "1" oder "3" sein) | |
String input = userTextField.getText().trim(); | |
if (!input.equals("1") && !input.equals("3")) { | |
return; | |
} | |
int index = Integer.parseInt(input); | |
try { | |
// Rufe den Kommentar mit dem entsprechenden Index ab | |
String comment = DBQueries.getComment(index); | |
// Zeige den Kommentar im Label imageLabel an | |
imageLabel.setText(comment); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
imageLabel.setText("Fehler: " + ex.getMessage()); | |
} | |
} | |
}); | |
**19 First Gadget (mit DTO)** | |
static public DTOGadget firstGadget() { | |
DTOGadget ret = new DTOGadget(); | |
String query = "SELECT * FROM gadgets ORDER BY URL ASC"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement st = con.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); | |
ResultSet rs = st.executeQuery()) { | |
if (rs.next()) { | |
ret.url = rs.getString("URL"); | |
ret.email = rs.getString("email"); | |
ret.keywords = rs.getString("keywords"); | |
ret.description = rs.getString("description"); | |
ret.cover = rs.getBlob("cover"); | |
loadCommentsAndRatings(ret, con); | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Error in firstGadget: " + e.getMessage(), e); | |
} | |
return ret; | |
} | |
**19.1 firstbutton** | |
firstButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
gadget = 1; | |
DTOGadget data = DBQueries.firstGadget(); | |
descriptionArea.setText(data.description); | |
keywordField.setText(data.keywords); | |
gadgetURLfield.setText(data.url); | |
ownerLabel.setText(data.email); | |
imageLabel.setIcon(Converter.blob2Icon(data.cover)); | |
commentsArea.setText(data.comment); | |
//ratingLabel.setText(String.format("%.1f", data.rating).replace('.', ',')); | |
} | |
} | |
}); | |
**20 Bestimmte Zeile ausgeben, sortiert nach URL zeile z spalte s** | |
public static String getinhalt (int z, int s) { | |
String q = "SELECT * from bewertung ORDER BY URL ASC"; | |
try (Connection con = Globals.getPoolConnection(); | |
PreparedStatement pst = con.prepareStatement(q); | |
ResultSet rs = pst.executeQuery();) { | |
int index = 0; | |
while (rs.next()){ | |
index++; | |
if (index == z){ | |
return rs.getString(s); | |
} | |
} | |
} catch (SQLException e) { | |
throw new RuntimeException("Error keine spalte " + e.getMessage(), e); | |
} | |
return "pissbad"; | |
} | |
**20.1 button setzen.............** | |
clearButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
int z = Integer.parseInt(usernameTextField.getText()); | |
int s = Integer.parseInt(passwortTextField.getText()); | |
String inhalt = DBQueries.getinhalt(z,s); | |
imageLabel.setText(inhalt); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment