Me ajudem urgente

8 respostas
H

Galera boa noite criei um simples aplicativo mas não consigo inserir no banco os dados digitados no JTextField ja fiz de tudo o erro esta abaixo

Called Compiler C:\J2SDK1~1.0\bin\javac.exe-
-Target File: C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java-
C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: ‘)’ expected
st.executeUpdate(“INSERT INTO cadastro (nome, telefone, cidade) VALUES (’“texto1.getText()”’,’“texto2.getText()”’,’“texto3.getText()”’)”);
^
1 error

abaixo o codigo me ajudem obrigado

import javax.swing.<em>;

import java.awt.</em>;

import java.awt.event.<em>;

import java.sql.</em>;
public class ExemploDeMenu extends JFrame{

JMenuBar barra;

JMenu opcoes;

JMenuItem abrir,sair;

Inicial segundajanela;
public ExemploDeMenu(){

super(“Exemplo de Menus”);

Container tela = getContentPane();
barra = new JMenuBar();

setJMenuBar(barra);

opcoes = new JMenu(“Opções”);

barra.add(opcoes);
abrir = new JMenuItem(“NOVO”);

sair = new JMenuItem(“SAIR”);

abrir.setMnemonic(KeyEvent.VK_A);

sair.setMnemonic(KeyEvent.VK_S);
sair.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent event){

int status = JOptionPane.showConfirmDialog(null,Deseja realmente fechar o programa?,Mensagem de saída,JOptionPane.YES_NO_OPTION);

if (status == JOptionPane.YES_OPTION)

{

System.exit(0);

}

}});

abrir.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

segundajanela = new Inicial(null,Segunda Janela,true);

segundajanela.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

segundajanela.setVisible(true); }});
opcoes.addSeparator();

opcoes.add(abrir);

opcoes.add(sair);

setSize(500,300);
setVisible(true);

}

public static void main (String args[]){

ExemploDeMenu app = new ExemploDeMenu();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
private class Inicial extends JDialog{

JButton inserir;

JTextField texto1,texto2,texto3;

JLabel rotulo1,rotulo2,rotulo3;

public Inicial(Frame owner, String title,boolean modal){

super(owner,title,modal);

Container tela1 = getContentPane();
rotulo1=new JLabel(“Nome”);

rotulo2=new JLabel(“Telefone”);

rotulo3=new JLabel(“Cidade”);
texto1=new JTextField(15);

texto2=new JTextField(15);

texto3=new JTextField(15);

inserir = new JButton(“Guardar”);
rotulo1.setBounds(50,20,100,20);texto1.setBounds(150,20,100,20);

rotulo2.setBounds(50,40,100,20);texto2.setBounds(150,40,100,20);

rotulo3.setBounds(50,60,100,20);texto3.setBounds(150,60,100,20);

inserir.setBounds(120,200,100,20);

inserir.setMnemonic(KeyEvent.VK_I);

TBinserir tinserir = new TBinserir();
inserir.addActionListener(tinserir);

tela1.add(rotulo1);tela1.add(texto1);

tela1.add(rotulo2);tela1.add(texto2);

tela1.add(rotulo3);tela1.add(texto3);
tela1.add(inserir);

setSize(500,350);

setLocationRelativeTo(null);} }

public class TBinserir implements ActionListener{
public void actionPerformed(ActionEvent evento){

try{

Class.forName(com.mysql.jdbc.Driver);

Connection con;

con=DriverManager.getConnection(jdbc:mysql://localhost/teste,root,"");

Statement st = con.createStatement();

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’“texto1.getText()”’,’“texto2.getText()”’,’“texto3.getText()”’));
JOptionPane.showMessageDialog(null,“Dados Criado Com Sucesso”,“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);

}

}

}
}

8 Respostas

T

Troque

st.executeUpdate("INSERT INTO cadastro (nome, telefone, cidade) VALUES ('"texto1.getText()"','"texto2.getText()"','"texto3.getText()"')");

por

st.executeUpdate("INSERT INTO cadastro (nome, telefone, cidade) VALUES ('"
    + texto1.getText()
    + "','"
    + texto2.getText()
    + "','"
    + texto3.getText()
    + "')");
Paezani

Bom colega vc deve ser novo por aqui, sempre q possível coloque o código entre as tags [code], para melhor visualização código fonte...

O problema pode estar na instrução sql, tente assim:

st.executeUpdate("INSERT INTO cadastro (nome, telefone, cidade) VALUES (' "+texto1.getText()+" ',' "+texto2.getText()+" ',' "+texto3.getText()+" ') ");

H

novos erros apareceram

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto1

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto2

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto3

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

3 errors
fantomas

Coloque os sinais de concatenação na linha, assim:

st.executeUpdate("INSERT INTO cadastro (nome, telefone, cidade) VALUES ('"+texto1.getText()+"','"+texto2.getText()+"','"+texto3.getText()+"')");

Acho que será menos um erro.

P.S Utilize as tags code

flws e bem vindo ao guj

thiago.correa

highpowerinfo:
novos erros apareceram

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto1

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto2

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

C:\DOCUME~1\usuarios\Desktop\Java\PROGRA~1\ExemploDeMenu.java:104: cannot resolve symbol

symbol  : variable texto3

location: class ExemploDeMenu.TBinserir

st.executeUpdate(INSERT INTO cadastro (nome, telefone, cidade) VALUES (’”+ texto1.getText()+ “’,’”+ texto2.getText()+ “’,’”+texto3.getText()+"’)");

^

3 errors

cannot resolve symbol -> não foi possível resolver (encontrar, localizar etc…) o simbolo

Certifique-se que essa variável está visível para o método que você está executando e tentando usá-la algo do tipo não vai funcionar

void metodoA() {
    JTextField a = new JTextField();
    ....
}
void metodoB() {
    String b = a.getText();
    ....
}
H
Galera ta complicado num vai de geito nenhum
fantomas

Não vai dar uma frouxo agora, vai?

Coloca o código na integra ai pra gente ver de novo.

Mas use as tags code.

flws

H
Quero agradecer a todos pela força e em especial agradecer o Thiago correa problema resolvido desculpem a minha iguinorancia no uso do forum começei ele hoje por volta de 1 hora com tempo corrigirei meus abtos Forte abraçoss
Criado 29 de junho de 2009
Ultima resposta 29 de jun. de 2009
Respostas 8
Participantes 5