· 8 years ago · Jun 30, 2018, 02:24 PM
1package DBAnbindung;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.util.Vector;
8
9public class Main {
10
11 public static void main(String[] args) throws Exception {
12 //verbindungHerstellen();
13 tabelleErstellen();
14 artEinfuegen();
15 artActuallisieren();
16 infoAbrufen();
17 }
18
19 public static void artActuallisieren() throws Exception {
20 final String neuerName = "Kopfsalat";
21 final String neuerPreis = "0.69";
22 final String gewünschteArtikelnr = "8";
23 try {
24 String sql = "UPDATE beispiel SET name = '"+neuerName+"', preis = '"+neuerPreis+"' WHERE artikelnr = "+gewünschteArtikelnr+"" ;
25 Connection con = verbindungHerstellen();
26 PreparedStatement post = con.prepareStatement(sql);
27 post.executeUpdate();
28 } catch(Exception e) {
29 System.out.println("Fehler beim Einfügen der Attribute: "+e);
30
31 } finally {
32 System.out.println("Informationen erfolgreich in die Tabelle eingetragen");
33
34 }
35
36 }
37
38 public static Vector<String> infoAbrufen() throws Exception {
39 try{
40 Connection con = verbindungHerstellen();
41 String query = "SELECT * FROM beispiel";
42 PreparedStatement stm = con.prepareStatement(query);
43
44 ResultSet erg = stm.executeQuery();
45
46 while(erg.next()) {
47 Vector<String> selectInfo = new Vector<String>();
48 selectInfo.add(erg.getString("artikelnr"));
49 selectInfo.add(erg.getString("name"));
50 selectInfo.add(erg.getString("preis"));
51
52 System.out.println(selectInfo);
53
54 }
55 System.out.println("Informationen Erfolgreich abgerufen");
56 } catch(Exception e) {
57 System.out.println("Beim Abrufen der Informationen ist ein Fehler unterlaufen: " +e);
58
59 }
60 return null;
61 }
62
63 public static void artEinfuegen() throws Exception {
64 final String var1 = "Brötchen";
65 final String var2 = "0.13";
66 try {
67 String sql = "INSERT INTO beispiel (name, preis) VALUES ('"+var1+"', '"+var2+"')";
68 Connection con = verbindungHerstellen();
69 PreparedStatement post = con.prepareStatement(sql);
70 post.executeUpdate();
71 } catch(Exception e) {
72 System.out.println("Fehler beim Einfügen der Attribute: "+e);
73
74 } finally {
75 System.out.println("Informationen erfolgreich in die Tabelle eingetragen");
76
77 }
78
79 }
80
81 public static void tabelleErstellen() throws Exception {
82 try{
83 Connection con = verbindungHerstellen();
84 String sql = "CREATE TABLE IF NOT EXISTS benutzer(benutzernr int NOT NULL AUTO_INCREMENT, vorname varchar(255), nname varchar(255), PRIMARY KEY(benutzernr))";
85 PreparedStatement create = con.prepareStatement(sql);
86 create.executeUpdate();
87 } catch(Exception e){
88 System.out.println("Fehler beim erstellen der Tabelle: "+e);
89 } finally {
90 System.out.println("Erstellen der Tabelle wurde erfolgreich probiert");
91 }
92 }
93
94 public static Connection verbindungHerstellen() throws Exception{
95 try{
96 String treiber = "com.mysql.jdbc.Driver";
97 String url = "jdbc:mysql://localhost:3306/beispiel";
98 String benutzer = "root";
99 String passwort = "";
100 Class.forName(treiber);
101
102 Connection conn = DriverManager.getConnection(url,benutzer,passwort);
103 System.out.println("Verbindung zur Datenbank hergestellt");
104 return conn;
105 } catch(Exception e){
106 System.out.println("Fehler beim Verbinden: "+e);
107 }
108
109
110 return null;
111 }
112
113}