quarta-feira, 17 de dezembro de 2008

Teste de Conexão com Banco de Dados MySQL com JFrame

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class TestaConexao extends JFrame{
JButton botao;
public TestaConexao(){
super("Testando a Conexão...");
Container tela = getContentPane();
setLayout(null);
botao = new JButton("Verificar Conexão");
botao.setBounds(50,50,150,20);
botao.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
String url = "jdbc:mysql://localhost:3306/agenda";
String usuario = "root";
String senha = "vertrigo";
Class.forName("com.mysql.jdbc.Driver");
Connection con;
con = DriverManager.getConnection(url,usuario,senha);
JOptionPane.showMessageDialog(null,"Conexão estabelecida","Mensagem do Programa",JOptionPane.INFORMATION_MESSAGE);
con.close();
} catch(Exception event){
JOptionPane.showMessageDialog(null,"Conexão não estabelecida","Mensagem do Programa",JOptionPane.ERROR_MESSAGE);
}
}}
);
tela.add(botao);
setSize(300, 150);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String args[]){
TestaConexao app = new TestaConexao();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Nenhum comentário:

Postar um comentário