Escolar Documentos
Profissional Documentos
Cultura Documentos
Este tutorial apresenta o cdigo fonte da srie do tutorial de programao desktop utilizando a linguagem
de programao Java. O objetivo do tutorial facilitar o desenvolvimento de um bsico sistema desktop.
Segue a baixo e ferramentas utilizadas durante o desenvolvimento.
Ferramentas:
Banco de Dados: Mysql
IDE: Eclipse JUNO
Plug-in IDE: WINDOWBUILDER
Verso Java: JRE6
Connector Mysql: mysql-connector-java-5.1.7-bin.jar
Obs*: O script auxilia apenas as vdeos aula de cadastro, atualizao e excluso de dados.
Segue abaixo os cdigos fonte da classe bin ClienteBin.java, classe conexao conexao.java,
classe control ClienteControl.java e as classesClienteGui.java e Principal.java
sistema.bin ClienteBin.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package sistema.bin;
public class ClienteBin {
private
private
private
private
private
private
String
String
String
String
String
String
nome;
email;
telefone;
endereco;
cidade;
estado;
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
sistema.conexao conexao.java
?
package sistema.conexao;
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class conexao {
Connection con;
private Connection oConn;
private Statement sStmt;
public conexao(){
}
public Connection abrirBDConn(){
try{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/banco";
con = DriverManager.getConnection(url,"root","aluno");
System.out.println("Conexao efetuada com sucesso");
return con;
}
catch(Exception e){
System.out.println("Erro ao abrir conexao com banco:");
e.printStackTrace();
return null;
}
}
public void fecharBDConn(){
try{
con.close();
System.out.println("Conexao finalizada com sucesso");
}catch(Exception e){
System.out.println("Erro ao fechar conexao com banco" + e.getMessage());
}
}
}
sistema.control ClienteControl.java
?
1
2
3
4
5
6
7
8
9
10
11
package sistema.control;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import sistema.bin.ClienteBin;
import sistema.conexao.conexao;
public class ClienteControl {
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
}
return retorno;
}
public void BuscarDados(int codigo,ClienteBin CliBin) {
conexao banco = new conexao();
try {
Connection ExConn = (Connection) banco.abrirBDConn();
Statement stmt = (Statement) ExConn.createStatement();
String sSQL = "SELECT * FROM banco.cliente WHERE idCliente = "+codigo;
ResultSet rs = stmt.executeQuery(sSQL);
while(rs.next())
{
CliBin.setCodigo(rs.getInt("idCliente"));
CliBin.setNome(rs.getString("nome"));
CliBin.setEndereco(rs.getString("endereco"));
CliBin.setEstado(rs.getString("estado"));
CliBin.setCidade(rs.getString("cidade"));
CliBin.setTelefone(rs.getString("telefone"));
CliBin.setEmail(rs.getString("email"));
}
stmt.close();
banco.fecharBDConn();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"Os dados no puderam ser encontrado!!!")
}
}
}
sistema.gui ClienteGui.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sistema.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JOptionPane;
javax.swing.JPanel;
javax.swing.border.EmptyBorder;
javax.swing.JLabel;
java.awt.Font;
javax.swing.JTextField;
javax.swing.JButton;
import sistema.bin.ClienteBin;
import sistema.control.ClienteControl;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ClienteGui extends JFrame {
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
private
private
private
private
private
private
private
JPanel contentPane;
JTextField jtfNome;
JTextField jtfEmail;
JTextField jtfTelefone;
JTextField jtfEndereco;
JTextField jtfCidade;
JTextField jtfEstado;
/**
* Create the frame.
*/
public ClienteGui() {
setTitle("Cliente");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 509, 365);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Cadastro de Clientes");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel.setBounds(27, 11, 233, 45);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Nome:");
lblNewLabel_1.setBounds(27, 79, 46, 14);
contentPane.add(lblNewLabel_1);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(27, 104, 46, 14);
contentPane.add(lblEmail);
JLabel lblTelefone = new JLabel("Telefone:");
lblTelefone.setBounds(27, 129, 71, 14);
contentPane.add(lblTelefone);
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
});
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
}
});
btnSair.setBounds(286, 283, 89, 23);
contentPane.add(btnSair);
}
sistema.gui Principal.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
package sistema.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import
import
import
import
import
import
import
import
javax.swing.JFrame;
javax.swing.JPanel;
javax.swing.border.EmptyBorder;
javax.swing.JMenuBar;
javax.swing.JMenu;
javax.swing.JMenuItem;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;
42
43
44
45
46
47
48
49
50
51
52
53