0
public class VerEstoque extends JFrame {
Connection conexao;
PreparedStatement pst;
ResultSet rs;
public void Adicionar()
{ 
 String sql = "INSERT INTO Produtos (Nome, Marca, Loja, Estoque ,ValordeVenda) VALUES (?,?,?,?,?)";
 try
 {
 pst = conexao.prepareStatement(sql);
 pst.setString(1,textNome.getText());
 pst.setString(2,textMarca.getText());
 pst.setString(3,textLoja.getText());
 pst.setString(4,textEstoque.getText());
 pst.setString(5,textValordeVenda.getText());
 pst.execute();
 JOptionPane.showMessageDialog(null,"Produto adicionado com sucesso!", "Cadastrado com Sucesso", JOptionPane.INFORMATION_MESSAGE);
 listarProdutos();
 }
 catch(SQLException error)
 {
 JOptionPane.showMessageDialog(null,"Erro ao adicionar o produto!");
 }
}
public void listarProdutos(){
 String sql = "Select *from Produtos";
 try{
 pst = conexao.prepareStatement(sql);
 rs = pst.executeQuery();
 tblProdutos.setModel(DbUtils.resultSetToTableModel(rs));
 }
 catch(SQLException error){
 JOptionPane.showMessageDialog(null, error);
 }
}
public void buscarProdutos(){
 String sql = "Select *from Produtos where Nome like ?";
 try{
 pst = conexao.prepareStatement(sql);
 rs = pst.executeQuery();
 pst.setString(1,textBusca.getText()+"%");
 tblProdutos.setModel(DbUtils.resultSetToTableModel(rs));
 }
 catch(SQLException error){
 JOptionPane.showMessageDialog(null, error);
 }
}
private JPanel contentPane;
private JTextField textNome;
private JTextField textMarca;
private JTextField textLoja;
private JTextField textEstoque;
private JTextField textValordeVenda;
protected AbstractButton rdbtnRemover;
private JTable tblProdutos;
private JTextField textBusca;
/**
 * Launch the application.
 */
public static void main(String[] args) {
 EventQueue.invokeLater(new Runnable() {
 public void run() {
 try {
 VerEstoque frame = new VerEstoque();
 frame.setVisible(true);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 });
}
/**
 * Create the frame.
 */
public VerEstoque() throws ClassNotFoundException {
 conexao = BancoDeDados.bancodedados();
 setBackground(new Color(238, 238, 238));
 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 setBounds(100, 100, 697, 514);
 contentPane = new JPanel();
 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
 setContentPane(contentPane);
 contentPane.setLayout(null);
 textNome = new JTextField();
 textNome.setHorizontalAlignment(SwingConstants.CENTER);
 textNome.setBounds(289, 68, 134, 28);
 contentPane.add(textNome);
 textNome.setColumns(10);
 textMarca = new JTextField();
 textMarca.setHorizontalAlignment(SwingConstants.CENTER);
 textMarca.setBounds(289, 96, 134, 28);
 contentPane.add(textMarca);
 textMarca.setColumns(10);
 textLoja = new JTextField();
 textLoja.setHorizontalAlignment(SwingConstants.CENTER);
 textLoja.setBounds(289, 125, 134, 28);
 contentPane.add(textLoja);
 textLoja.setColumns(10);
 textEstoque = new JTextField();
 textEstoque.setHorizontalAlignment(SwingConstants.CENTER);
 textEstoque.setBounds(289, 152, 134, 28);
 contentPane.add(textEstoque);
 textEstoque.setColumns(10);
 textValordeVenda = new JTextField();
 textValordeVenda.setHorizontalAlignment(SwingConstants.CENTER);
 textValordeVenda.setBounds(289, 181, 134, 28);
 contentPane.add(textValordeVenda);
 textValordeVenda.setColumns(10);
 JButton btnAdicionar = new JButton("Adicionar!");
 btnAdicionar.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 Adicionar();
 }
 });
 btnAdicionar.setBounds(507, 69, 117, 29);
 contentPane.add(btnAdicionar);
 JButton btnTabela = new JButton("Ver tabela completa de estoque!");
 btnTabela.setToolTipText("");
 btnTabela.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 listarProdutos();
 }
 });
 btnTabela.setBounds(140, 421, 441, 45);
 contentPane.add(btnTabela);
 JButton btnBuscar = new JButton("Buscar!");
 btnBuscar.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 buscarProdutos();
 }
 });
 btnBuscar.setBounds(16, 126, 117, 29);
 contentPane.add(btnBuscar);
 JTextPane txtpnSelecioneAOpo = new JTextPane();
 txtpnSelecioneAOpo.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 15));
 txtpnSelecioneAOpo.setBackground(SystemColor.window);
 txtpnSelecioneAOpo.setText("Selecione a opção que deseja fazer :");
 txtpnSelecioneAOpo.setBounds(186, 6, 284, 23);
 contentPane.add(txtpnSelecioneAOpo);
 JLabel lblNome = new JLabel("Nome :");
 lblNome.setBounds(177, 74, 61, 16);
 contentPane.add(lblNome);
 JLabel lblMarca = new JLabel("Marca :");
 lblMarca.setBounds(177, 102, 61, 16);
 contentPane.add(lblMarca);
 JLabel lblLoja = new JLabel("Loja :");
 lblLoja.setBounds(178, 131, 43, 16);
 contentPane.add(lblLoja);
 JLabel lblEstoque = new JLabel("Estoque :");
 lblEstoque.setBounds(177, 158, 61, 16);
 contentPane.add(lblEstoque);
 JLabel lblValorDeVenda = new JLabel("Valor de Venda :");
 lblValorDeVenda.setBounds(177, 187, 108, 16);
 contentPane.add(lblValorDeVenda);
 JButton btnRemover = new JButton("Remover!");
 btnRemover.setBounds(507, 182, 117, 29);
 contentPane.add(btnRemover);
 JRadioButton rdbtnBuscar = new JRadioButton("Buscar");
 rdbtnBuscar.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 textEstoque.setEditable(false);
 textValordeVenda.setEditable(false);
 textLoja.setEditable(false);
 textMarca.setEditable(false);
 }
 });
 rdbtnBuscar.setBounds(140, 34, 98, 23);
 contentPane.add(rdbtnBuscar);
 JRadioButton rdbtnAdicionar = new JRadioButton("Adicionar");
 rdbtnAdicionar.addKeyListener(new KeyAdapter() {
 @Override
 public void keyPressed(KeyEvent e) {
 textNome.setEditable(true);
 textEstoque.setEditable(true);
 textValordeVenda.setEditable(true);
 textLoja.setEditable(true);
 textMarca.setEditable(true);
 }
 });
 rdbtnAdicionar.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 textNome.setEditable(true);
 textEstoque.setEditable(true);
 textValordeVenda.setEditable(true);
 textLoja.setEditable(true);
 textMarca.setEditable(true);
 }
 });
 rdbtnAdicionar.setBounds(279, 34, 98, 23);
 contentPane.add(rdbtnAdicionar);
 JRadioButton rdbtnRemover = new JRadioButton("Remover");
 rdbtnAdicionar.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 textNome.setEditable(true);
 textEstoque.setEditable(true);
 textValordeVenda.setEditable(false);
 textLoja.setEditable(false);
 textMarca.setEditable(false);
 }
 });
 rdbtnRemover.setBounds(440, 34, 141, 23);
 contentPane.add(rdbtnRemover);
 JButton btnCancelar = new JButton("Cancelar!");
 btnCancelar.setBounds(417, 126, 98, 28);
 contentPane.add(btnCancelar);
 JScrollPane scrollPane = new JScrollPane();
 scrollPane.setBounds(29, 251, 643, 170);
 contentPane.add(scrollPane);
 tblProdutos = new JTable();
 scrollPane.setViewportView(tblProdutos);
 tblProdutos.setToolTipText("");
 JLabel pesquisaproduto = new JLabel("Nome do Produto para Busca : ");
 pesquisaproduto.setBounds(29, 228, 226, 16);
 contentPane.add(pesquisaproduto);
 textBusca = new JTextField();
 textBusca.addKeyListener(new KeyAdapter() {
 @Override
 public void keyReleased(KeyEvent e) {
 buscarProdutos();
 }
 });
 textBusca.setBounds(244, 222, 214, 28);
 contentPane.add(textBusca);
 textBusca.setColumns(10);
}
}

When I write some text on textBusca and then select buscarProdutos button to do a search, I receive this error:

java.sql.SQLException: No value specified for parameter 1.

Luiggi Mendoza
85.9k16 gold badges160 silver badges355 bronze badges
asked Jun 20, 2015 at 3:22

1 Answer 1

2

You execute the query before setting the first parameter :

 pst = conexao.prepareStatement(sql);
 rs = pst.executeQuery();
 pst.setString(1,textBusca.getText()+"%");

change to

 pst = conexao.prepareStatement(sql);
 pst.setString(1,textBusca.getText()+"%");
 rs = pst.executeQuery();
answered Jun 20, 2015 at 3:23
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.