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;
} |
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();
}
}
}
} |
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();
}
} |
package repository;
public class Main
{
public static void main(String args[])
{
JdbcConnection test = new JdbcConnection();
test.findAllTest();
}
}
Java Database Connectivity in Aktion mit phpMyAdmin und NetBeans