Alguém pode me ajudar tenho que entregar este trabalho sexta.
Estou com o seguinte problema:
Tenho uma classe agenda com o método main e um Switch Case chamando métodos da classe conexao que faz o link com o BD.
Na classe conexao tenho métodos genéricos para conectar no BD, inserir, excluir, listar, pesquisar e alterar dados na tabela do BD. Esta rodando “redondo” em modo texto. Preciso passar para modo gráfico e não estou conseguindo.
Duvidas: posso criar toda parte gráfica na classe agenda?
como faço para quando clicar no botão inserir por ex o mesmo chamar o método da classe conexao e preencher os dados na tabela?
Segue parte do código.
public static void main (String[] args){
Botoes janela = new Botoes();
janela.setTitle("AGENDA TELEFONICA v1.0");
janela.setSize(600,550);
janela.setLocation(100, 50);
janela.setLayout(null);
janela.setVisible(true);
String nome = " ", end = " ", tel = " ", email = " ";
int op=0;
do {
System.out.println("1 - Inserir os dados:");
System.out.println("2 - Listar os dados:");
System.out.println("3 - Excluir os dados:");
System.out.println("4 - Pesquisar os dados:");
System.out.println("5 - Alterar os dados:");
System.out.println("6 - Sair:");
Conexao link = new Conexao();
System.out.println("Digite a opção:");
op = Util.leiaInt();
switch (op) {
case 1 : {
link.conectar();
System.out.println("Favor digitar o nome:" );
nome = Util.leiaStr();
System.out.println("Favor digitar o endereco:" );
end = Util.leiaStr();
System.out.println("Favor digitar o Tel:" );
tel = Util.leiaStr();
System.out.println("Favor digitar o Email:" );
email = Util.leiaStr();
String Dados = ("Insert into tabela1 (Nome,End,Tel,Email) values ( '"+nome+"','"+end+"','"+tel+"','"+email+"')");
link.inserir(Dados);
break;
}
public class Conexao {
String Nome;
String list;
public Connection con ;
String strBanco = “jdbc:odbc:LinkBanco”;
public void conectar(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(strBanco, "", "");
Statement st = con.createStatement();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void inserir(String Dados){
try{
Statement st = con.createStatement();
st.executeUpdate(Dados);
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
public class Botoes extends JFrame implements ActionListener{
JButton btn1, btn2, btn3, btn4, btn5, btn6;
JTextField ed1, ed2, ed3, ed4, ed5, ed6;
JLabel rot1, rot2, rot3, rot4, rot5, rot6;
Botoes(){
btn1 = new JButton();
btn1.setText("INSERIR");
btn1.setSize(80, 30);
btn1.setLocation(10, 10);
ed1 = new JTextField();
ed1.setText("");
ed1.setLocation(90, 60);
ed1.setSize(450, 30);
rot1 = new JLabel();
rot1.setSize(70, 30);
rot1.setLocation(20, 60);
rot1.setText("NOME:");
getContentPane().add(btn1);
getContentPane().add(ed1);
getContentPane().add(rot1);
ed2 = new JTextField();
ed2.setText("");
ed2.setLocation(90, 100);
ed2.setSize(450, 30);
rot2 = new JLabel();
rot2.setSize(80, 30);
rot2.setLocation(20, 100);
rot2.setText("ENDERECO:");
getContentPane().add(ed2);
getContentPane().add(rot2);
ed3 = new JTextField();
ed3.setText("");
ed3.setLocation(90, 140);
ed3.setSize(450, 30);
rot3 = new JLabel();
rot3.setSize(80, 30);
rot3.setLocation(20, 140);
rot3.setText("TELEFONE:");
getContentPane().add(ed3);
getContentPane().add(rot3);
ed4 = new JTextField();
ed4.setText("");
ed4.setLocation(90, 180);
ed4.setSize(450, 30);
rot4 = new JLabel();
rot4.setSize(80, 30);
rot4.setLocation(20, 180);
rot4.setText("EMAIL:");
getContentPane().add(ed4);
getContentPane().add(rot4);
Alguem pode me dar umas dicas?
Obrigado