9. März 2010
MySQL-Datenbankabfrage mit der Java Database Connectivity aus der Java Enterprise Edition. Für die Verwendung wird die Bibliothek mysql-connector-java-5.1.5-bin.jar benötigt, damit der MySQL Driver für die JDBC zur Verfügung steht. Das Kürzel “VO” bezeichnet die Value Objects, welche die Datensätze in der Datenbank sind.
data.CD_VO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package data; import java.sql.Date; import java.util.ArrayList; public class CD_VO { int id; String titel; String interpret; Date veroeffentlichung; String coverDatei; ArrayList<song_VO> songs; public CD_VO() { this.id = 0; this.titel = ""; this.interpret = ""; this.veroeffentlichung = new java.sql.Date(2010-10-10); this.coverDatei = ""; songs = new ArrayList<song_VO>(); } public CD_VO(String titel, String interpret, Date veroeffentlichung, String coverDatei) { setID(); this.titel = titel; this.interpret = interpret; this.veroeffentlichung = veroeffentlichung; this.coverDatei = coverDatei; songs = new ArrayList<song_VO>(); } private void setID() { this.id++; } public String getCoverDatei() { return coverDatei; } public void setCoverDatei(String coverDatei) { this.coverDatei = coverDatei; } public long getId() { return id; } public void setId(int id) { this.id = id; } public String getInterpret() { return interpret; } public void setInterpret(String interpret) { this.interpret = interpret; } public String getTitel() { return titel; } public void setTitel(String titel) { this.titel = titel; } public Date getVeroeffentlichung() { return veroeffentlichung; } public void setVeroeffentlichung(Date veroeffentlichung) { this.veroeffentlichung = veroeffentlichung; } |
repository.JdbcConnection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | package repository; import data.CD_VO; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.LinkedList; public class JdbcConnection { protected Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); return DriverManager.getConnection("jdbc:mysql://localhost/jee", "root", "password"); } catch(SQLException excSQL) { System.out.println("Fehler beim Aufbau der SQL-Verbindung:"); excSQL.printStackTrace(); return null; } catch(ClassNotFoundException excCNF) { System.out.println("Treiber-Manager nicht gefunden."); excCNF.printStackTrace(); return null; } } public void findAllTest() { Connection con = null; PreparedStatement ps = null; ResultSet rs = null; String sqlStr = "SELECT * FROM cd"; LinkedList<cd_VO> voList = new LinkedList<cd_VO>(); CD_VO vo; try { con = getConnection(); ps = con.prepareStatement(sqlStr); rs = ps.executeQuery(); while(rs.next()) { vo = new CD_VO(); vo.setId( rs.getInt("id") ); vo.setInterpret( rs.getString("interpret") ); vo.setTitel( rs.getString("titel") ); vo.setVeroeffentlichung( rs.getDate("veroeffentlichung") ); vo.setCoverDatei( rs.getString("coverdatei") ); voList.add(vo); // Testausgabe: System.out.println( vo.getId() ); System.out.println( vo.getInterpret() ); System.out.println( vo.getTitel() ); System.out.println( vo.getVeroeffentlichung() ); System.out.println( vo.getCoverDatei() ); } } catch(SQLException excSQL) { System.out.println("Fehler beim Abruf aus der SQL-Verbindung:"); excSQL.printStackTrace(); } finally { try { if( ps != null ) ps.close(); if( con != null ) con.close(); } catch(SQLException excSQL) { System.out.println("Fehler beim Abbau der SQL-Verbindung:"); excSQL.printStackTrace(); } } } } |
repository.Main
1 2 3 4 5 6 7 8 9 10 | package repository; public class Main { public static void main(String args[]) { JdbcConnection test = new JdbcConnection(); test.findAllTest(); } } |
27. Februar 2010
Der folgende Code-Schnipssel speichert die Anzahl der Elemente einer Enumeration als Ganzzahl ab:
1 2 3 4 5 6 7 8 | Enumeration enumeration; int enumCount = 0; while(enumeration.hasMoreElements()) { enumCount++; enumeration.nextElement(); } |
28. Januar 2010
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> int main(int argc, char* argv[]) { unsigned char i = 0; /* Eingabe-Maximum: 254 Zeichen */ unsigned char eingabe[255]; /* Text einlesen */ printf("Eingabe:\n"); /* Einlesen der Eingabe mit 255 Chars in "stdin". stdin ist eine Datei, mit dem Inhalt der Tastaur. */ fgets(eingabe,255,stdin); /* Solange der Text noch nicht zu Ende ist */ while(eingabe[i] != '\0') { /* Wenn Zeichen ein Leerzeichen ist */ if(eingabe[i] == 32) { i++; continue; } /* sonst */ else printf("%c",eingabe[i]); /* String weiter parsen */ i += 1; } printf("\n"); return 0; } |
28. Januar 2010
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <stdio.h> int main(int argc, char* argv[]) { unsigned char i = 0; for(i = 1; i < 101; i++) /* 100 Durchläufe */ { printf("\nZahl: %2i",i); if((i%10 != 0)) /* Nächster Schleifendurchlauf, */ continue; /* wenn nicht durch 10 teilbar */ printf("\n"); printf("\nHurra!"); printf("\nDie Zahl %i war durch Zehn teilbar.",i); printf("\n"); } return 0; } |


4