错误java.sql.SQLEception:参数索引超出范围(1>参数个数,为0)

时间:2017-06-11 17:17:04

标签: java database sqlexception

我是编程新手,我无法弄清楚如何修复此错误:java.sql.SQLEception:参数索引超出范围(1>参数个数,为0)。第一个JFrame运行,我收到消息,它已连接到数据库,但然后弹出错误,没有任何内容保存到我的数据库。 这是我的代码:

import java.sql.*;
import javax.swing.*;

public class SaveData extends javax.swing.JFrame {
Connection conn=null;
PreparedStatement pst=null;
ResultSet rs=null;

public SaveData() {
    initComponents();
}                 

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
   conn=MySQLConnect.ConnectDB();
   String Sql="Select * from TripData";
   try{
       pst=conn.prepareStatement(Sql);
       pst.setString(1, txtTripDate.getText());
       pst.setString(2, txtStartDest.getText());
       pst.setString(3, txtEndDest.getText());
       pst.setString(4, txtMileage.getText());
       pst.setString(5, txtCost.getText());
       if(rs.next()){
           JOptionPane.showMessageDialog(null, "Trip Saved");
           InfoSaved s=new InfoSaved();
           s.setVisible(true);
       }
       else{
           JOptionPane.showMessageDialog(null, "Invalid Information", 
"Please enter again", JOptionPane.ERROR_MESSAGE);

       }
   }catch(Exception e){
       JOptionPane.showMessageDialog(null, e);       }
}                                       

2 个答案:

答案 0 :(得分:0)

SQL字符串必须包含要设置的每个参数的?占位符。

答案 1 :(得分:0)

我认为你想做这样的事情[如果pst是一个声明就有效]:

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
        conn=MySQLConnect.ConnectDB();
        String Sql="Select * from TripData";
        try{
            pst = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = pst.executeQuery(Sql); // Gets all the TripData rows
            rs.moveToInsertRow();       // Moves to "insert" row
            // Updates "insert" row columns
            rs.updateString("colum_name", txtTripDate.getText()); 
            rs.updateString("colum_name", txtStartDest.getText());
            rs.updateString("colum_name", txtEndDest.getText());
            rs.updateString("colum_name", txtMileage.getText());
            rs.updateString("colum_name", txtCost.getText());
            rs.insertRow(); // Inserts the "insert" row to your database
            if(rs.rowInserted()){
                JOptionPane.showMessageDialog(null, "Trip Saved");
                InfoSaved s=new InfoSaved();
                s.setVisible(true);
            }
            else{
                JOptionPane.showMessageDialog(null, "Invalid Information", "Please enter again", JOptionPane.ERROR_MESSAGE);
        }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);       
        }
    }

EDITED: [作为准备好的声明的代码]
你必须改变你的查询,你不能通过SELECT命令INSERT(如果这是你想要做的)。

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    conn=MySQLConnect.ConnectDB();
    String Sql="INSERT INTO TripData(COLUMN_NAME, COLUMN_NAME, COLUMN_NAME, COLUMN_NAME, COLUMN_NAME) VALUES(?, ?, ?, ?, ?)";
    try{
        pst = conn.prepareStatement(Sql);
        pst.setString(1, txtTripDate.getText()); 
        pst.setString(2, txtStartDest.getText());
        pst.setString(3, txtEndDest.getText());
        pst.setString(4, txtMileage.getText());
        pst.setString(5, txtCost.getText());
        int affectedRows = pst.executeUpdate();
        if(affectedRows != 0){
            JOptionPane.showMessageDialog(null, "Trip Saved");
            InfoSaved s=new InfoSaved();
            s.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(null, "Invalid Information", "Please enter again", JOptionPane.ERROR_MESSAGE);
    }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);       
    }
}

抱歉我的错误。