-
Notifications
You must be signed in to change notification settings - Fork 0
/
BandDao.java
79 lines (60 loc) · 2.36 KB
/
BandDao.java
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
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class BandDao implements DAO<BandClass> {
@Override
public void save(BandClass band) throws SQLException{
String sql = "INSERT INTO band (band_name, band_birth) VALUES (?, ?)";
PreparedStatement stt = Connect.connection.prepareStatement(sql);
stt.setString(1, band.getBandName());
stt.setString(2, band.getBandBirth());
int response = stt.executeUpdate();
if(response > 0){
String text = "Band %s successfully created!";
JOptionPane.showMessageDialog(null, String.format(text, band.getBandName()));
}
}
String everyRow = "";
@Override
public String list() throws SQLException{
String sql = "select * from band";
Statement stt = Connect.connection.createStatement();
ResultSet res = stt.executeQuery(sql);
while(res.next()){
int id = res.getInt(1);
String name = res.getString(2);
String year = res.getString(3);
String row = "%d - %s - %s \n";
everyRow += String.format(row, id, name, year);
}
return everyRow;
}
@Override
public void update(int bandId, String columnName, String newValue) throws SQLException{
try {
String sql = "UPDATE band SET %s = ? WHERE band_id = %d";
String formatedString = String.format(sql, columnName, bandId);
PreparedStatement stt = Connect.connection.prepareStatement(formatedString);
stt.setString(1, newValue);
int res = stt.executeUpdate();
if(res > 0){
JOptionPane.showMessageDialog(null, "Band updated!");
}
} catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void delete(int bandId) throws SQLException {
String sql = "DELETE FROM band WHERE band_id = ?";
PreparedStatement stt = Connect.connection.prepareStatement(sql);
stt.setInt(1, bandId);
int res = stt.executeUpdate();
if(res > 0){
String msg = "Band %d successfully deleted!";
JOptionPane.showMessageDialog(null, String.format(msg, bandId));
}
}
}