From cde6ffac00da920ecc24e7667f34136b21c47d77 Mon Sep 17 00:00:00 2001 From: Atdhe Date: Thu, 22 May 2025 09:01:21 +0200 Subject: [PATCH] Datenbanken --- .DS_Store | Bin 0 -> 6148 bytes db/.DS_Store | Bin 0 -> 6148 bytes db/Converter.java | 214 +++++++++++++++++++++++++ db/DBQueries.java | 390 ++++++++++++++++++++++++++++++++++++++++++++++ db/DTO.java | 29 ++++ db/GUI.form | 266 +++++++++++++++++++++++++++++++ db/GUI.java | 193 +++++++++++++++++++++++ db/Globals.java | 45 ++++++ db/Main.java | 24 +++ 9 files changed, 1161 insertions(+) create mode 100644 .DS_Store create mode 100644 db/.DS_Store create mode 100644 db/Converter.java create mode 100644 db/DBQueries.java create mode 100644 db/DTO.java create mode 100644 db/GUI.form create mode 100644 db/GUI.java create mode 100644 db/Globals.java create mode 100644 db/Main.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..52ae1e5625fece818aeb63c7075ba4ee9adfaf44 GIT binary patch literal 6148 zcmeHK%}N6?5Kh`^Q$*-NL2m)C1-n*M5MizL1zgdCO6_)wE^ara+uB1Z>|KvOfUn`r zlUHwg^I4opQh%skrN|6SzR6@Jn|!-XI*c)1?FSBHMaGx_Ma&eS`9W|TbwV=MlLzED zhaV{)><7OSg%i=_m_`Qh-DOz7e1DL1zP`VtS1!NsOg1+&n-_U8C(gV}HSl_kUO%Ze zx+mB>R4VevcEdjkTZ4LWVNb=qMi{rcIw1^N5OR7P#(^4CRX+|Ao$HwaQ4ocCad|j& z%H@(=+uRtH+p^@?mip zs2$OT<26;gVt@n z$4H0)Vt^Q!E(YugCFZ7US+p8rfEbtp2Jn24pop%;)Sx~(pwT4&U=G|`z{XkvbEHMr zVrmdZK)6W-G^yN{7~G_TU)ng=VrtN&Gj59yZlBC;g~IjIVScH@8Fvj*OAHVL-x3o4E!qwc&_Hvs<0({w@z#h?^+3Z4vKL8R2>lVzG*Ckf{3!$Po-bzo literal 0 HcmV?d00001 diff --git a/db/.DS_Store b/db/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 icons) { + int iconsPerRow = 3; + if (icons == null || icons.isEmpty()) { + throw new IllegalArgumentException("Die Icon-Liste darf nicht leer sein."); + } + if (iconsPerRow <= 0) { + throw new IllegalArgumentException("Die Anzahl der Icons pro Zeile muss größer als 0 sein."); + } + + // Berechnung der Dimensionen des kombinierten Icons + int rows = (int) Math.ceil((double) icons.size() / iconsPerRow); // Gesamtanzahl der Zeilen + int maxWidth = 0; + int totalHeight = 0; + int[] rowHeights = new int[rows]; // Speichert die maximale Höhe jeder Zeile + + for (int row = 0; row < rows; row++) { + int currentRowWidth = 0; + int currentRowHeight = 0; + + for (int col = 0; col < iconsPerRow; col++) { + int index = row * iconsPerRow + col; + if (index >= icons.size()) break; // Keine weiteren Icons + + Icon icon = icons.get(index); + currentRowWidth += icon.getIconWidth(); + currentRowHeight = Math.max(currentRowHeight, icon.getIconHeight()); + } + + maxWidth = Math.max(maxWidth, currentRowWidth); + totalHeight += currentRowHeight; + rowHeights[row] = currentRowHeight; + } + + // Neues kombiniertes Bild erstellen + BufferedImage combinedImage = new BufferedImage(maxWidth, totalHeight, BufferedImage.TYPE_INT_ARGB); + Graphics g = combinedImage.getGraphics(); + + // Icons zeichnen + int currentY = 0; + for (int row = 0; row < rows; row++) { + int currentX = 0; + for (int col = 0; col < iconsPerRow; col++) { + int index = row * iconsPerRow + col; + if (index >= icons.size()) break; + + Icon icon = icons.get(index); + icon.paintIcon(null, g, currentX, currentY); + currentX += icon.getIconWidth(); + } + currentY += rowHeights[row]; + } + + g.dispose(); + + // Kombiniertes Bild als Icon zurückgeben + return new ImageIcon(resizeTo250x250(combinedImage)); + } + + public static BufferedImage resizeTo250x250(BufferedImage originalImage) { + if (originalImage == null) { + throw new IllegalArgumentException("Das Bild darf nicht null sein."); + } + + // Neue Dimensionen für das Bild + int targetWidth = 200; + int targetHeight = 300; + + // Erstelle ein neues BufferedImage mit den Zielmaßen + BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB); + + // Zeichne das originale Bild auf das neue Bild mit Skalierung + Graphics2D g2d = resizedImage.createGraphics(); + g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); + g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null); + g2d.dispose(); + + return resizedImage; + } + + public static Icon generatePatternIcon() { + // Bildgröße + int width = 250; + int height = 250; + + // Erstelle ein BufferedImage + BufferedImage patternImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + + // Muster erzeugen (z.B. Rechteckmuster mit zufälligen Farben) + Graphics2D g2d = patternImage.createGraphics(); + + for (int y = 0; y < height; y += 25) { // Abstand der horizontalen Streifen + for (int x = 0; x < width; x += 25) { // Abstand der vertikalen Streifen + // Erzeuge eine zufällige Farbe + Color randomColor = new Color( + (int) (Math.random() * 255), // Rot + (int) (Math.random() * 255), // Grün + (int) (Math.random() * 255) // Blau + ); + + // Zeichne Rechtecke für das Muster + g2d.setColor(randomColor); + g2d.fillRect(x, y, 25, 25); + } + } + + g2d.dispose(); + + // Rückgabe des generierten Musters als Icon + return new ImageIcon(patternImage); + } +} + + diff --git a/db/DBQueries.java b/db/DBQueries.java new file mode 100644 index 0000000..f52858b --- /dev/null +++ b/db/DBQueries.java @@ -0,0 +1,390 @@ +package edu.hsog.db; + +import javax.swing.*; +import java.sql.*; +import java.util.ArrayList; + +public class DBQueries { + + // Count ohne try-with-resource und mit statement + public static int count() { + + Connection con = Globals.getPoolConnection(); + Statement st = null; + ResultSet rs = null; + try { + st = con.createStatement(); + String q = "select count(*)\n" + + "from gadgets"; + rs = st.executeQuery(q); + rs.next(); + return rs.getInt(1); + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (rs != null) rs.close(); + if (st != null) st.close(); + if (con != null) con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + + // Count mit try-with-resource und mit statement + /* + public static int count() { + String query = "SELECT COUNT(*) FROM gadgets"; + + try (Connection con = Globals.getPoolConnection(); + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery(query)) { + + rs.next(); // Kein if, da COUNT(*) immer ein Ergebnis liefert + return rs.getInt(1); + + } catch (SQLException e) { + throw new RuntimeException("Error while counting gadgets", e); + } + */ + + // Count mit try-with-resource und mit prepared-statement + /* + public static int count() { + String query = "SELECT COUNT(*) FROM gadgets"; + + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query); + ResultSet rs = pst.executeQuery()) { + + rs.next(); // Kein if, da COUNT(*) immer ein Ergebnis liefert + return rs.getInt(1); + + } catch (SQLException e) { + throw new RuntimeException("Error while counting gadgets", e); + } + } + */ + + // Login mit try-with-resource und mit statement + /* + public static boolean login(String username, String password) { + + Connection con = Globals.getPoolConnection(); + Statement st = null; + ResultSet rs = null; + try { + st = con.createStatement(); + + String q = "select count(*)\n" + + "from users\n" + + "where email = '" + username + "' and passwd = '" + password + "'"; + rs = st.executeQuery(q); + rs.next(); + int c = rs.getInt(1); + return (c == 1); + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (rs != null) rs.close(); + if (st != null) st.close(); + if (con != null) con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } + */ + + // Login mit try-with-resource und mit prepared-statement + public static boolean login(String username, String password) { + String query = "SELECT COUNT(*) FROM users WHERE email = ? AND passwd = ?"; + + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query)) { + + // Parameter setzen + pst.setString(1, username); + pst.setString(2, password); + + try (ResultSet rs = pst.executeQuery()) { + rs.next(); // Kein if notwendig, COUNT(*) liefert immer eine Zeile + int count = rs.getInt(1); + return count == 1; + } + + } catch (SQLException e) { + throw new RuntimeException("Error during login attempt", e); + } + } + + public static ArrayList showAll() { + String query = "SELECT * FROM gadgets"; + ArrayList icons = new ArrayList(); + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query)) { + + // Parameter setzen + // pst.setString(1, username); + try (ResultSet rs = pst.executeQuery()) { + while (rs.next()) { + Icon i = Converter.blob2Icon(rs.getBlob("cover")); + icons.add(i); + } + return icons; + } + } catch (SQLException e) { + throw new RuntimeException("Error during login attempt", e); + } + } + + public static ImageIcon getImage(int z) { + String query = "SELECT * from gadgets order by url asc"; + + ImageIcon imIcon; + + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query)){ + ResultSet rs = pst.executeQuery(); + + for(int i = 0; i iconListEmail(String username) { + String query = "SELECT cover from gadgets where email = ?"; + Icon icon = Converter.generatePatternIcon(); + ArrayListmyList = new ArrayList<>(); + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query)) { + + pst.setString(1,username); + ResultSet rs = pst.executeQuery(); + + while (rs.next()){ + + icon = Converter.blob2Icon(rs.getBlob("cover")); + myList.add(icon); + + } + return myList; + } catch (SQLException e) { + throw new RuntimeException("Error during login attempt", e); + } + } + + + public static int searchGadgetsUrlIndex(String url) { + + Connection con = Globals.getPoolConnection(); + Statement st = null; + ResultSet rs = null; + try { + st = con.createStatement(); + String q = "SELECT * from gadgets"; + rs = st.executeQuery(q); + int lineCounter = 1; + String foundURL = null; + + while (rs.next()){ + if(rs.getString(1).contains(url)){ + foundURL = url; + break; + } + lineCounter ++; + } + if(foundURL != null){ + return lineCounter; + } + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (rs != null) rs.close(); + if (st != null) st.close(); + if (con != null) con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return -1; + } + + public static String zeilenSpalten(int z, int col) { + String query = "select * from bewertung order by url asc"; + + try (Connection con = Globals.getPoolConnection(); + Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); + ResultSet rs = st.executeQuery(query )) { + + rs.next(); + // Parameter setzen + rs.absolute(z); + + if(col== 1){ + return rs.getString("email"); + } + if(col== 2){ + return rs.getString("url"); + } + if(col== 3){ + return rs.getString("bewertung"); + } + if(col== 4){ + return rs.getString("kommentars"); + } + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + + + return ".."; + } + + + public static DTO emailPassword(String teil) { + String query = "SELECT EMAIL,PASSWD FROM users WHERE email like ? "; + + try (Connection con = Globals.getPoolConnection(); + PreparedStatement pst = con.prepareStatement(query)) { + + // Parameter setzen + pst.setString(1,"%"+ teil+"%"); + + + ResultSet rs = pst.executeQuery(); + rs.next(); // Kein if notwendig, COUNT(*) liefert immer eine Zeile + + String email = rs.getString("EMAIL"); + String passwd = rs.getString("PASSWD"); + + return new DTO(email,passwd); + + } catch (SQLException e) { + throw new RuntimeException("Error during login attempt", e); + } + } + + public static int sumGefallenUrl(String eingabe) { + + Connection con = Globals.getPoolConnection(); + Statement st = null; + ResultSet rs = null; + try { + st = con.createStatement(); + String q = "select sum(gefallen) from bewertung where url like '%" + eingabe +"%' "; + rs = st.executeQuery(q); + + if(rs.next()){ + return rs.getInt(1); + }else { + return 0; + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } finally { + try { + if (rs != null) rs.close(); + if (st != null) st.close(); + if (con != null) con.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + } +} diff --git a/db/DTO.java b/db/DTO.java new file mode 100644 index 0000000..51b414d --- /dev/null +++ b/db/DTO.java @@ -0,0 +1,29 @@ +package edu.hsog.db; + +public class DTO { + + String email; + String password; + + + public DTO(String email, String password) { + this.email = email; + this.password = password; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/db/GUI.form b/db/GUI.form new file mode 100644 index 0000000..9e16883 --- /dev/null +++ b/db/GUI.form @@ -0,0 +1,266 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/db/GUI.java b/db/GUI.java new file mode 100644 index 0000000..d31b863 --- /dev/null +++ b/db/GUI.java @@ -0,0 +1,193 @@ +package edu.hsog.db; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Random; + +public class GUI { + public GUI() { + + slider1.setMinimum(1); + slider1.setMaximum(5); + slider1.setMajorTickSpacing(1); + slider1.setPaintTicks(true); + slider1.setPaintLabels(true); + slider1.setValue(3); + + + exitButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }); + initConPoolButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Globals.initConnectionPool(); + conLabel.setText("verbunden"); + } + }); + countButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + int c = DBQueries.count(); + countJLabel.setText("Count: " + c); + } + }); + loginButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + String user = userTextField.getText(); + String passwd = passwdTextField.getText(); + boolean loggedIn = DBQueries.login(user, passwd); + if (loggedIn) { + conLabel.setText("logged in"); + + Globals.currentmail = user; + } else { + Globals.currentmail = null; + + conLabel.setText("not logged in"); + } + } + }); + loadImageButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + imageLabel.setIcon(Converter.mergeIcons(DBQueries.showAll())); + + + } + }); + getImageByZahlGadgetsAscButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + String eingabe = userTextField.getText(); + ImageIcon i = DBQueries.getImage(Integer.parseInt(eingabe)); + imageLabel.setIcon(i); + } + }); + getImageByUrlGadgetsURLButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String urlEingabe = userTextField.getText().trim(); + ImageIcon i = DBQueries.getImageByUrl(urlEingabe); + imageLabel.setIcon(i); + } + }); + getImageByDescGadgetsDescButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String descEingabe = userTextField.getText().trim(); + ImageIcon i = DBQueries.getImageByDesc(descEingabe); + imageLabel.setIcon(i); + } + }); + + insertImageByURLEmailButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + Random random = new Random(); + int randomNumber = random.nextInt(1000); + String gadgetUrl = Integer.toString(randomNumber); + String eingabe = "abc@web.de"; + DBQueries.insertImageUrlEmail(gadgetUrl,eingabe); + + } + }); + updateBildLikeKeywordsButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String eingabe = "%"+userTextField.getText()+"%"; + DBQueries.updateItemLikeKeywords(eingabe); + } + }); + iconListEmailButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + ArrayListergebnis = DBQueries.iconListEmail(userTextField.getText()); + imageLabel.setIcon(Converter.mergeIcons(ergebnis)); + + } + }); + searchGadgetsByUrlButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + + int ergebnis = DBQueries.searchGadgetsUrlIndex(userTextField.getText()); + imageLabel.setText(" :"+ ergebnis); + } + }); + zeilenSpaltenButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + int row = Integer.parseInt(userTextField.getText()); + int col = Integer.parseInt((passwdTextField.getText())); + String ergebnis = DBQueries.zeilenSpalten(row,col); + imageLabel.setText(ergebnis); + } + }); + emailPasswordButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + DTO ergebnis = DBQueries.emailPassword(userTextField.getText()); + + String s1 = ergebnis.email; + String s2 = ergebnis.password; + + passwdTextField.setText(s1 +" #" +s2); + + } + }); + sumGefallenByURLButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + String eingabe = userTextField.getText(); + passwdTextField.setText(String.valueOf(DBQueries.sumGefallenUrl(eingabe))); + } + }); + } + + public JPanel getMasterPanel() { + return masterPanel; + } + + private JPanel masterPanel; + private JButton exitButton; + private JButton initConPoolButton; + private JButton countButton; + private JLabel conLabel; + private JLabel countJLabel; + private JTextField userTextField; + private JTextField passwdTextField; + private JButton loginButton; + private JButton registerButton; + private JLabel imageLabel; + private JButton loadImageButton; + private JSlider slider1; + private JButton getImageByZahlGadgetsAscButton; + private JButton getImageByUrlGadgetsURLButton; + private JButton getImageByDescGadgetsDescButton; + private JButton insertImageByURLEmailButton; + private JButton updateBildLikeKeywordsButton; + private JButton iconListEmailButton; + private JButton searchGadgetsByUrlButton; + private JButton zeilenSpaltenButton; + private JButton emailPasswordButton; + private JButton sumGefallenByURLButton; + + +} diff --git a/db/Globals.java b/db/Globals.java new file mode 100644 index 0000000..9657c46 --- /dev/null +++ b/db/Globals.java @@ -0,0 +1,45 @@ +package edu.hsog.db; + +import org.apache.commons.dbcp2.BasicDataSource; +import java.sql.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author student + */ +public class Globals { + + static String url = "jdbc:oracle:thin:@//localhost:1521/FREE"; + // static String url = "jdbc:mariadb://localhost:1521/chris"; + static String username = "chris"; + static String passwd = "xyz"; + static String currentmail= null; + + static private BasicDataSource conPool = null; + + public static void initConnectionPool() { + if(conPool==null){ + System.out.println("Account: " + username + ":" + passwd); + conPool = new BasicDataSource(); + conPool.setDriverClassName("oracle.jdbc.driver.OracleDriver"); + // conPool.setDriverClassName("org.mariadb.jdbc.Driver"); + conPool.setUrl(url); + conPool.setUsername(username); + conPool.setPassword(passwd); + conPool.setMaxTotal(5); + conPool.setInitialSize(5); + } + } + + public static Connection getPoolConnection() { + Connection v_connection = null; + try { + v_connection = conPool.getConnection(); + } catch (SQLException ex) { + Logger.getLogger(Globals.class.getName()).log(Level.SEVERE, null, ex); + } + return v_connection; + } +} \ No newline at end of file diff --git a/db/Main.java b/db/Main.java new file mode 100644 index 0000000..4a92e7f --- /dev/null +++ b/db/Main.java @@ -0,0 +1,24 @@ +package edu.hsog.db; + +import javax.swing.*; +import java.awt.*; + +public class Main { + public static void main(String[] args) throws UnsupportedLookAndFeelException, ClassNotFoundException, InstantiationException, IllegalAccessException { + System.out.println("Hello world!"); + UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); + JFrame frame = generateJFrame(); + frame.setVisible(true); + Globals.initConnectionPool(); //Für Ausprobieren + } + + public static JFrame generateJFrame() { + GUI gui = new GUI(); + JFrame frame = new JFrame(); + frame.setContentPane(gui.getMasterPanel()); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setSize(800, 600); + gui.getMasterPanel().setPreferredSize(new Dimension(800, 600)); + return frame; + } +} \ No newline at end of file