Problema com Botão

4 respostas
R
Como faço para o codigo abaixo incluir um texto que vai ser digitado, ja tentei mudar o que eu sabia e não sabia mas só aparece o botao de sair.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.sql.*;
public class Estacionamentop4 extends JFrame implements ActionListener{
Connection Con;
Statement Stm;
JButton BtnIncluir, BtnSair;
JTextField txtCodigo, txtNome, txtEmail, txtTelefone;
String strMsg = "";
public static void main(String[] args){
	new Estacionamentop4();
}
public Estacionamentop4(){
	Formulario();
	int intRes = Conectar();
	if(intRes == 0){
		show();
		return;
	}
	if(intRes == 1) strMsg="Driver não encontrado";
	if(intRes == 2) strMsg="Erro de Conexão com o Banco de Dados";
	JOptionPane.showMessageDialog(null, strMsg,"Erro",0);
	System.exit(0);
}
public void Formulario(){
	setTitle("Programa de Inclusão");
	setSize(new Dimension(300,200));
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	getContentPane().setBackground(new Color (0,250,0));
	Image Ico;
	Ico = Toolkit.getDefaultToolkit().getImage("exemplo.gif");
	setIconImage(Ico);
	txtCodigo = new JTextField("");
	txtNome = new JTextField("");
	txtEmail = new JTextField("");
	txtTelefone = new JTextField("");
	
	BtnIncluir = new JButton("Incluir");
	BtnIncluir.setMnemonic('I');
	BtnIncluir.addActionListener(this);
	
	BtnSair = new JButton("Sair");
	BtnSair.setMnemonic('S');
	BtnSair.addActionListener(this);
	
	getContentPane().add(new JLabel("Codigo"));
	getContentPane().add(new JLabel("Nome"));
	getContentPane().add(txtCodigo);
	getContentPane().add(txtNome);
	
	getContentPane().add(new JLabel("E-mail"));
	getContentPane().add(new JLabel("Telefone"));
	getContentPane().add(txtEmail);
	getContentPane().add(txtTelefone);
	
	getContentPane().add(BtnIncluir);
	getContentPane().add(BtnSair);
	
	addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e){
			Desconectar();}});
}
public void Desconectar(){
	try{
		Stm.close();
		Con.close();
	}
	catch(SQLException sqle){}
}
public int Conectar(){
	try{
		Class.forName("com.mysql.jdbc.Driver");
	}
	catch(ClassNotFoundException cnfe){
		return 1;
	}
	try{
		Con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","011095");
		Stm = Con.createStatement();
		return 0;
	}
	catch (SQLException sqle){
		return 2;
	}
}
public void actionPerformed(ActionEvent e){
	if(e.getSource()==BtnSair){
		Desconectar();
		System.exit(0);
	}
	if(e.getSource()==BtnIncluir){
		Incluir_Dados();
	}
}
public void Incluir_Dados(){
	String strSQL = "";
	strSQL = "Insert into Amigos (Codigo,";
	strSQL = strSQL + "Nome, Email, Telefone) values (";
	strSQL = strSQL + txtCodigo.getText().trim() + ",'";
	strSQL = strSQL + txtNome.getText().trim() + ",'";
	strSQL = strSQL + txtEmail.getText().trim() + ",'";
	strSQL = strSQL + txtTelefone.getText().trim() + "')";
	try{
		Stm.executeUpdate(strSQL);
		Con.commit();
		strMsg = "Inclusão Efetuada com Sucesso";
		JOptionPane.showMessageDialog(null, strMsg,"Atenção",1);
	}
	catch(SQLException sqle){
		strMsg = "Ocorreu erro! \nCodigo:"+
	sqle.getErrorCode()+"\nMensagem:"+sqle.getMessage();
		JOptionPane.showMessageDialog(null, strMsg,"Erro",0);
	}
}
}

4 Respostas

F

Faltou vc definir o layout…
por exemplo

coloca isso na linha 47

setLayout(new FlowLayout());
R

como eu boto um tamanho para as caixas de texto

txtCodigo = new JTextField(""); txtNome = new JTextField(""); txtEmail = new JTextField(""); txtTelefone = new JTextField("");

pq elas estão com 1px Oo

R

Pode me ajudar com esse erro

O codigo q inclui é esse

public void Incluir_Dados(){ String strSQL = ""; strSQL = "Insert into Amigos (codigo,"; strSQL = strSQL + "name, email, telefone) values ("; strSQL = strSQL + txtCodigo.getText().trim() + ",'"; strSQL = strSQL + txtNome.getText().trim() + ",'"; strSQL = strSQL + txtEmail.getText().trim() + ",'"; strSQL = strSQL + txtTelefone.getText().trim() + "')"; try{ Stm.executeUpdate(strSQL); Con.commit(); strMsg = "Inclusão Efetuada com Sucesso"; JOptionPane.showMessageDialog(null, strMsg,"Atenção",1); } catch(SQLException sqle){ strMsg = "Ocorreu erro! \nCodigo:"+ sqle.getErrorCode()+"\nMensagem:"+sqle.getMessage(); JOptionPane.showMessageDialog(null, strMsg,"Erro",0); } }

WellingtonRamos

rodrigolujan:
Pode me ajudar com esse erro …

O codigo q inclui é esse

public void Incluir_Dados(){ String strSQL = ""; strSQL = "Insert into Amigos (codigo,"; strSQL = strSQL + "name, email, telefone) values ("; strSQL = strSQL + txtCodigo.getText().trim() + ",'"; strSQL = strSQL + txtNome.getText().trim() + ",'"; strSQL = strSQL + txtEmail.getText().trim() + ",'"; strSQL = strSQL + txtTelefone.getText().trim() + "')"; try{ Stm.executeUpdate(strSQL); Con.commit(); strMsg = "Inclusão Efetuada com Sucesso"; JOptionPane.showMessageDialog(null, strMsg,"Atenção",1); } catch(SQLException sqle){ strMsg = "Ocorreu erro! \nCodigo:"+ sqle.getErrorCode()+"\nMensagem:"+sqle.getMessage(); JOptionPane.showMessageDialog(null, strMsg,"Erro",0); } }


O erro que você está tendo é de sintaxe, ou seja, a instrução SQL gerada por teu código está errada.
Imprima a instrução e verifique se não se esqueceu de algo (ou colocou algo a mais)

Criado 17 de agosto de 2011
Ultima resposta 18 de ago. de 2011
Respostas 4
Participantes 3