Dúvida

31/10/2007

1

Galera, blz?
Seguinte... eu to tentando fazer uma agenda eletroncia com conexão no BD do Access (.mdb).
Ja fiz isso com um gerenciador de estacionamento, porém não consigo fazer rodar essa joça.... fica dando erro de conexão, e além disso tem um monte de outros erros que aparecem que eu num consigo entender pq.... foi por isso que eu usei /* */ em alguns trechos.... alguém pode me ajudar?

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

public class Agenda extends JFrame implements ActionListener{
final String url = ´jdbc:odbc:projeto_agenda´;
private JPanel jPanel1 = new JPanel();
private JButton jButton1=new JButton();
private JButton jButton2=new JButton();
private JButton jButton3=new JButton();
private JButton jButton4=new JButton();
private JButton jButton5=new JButton();
private JButton jButton_Primeiro=new JButton();
private JButton jButton_Ultimo=new JButton();
private JButton jButton_Anterior=new JButton();
private JButton jButton_Proximo=new JButton();

private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
private JLabel jLabel8 = new JLabel();
private JLabel jLabel9 = new JLabel();
private JLabel jLabel10 = new JLabel();
private JLabel jLabel11 = new JLabel();

private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JTextField jTextField4 = new JTextField();
private JTextField jTextField5 = new JTextField();
private JTextField jTextField6 = new JTextField();
private JTextField jTextField7 = new JTextField();
private JTextField jTextField8 = new JTextField();
private JTextField jTextField9 = new JTextField();
private JTextField jTextField10 = new JTextField();
private JTextField jTextField11 = new JTextField();

private Connection con;
private Statement meuStatement;
private ResultSet meuResultSet;

public Agenda(){
super(´Agenda Telefonica´);
getContentPane().setLayout(null);
addWindowListener(new WindowAdapter(){
public void windowClosing (WindowEvent evt){
System.exit(0);
}
});
jPanel1.setLayout(null);
jPanel1.setBorder(new javax.swing.border.EtchedBorder());
jLabel1.setText(´Nome´);
jPanel1.add(jLabel1);
jLabel1.setBounds(40,10,100,16);
jPanel1.add(jTextField1);
jTextField1.setBounds(100,10,150,20);

jLabel2.setText(´Endereço´);
jPanel1.add(jLabel2);
jLabel2.setBounds(40,35,300,16);
jPanel1.add(jTextField2);
jTextField2.setBounds(100,33,250,20);

jLabel3.setText(´CEP´);
jPanel1.add(jLabel3);
jLabel3.setBounds(360,35,70,16);
jPanel1.add(jTextField3);
jTextField3.setBounds(390,33,70,20);

jLabel4.setText(´Cidade´);
jPanel1.add(jLabel4);
jLabel4.setBounds(40,60,100,16);
jPanel1.add(jTextField4);
jTextField4.setBounds(100,60,130,20);

jLabel5.setText(´Estado´);
jPanel1.add(jLabel5);
jLabel5.setBounds(245,60,70,16);
jPanel1.add(jTextField5);
jTextField5.setBounds(310,60,150,20);

jLabel6.setText(´País´);
jPanel1.add(jLabel6);
jLabel6.setBounds(40,87,100,16);
jPanel1.add(jTextField6);
jTextField6.setBounds(100,87,130,20);

jLabel7.setText(´Telefone´);
jPanel1.add(jLabel7);
jLabel7.setBounds(40,115,100,16);
jPanel1.add(jTextField7);
jTextField7.setBounds(100,115,130,20);

jLabel8.setText(´Telefone 2´);
jPanel1.add(jLabel8);
jLabel8.setBounds(245,115,100,16);
jPanel1.add(jTextField8);
jTextField8.setBounds(310,115,150,20);

jLabel9.setText(´Celular´);
jPanel1.add(jLabel9);
jLabel9.setBounds(40,145,100,16);
jPanel1.add(jTextField9);
jTextField9.setBounds(100,145,130,20);

jLabel10.setText(´Celular 2´);
jPanel1.add(jLabel10);
jLabel10.setBounds(245,145,100,16);
jPanel1.add(jTextField10);
jTextField10.setBounds(310,145,150,20);

jButton1.setText(´Incluir´);
jButton1.addActionListener(this);
jPanel1.add(jButton1);
jButton1.setBounds(90,220,73,26);

jButton2.setText(´Alterar´);
jButton2.addActionListener(this);
jPanel1.add(jButton2);
jButton2.setBounds(168,220,73,26);

jButton3.setText(´Excluir´);
jButton3.addActionListener(this);
jPanel1.add(jButton3);
jButton3.setBounds(247,220,73,26);

jButton4.setText(´Consultar´);
jButton4.addActionListener(this);
jPanel1.add(jButton4);
jButton4.setBounds(325,220,90,26);

jButton5.setText(´Pesquisar´);
jButton5.addActionListener(this);
jPanel1.add(jButton5);
jButton5.setBounds(365,10,95,20);


/*jButton_Primeiro.setText(´<<´);
jButton_Primeiro.addActionListener(this);
jPanel1.add(jButton_Primeiro);
jButton_Primeiro.setBounds(70,190,100,26);

jButton_Anterior.setText(´<´);
jButton_Anterior.addActionListener(this);
jPanel1.add(jButton_Anterior);
jButton_Anterior.setBounds(174,190,100,26);

jButton_Proximo.setText(´>´);
jButton_Proximo.addActionListener(this);
jPanel1.add(jButton_Proximo);
jButton_Proximo.setBounds(279,190,100,26);

jButton_Ultimo.setText(´>>´);
jButton_Ultimo.addActionListener(this);
jPanel1.add(jButton_Ultimo);
jButton_Ultimo.setBounds(380,190,70,26);*/

getContentPane().add(jPanel1);
jPanel1.setBounds(10,10,480,290);
try{
con = DriverManager.getConnection(url);
meuStatement=
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
meuResultSet = meuStatement.executeQuery(´SELECT * FROM agenda´);
}
catch (SQLException ex){
JOptionPane.showMessageDialog (null, ´Problemas na Base´,´ERRO´,0);
}
setSize (510,330);
show();
}
public void actionPerformed (ActionEvent e){

/*if(e.getSource().equals(jButton_Primeiro)){

try{
if (meuResultSet.first()){
jTextField1.setText(meuResultSet.getString(´Nome´))
jTextField2.setText(meuResultSet.getString(´Endereço´))
jTextField3.setText(meuResultSet.getString(´CEP´))
jTextField4.setText(meuResultSet.getString(´Cidade´))
jTextField5.setText(meuResultSet.getString(´Estado´))
jTextField6.setText(meuResultSet.getString(´País´))
jTextField7.setText(meuResultSet.getString(´Telefone´))
jTextField8.setText(meuResultSet.getString(´Telefne 2´))
jTextField9.setText(meuResultSet.getString(´Celular´))
jTextField10.setText(meuResultSet.getString(´Celular 2´))
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog (null, ´Problemas na Base´,´ERRO´,0);
}
}
if (e.getSource().equals(jButton_Anterior)){
try{
if(meuResultSet.previous()){
/*jTextField1.setText(meuResultSet.getString(´Nome´))
jTextField2.setText(meuResultSet.getString(´Endereço´))
jTextField3.setText(meuResultSet.getString(´CEP´))
jTextField4.setText(meuResultSet.getString(´Cidade´))
jTextField5.setText(meuResultSet.getString(´Estado´))
jTextField6.setText(meuResultSet.getString(´País´))
jTextField7.setText(meuResultSet.getString(´Telefone´))
jTextField8.setText(meuResultSet.getString(´Telefne 2´))
jTextField9.setText(meuResultSet.getString(´Celular´))
jTextField10.setText(meuResultSet.getString(´Celular 2´))*/
/*}
}
catch (SQLException ex){
JOptionPane.showMessageDialog (null,´Problemas na Base´,´ERRO´,0);
}
}
if (e.getSource().equals(jButton_Proximo)){
try{
if(meuResultSet.next()){
/*jTextField1.setText(meuResultSet.getString(´Nome´))
jTextField2.setText(meuResultSet.getString(´Endereço´))
jTextField3.setText(meuResultSet.getString(´CEP´))
jTextField4.setText(meuResultSet.getString(´Cidade´))
jTextField5.setText(meuResultSet.getString(´Estado´))
jTextField6.setText(meuResultSet.getString(´País´))
jTextField7.setText(meuResultSet.getString(´Telefone´))
jTextField8.setText(meuResultSet.getString(´Telefne 2´))
jTextField9.setText(meuResultSet.getString(´Celular´))
jTextField10.setText(meuResultSet.getString(´Celular 2´))*/
/*}
}
catch (SQLException ex){
JOptionPane.showMessageDialog (null,´Problemas na Base´,´ERRO´,0);
}
}
if (e.getSource().equals(jButton_Ultimo)){
try{
if(meuResultSet.last()){
/*jTextField1.setText(meuResultSet.getString(´Nome´))
jTextField2.setText(meuResultSet.getString(´Endereço´))
jTextField3.setText(meuResultSet.getString(´CEP´))
jTextField4.setText(meuResultSet.getString(´Cidade´))
jTextField5.setText(meuResultSet.getString(´Estado´))
jTextField6.setText(meuResultSet.getString(´País´))
jTextField7.setText(meuResultSet.getString(´Telefone´))
jTextField8.setText(meuResultSet.getString(´Telefne 2´))
jTextField9.setText(meuResultSet.getString(´Celular´))
jTextField10.setText(meuResultSet.getString(´Celular 2´))*/
/*}
}
catch (SQLException ex){
JOptionPane.showMessageDialog (null,´Problemas na Base´,´ERRO´,0);
}*/
//}
if (e.getSource().equals(jButton1)){
try{
Statement st = con.createStatement();
st.executeUpdate (´INSERT INTO agenda (Nome,Endereco,Cep,Cidade,Estado,Pais,Telefone,Telefone2,Celular,Celular2) VALUES (´´+jTextField1.getText()+´´,´´+jTextField2.getText()+´´,´´+jTextField3.getText()+´´,´´+jTextField4.getText()+´´,´´+jTextField5.getText()+´´,´´+jTextField6.getText()+´´,´´+jTextField7.getText()+´´,´´+jTextField8.getText()+´´,´´+jTextField9.getText()+´´,´´+jTextField10.getText()+´´)´);
JOptionPane.showMessageDialog(null, ´Contato adicionado´, ´Info´,1);
st.close();
/*jTextField1.setText(´´);
jTextField2.setText(´´);
jTextField3.setText(´´);
jTextField4.setText(´´);
jTextField5.setText(´´);
jTextField6.setText(´´);
jTextField7.setText(´´);
jTextField8.setText(´´);
jTextField9.setText(´´);
jTextField10.setText(´´);*/
}
catch (SQLException ex){
JOptionPane.showMessageDialog(null,´Problemas na Base´,´ERRO´,0);
}
}
if (e.getSource().equals(jButton2)){
if(jTextField1.getText()==´´){
JOptionPane.showMessageDialog(null,´Insira o nome para poder alterar´);
}else{
try{
Statement st = con.createStatement();
//st.executeUpdate(´UPDATE agenda set Nome=´´+jTextField1.getText()+´´ WHERE Nome=´+Nome);
JOptionPane.showMessageDialog(null,´Dados alterados com Sucesso!´);
st.close();
/*jTextField1.setText(´´);
jTextField2.setText(´´);
jTextField3.setText(´´);
jTextField4.setText(´´);
jTextField5.setText(´´);
jTextField6.setText(´´);
jTextField7.setText(´´);
jTextField8.setText(´´);
jTextField9.setText(´´);
jTextField10.setText(´´);*/
}
catch (SQLException ex){
JOptionPane.showMessageDialog (null, ´Problemas na Base´,´ERRO´,0);
}
}
}
if(e.getSource().equals(jButton3)){
if(jTextField1.getText()==´´){
JOptionPane.showMessageDialog(null,´Insira o nome para deletar´);
}else{
try{
Statement st = con.createStatement();
//st.executeUpdate(´DELETE * FROM agenda WHERE Nome = ´+Nome);
JOptionPane.showMessageDialog(null, ´Contato deletado com sucesso!´);
st.close();
/*jTextField1.setText(´´);
jTextField2.setText(´´);
jTextField3.setText(´´);
jTextField4.setText(´´);
jTextField5.setText(´´);
jTextField6.setText(´´);
jTextField7.setText(´´);
jTextField8.setText(´´);
jTextField9.setText(´´);
jTextField10.setText(´´);*/
}
catch (SQLException ex){
JOptionPane.showMessageDialog(null,´Erro na Base´,´ERRO´,0);
}
}
}
if (e.getSource().equals(jButton5)){
if (jTextField1.getText()==´´){
JOptionPane.showMessageDialog(null,´Insira o Nome´);
}else{
try{
Statement st= con.createStatement();
ResultSet rs = st.executeQuery (´SELECT * FROM agenda WHERE Nome=´+jTextField1.getText());
if (rs.next()){
/*jTextField1.setText(´Nome´));
jTextField2.setText(´Endereço´));
jTextField3.setText(´CEP´));
jTextField4.setText(´Cidade´));
jTextField5.setText(´Estado´));
jTextField6.setText(´País´));
jTextField7.setText(´Telefone´));
jTextField8.setText(´Telefone 2´));
jTextField9.setText(´Celular´));
jTextField10.setText(´Celular 2´));*/
rs.close();
}else{
JOptionPane.showMessageDialog(null,´Não existem cadastros´,´Info´,1);
}
}
catch (SQLException ex){
JOptionPane.showMessageDialog(null,´Problemas na Base´,´ERRO´,0);
}
}
}
}
public static void main (String [] args){
try{
Class.forName(´sun.jdbc.odbc.JdbcOdbcDriver´);
}
catch (Exception ex){
System.out.println(´Driver JDBC-ODBC não encontrado!´);
}
Agenda app = new Agenda();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Responder
×
+1 DevUP
Acesso diário, +1 DevUP
Parabéns, você está investindo na sua carreira