簡體   English   中英

錯誤:“ java.sql.SQLException:未為參數1指定值”

[英]error : “java.sql.SQLException: No value specified for parameter 1 ”

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);


}
}

當我在textBusca上寫一些文本,然后選擇buscarProdutos按鈕進行搜索時,出現此錯誤:

java.sql.SQLException:未為參數1指定值。

您在設置第一個參數之前執行查詢:

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

改成

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM