Vou postar abaixo o programa completo para vcs entenderem, acho que é mais facil né?
package br.sabe.poo.programa;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Programa {
private JFrame frame;
private JTextField textDES;
private JTextField textDEC;
private JTextField textMED;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Programa window = new Programa();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Programa() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblPooAtividade = new JLabel("POO - Atividade 06");
lblPooAtividade.setFont(new Font("Calibri", Font.BOLD, 30));
lblPooAtividade.setBounds(100, 11, 234, 37);
frame.getContentPane().add(lblPooAtividade);
JLabel lblDescrio = new JLabel("DESCRI\u00C7\u00C3O:");
lblDescrio.setFont(new Font("Calibri", Font.BOLD, 16));
lblDescrio.setBounds(10, 86, 80, 20);
frame.getContentPane().add(lblDescrio);
textDES = new JTextField();
textDES.setFont(new Font("Calibri", Font.PLAIN, 16));
textDES.setBounds(100, 81, 250, 25);
frame.getContentPane().add(textDES);
textDES.setColumns(10);
JLabel lblNumeroDecimal = new JLabel("VALOR DECIMAL:");
lblNumeroDecimal.setFont(new Font("Calibri", Font.BOLD, 16));
lblNumeroDecimal.setBounds(10, 146, 115, 20);
frame.getContentPane().add(lblNumeroDecimal);
textDEC = new JTextField();
textDEC.setFont(new Font("Calibri", Font.PLAIN, 16));
textDEC.setBounds(134, 141, 50, 25);
frame.getContentPane().add(textDEC);
textDEC.setColumns(10);
JLabel lblUnidadeDeMedida = new JLabel("UNIDADE DE MEDIDA:");
lblUnidadeDeMedida.setFont(new Font("Calibri", Font.BOLD, 16));
lblUnidadeDeMedida.setBounds(10, 206, 149, 20);
frame.getContentPane().add(lblUnidadeDeMedida);
textMED = new JTextField();
textMED.setFont(new Font("Calibri", Font.PLAIN, 16));
textMED.setBounds(169, 201, 50, 25);
frame.getContentPane().add(textMED);
textMED.setColumns(10);
JButton btnGravar = new JButton("GRAVAR");
btnGravar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//1º Parte - Conectando...
try{
Connection conexao = null;
Statement stm = null;
String msg = "";
String sql = "";
Class.forName("com.mysql.jdbc.Driver");
conexao = DriverManager.getConnection("jdbc:mysql://localhost/poo_atividade_06", "root", "");
stm = conexao.createStatement();
msg = "Conexao realizada com sucesso";
System.out.println(msg);
//OK Funcionou!
//sql = "INSERT INTO `tblproduto`(`codigo`, `descricao`, `valor decimal`, `unidade de medida`) VALUES (null, 'Thiago', 1.1, 'T')";
//Erro - Erro encontrado: java.sql.SQLException: Incorrect decimal value: 'textDEC.GetText()' for column 'valor decimal' at row 1
sql = "INSERT INTO `tblproduto`(`codigo`, `descricao`, `valor decimal`, `unidade de medida`) VALUES (null, 'textDES.GetText()', 'textDEC.GetText()', 'textMED.GetText()')";
//Erro - You have an error in your SQL syntax;
//sql = "INSERT INTO `tblproduto`(codigo, descricao, valor decimal, unidade de medida) VALUES (null, 'textDES.GetText()', 'textDEC.GetText()', 'textMED.GetText()')";
stm.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Dados incluidos com sucesso.");
}catch(Exception erro){
System.out.println("Erro encontrado: " + erro);
}
finally{
System.out.println("Bloco finalizado");
}
}
});
btnGravar.setFont(new Font("Calibri", Font.BOLD, 16));
btnGravar.setBounds(300, 197, 95, 30);
frame.getContentPane().add(btnGravar);
}
}