Skip to content

Commit 33eaec3

Browse files
committed
Agora o código funciona 100% na base de queries
1 parent 384b30b commit 33eaec3

File tree

3 files changed

+66
-35
lines changed

3 files changed

+66
-35
lines changed

MVCMusicas/src/control/MusicaControl.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
import pExceptions.MusicNotFoundException;
99

1010
public class MusicaControl {
11-
private ArrayList<Musica> lstMusica;
1211
private final MusicaDAO dao;
1312
private static MusicaControl musicaControl;
1413

1514
private MusicaControl() throws SQLException {
1615
this.dao = MusicaDAO.getInstance();
17-
this.lstMusica = new ArrayList<Musica>();
18-
fetch();
1916
}
2017

2118
public static MusicaControl getInstance() throws SQLException {
@@ -27,52 +24,54 @@ public static MusicaControl getInstance() throws SQLException {
2724

2825
public void add(Musica e) throws SQLException {
2926
dao.add(e);
30-
lstMusica.add(e);
3127
}
3228

3329
public Musica find(String nome, String autor) throws MusicNotFoundException, SQLException {
3430
return findAll(nome, autor).get(0);
3531
}
3632

3733
public ArrayList<Musica> findAll(String nome, String autor) throws MusicNotFoundException, SQLException {
38-
fetch();
39-
ArrayList<Musica> lMusicas = new ArrayList<Musica>();
34+
ArrayList<Musica> lMusicas = dao.selectByAuthorANDName(nome, autor);
4035

36+
/*
4137
for (Musica musica : lstMusica) {
4238
if (musica.getAutor().toLowerCase().startsWith(autor.toLowerCase())
43-
&& musica.getNome().toLowerCase().startsWith(nome.toLowerCase()))
44-
lMusicas.add(musica);
39+
&& musica.getNome().toLowerCase().startsWith(nome.toLowerCase()))
40+
lMusicas.add(musica);
4541
}
46-
42+
*/
43+
4744
if (lMusicas.size() < 1)
4845
throw new MusicNotFoundException(MusicNotFoundException.NotFound);
4946

5047
return lMusicas;
5148
}
5249

5350
public ArrayList<Musica> findByName(String nome) throws MusicNotFoundException, SQLException {
54-
fetch();
55-
ArrayList<Musica> lMusicas = new ArrayList<Musica>();
51+
ArrayList<Musica> lMusicas = dao.selectByName(nome);
5652

53+
/*
5754
for (Musica musica : lstMusica) {
5855
if (musica.getNome().toLowerCase().startsWith(nome.toLowerCase()))
5956
lMusicas.add(musica);
6057
}
61-
58+
*/
59+
6260
if (lMusicas.size() > 0)
6361
return lMusicas;
6462
else
6563
throw new MusicNotFoundException(MusicNotFoundException.NotFoundByName);
66-
}
67-
64+
}
65+
6866
public ArrayList<Musica> findByAuthor(String autor) throws MusicNotFoundException, SQLException {
69-
fetch();
70-
ArrayList<Musica> lMusicas = new ArrayList<Musica>();
71-
67+
ArrayList<Musica> lMusicas = dao.selectByAuthor(autor);
68+
69+
/*
7270
for (Musica musica : lstMusica) {
7371
if (musica.getAutor().toLowerCase().startsWith(autor.toLowerCase()))
7472
lMusicas.add(musica);
7573
}
74+
*/
7675

7776
if (lMusicas.size() > 0)
7877
return lMusicas;
@@ -83,7 +82,7 @@ public ArrayList<Musica> findByAuthor(String autor) throws MusicNotFoundExceptio
8382
/*
8483
public Musica findPerf(String nome, String autor) throws MusicNotFoundException, SQLException {
8584
fetch();
86-
85+
8786
for (Musica musica : lstMusica) {
8887
if (musica.getAutor().equalsIgnoreCase(autor)
8988
&& musica.getNome().equalsIgnoreCase(nome))
@@ -97,21 +96,12 @@ public Musica findPerf(String nome, String autor) throws MusicNotFoundException,
9796
public void remove(String nome, String autor) throws MusicNotFoundException, SQLException {
9897
remove(find(nome, autor));
9998
}
100-
99+
101100
public void remove(Musica e) throws MusicNotFoundException, SQLException {
102-
lstMusica.remove(e);
103101
dao.remove(e);
104102
}
105103

106-
private void fetch() throws SQLException {
107-
try {
108-
this.lstMusica = dao.get();
109-
} catch (SQLException e) {
110-
throw new SQLException("Erro: Impossível receber valores do banco de dados.");
111-
}
112-
}
113-
114-
public ArrayList<Musica> getList() {
115-
return lstMusica;
104+
public ArrayList<Musica> getList() throws SQLException {
105+
return dao.select();
116106
}
117107
}

MVCMusicas/src/model/DAO/MusicaDAO.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,48 @@ public void remove(Musica e) throws SQLException {
6161
remove(e.getNome(), e.getAutor());
6262
}
6363

64-
public ArrayList<Musica> get() throws SQLException {
65-
ArrayList<Musica> lstMusicas = new ArrayList<Musica>();
66-
64+
public ArrayList<Musica> select() throws SQLException {
6765
PreparedStatement smt = connection.prepareStatement("SELECT * FROM JMusica");
6866
ResultSet rs = smt.executeQuery();
67+
ArrayList<Musica> lMusicas = RSetToList(rs);
68+
smt.close();
69+
70+
return lMusicas;
71+
}
72+
73+
public ArrayList<Musica> selectByName(String name) throws SQLException {
74+
PreparedStatement smt = connection.prepareStatement("SELECT * FROM JMusica WHERE LOWER(nome) LIKE ?");
75+
smt.setString(1, name + "%");
76+
ResultSet rs = smt.executeQuery();
77+
ArrayList<Musica> lMusicas = RSetToList(rs);
78+
smt.close();
79+
80+
return lMusicas;
81+
}
82+
83+
public ArrayList<Musica> selectByAuthor(String author) throws SQLException {
84+
PreparedStatement smt = connection.prepareStatement("SELECT * FROM JMusica WHERE LOWER(autor) LIKE ?");
85+
smt.setString(1, author + "%");
86+
ResultSet rs = smt.executeQuery();
87+
ArrayList<Musica> lMusicas = RSetToList(rs);
88+
smt.close();
89+
90+
return lMusicas;
91+
}
92+
93+
public ArrayList<Musica> selectByAuthorANDName(String name, String author) throws SQLException {
94+
PreparedStatement smt = connection.prepareStatement("SELECT * FROM JMusica WHERE LOWER(nome) LIKE ? AND LOWER(autor) LIKE ?");
95+
smt.setString(1, name.toLowerCase() + "%");
96+
smt.setString(2, author.toLowerCase() + "%");
97+
ResultSet rs = smt.executeQuery();
98+
ArrayList<Musica> lMusicas = RSetToList(rs);
99+
100+
smt.close();
101+
return lMusicas;
102+
}
103+
104+
public static ArrayList<Musica> RSetToList(ResultSet rs) throws SQLException {
105+
ArrayList<Musica> lstMusicas = new ArrayList<Musica>();
69106

70107
while (rs.next()) {
71108
String nome = rs.getString("nome");
@@ -77,7 +114,7 @@ public ArrayList<Musica> get() throws SQLException {
77114
lstMusicas.add(new Musica(nome, album, autor, duracao, dataPb));
78115
}
79116

80-
smt.close();
117+
rs.close();
81118
return lstMusicas;
82119
}
83120
}

MVCMusicas/src/view/FrmMusica.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ private void fillTable(ArrayList<Musica> lMusicas) {
131131
}
132132

133133
private void fillTable() {
134-
fillTable(lstMusica.getList());
134+
try {
135+
fillTable(lstMusica.getList());
136+
} catch (SQLException e) {
137+
JOptionPane.showInputDialog(null, "Erro no SQL: " + e.getMessage());
138+
}
135139
limparBusca();
136140
}
137141

0 commit comments

Comments
 (0)