Boa noite, esse é meu primeiro post e segue uma dúvida: como mostro o resultado da query abaixo???
Estou dando os meus primeiros passos em Java, antes só programava em PHP. Fiz atá ai sozinho, mas não consigo exibir o resultado na aplicação gráfica.
Valeu!!! Agradeço mesmo.
import java.awt.<em>;
import java.awt.event.</em>;
import javax.swing.<em>;
import java.lang.</em>;
import java.net.URL;
import java.sql.<em>;
import <a href="http://java.io">java.io</a>.</em>;
public class BuscaNome extends Frame implements ActionListener {
JLabel l1;
JTextField t1;
JButton b1;
public BuscaNome() {
setLayout (new GridLayout(10,5));
l1 = new JLabel("Nome do cliente: ");
t1 = new JTextField(50);
b1 = new JButton("Buscar");
b1.addActionListener(this);
add(l1);
add(t1);
add(b1);
}
public void actionPerformed(ActionEvent event) {
janela ap = new janela();
ap.sair();
}
public static void main(String args[]) throws IOException{
BuscaNome janela = new BuscaNome();
janela.setTitle("busca de cliente");
janela.pack();
janela.show();
}
class janela{
janela(){
}
public void sair(){
try{
//conexão com o banco de dados
String url = "jdbc:mysql://localhost:3306/banco";
String user = "root";
String password = "root";
Connection conexao = DriverManager.getConnection(url, user, password);
Class.forName("com.mysql.jdbc.Driver");
//inicia a conexão
Statement st = conexao.createStatement();
//SQL
String sql = "SELECT * FROM teste WHERE nome LIKE '"+ t1.getText() +"' ";
Statement stat = conexao.createStatement();
ResultSet set = stat.executeQuery(sql);
while(set.next()){
String titulo = set.getString("NomeAg");
System.out.println(titulo);
st.close();
conexao.close();
}
catch (Exception e) {
e.printStackTrace();
return;
}
System.exit(0);
}
}
}
e a tabela para teste:
–
– Estrutura da tabela teste
CREATE TABLE IF NOT EXISTS teste (
id int(10) NOT NULL auto_increment,
nome varchar(150) default NULL,
Telefone varchar(10) default NULL,
Cidade varchar(10) default NULL,
Idade varchar(10) default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;