java - 服务器 : Null java. sql.SQLException 参数索引超出范围(1 > 参数数量,为 0)

标签 java mysql sql sql-server netbeans

错误如下:

Error - This signifies the try catch statement with sql  
Oct 29, 2017 5:19:43 PM tenantcheck.Check jButton1MouseClicked   
SEVERE: null     
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 

代码

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
                Connection con = null;
                Statement statement = null;
       try{
            con = DriverManager.getConnection(CONN_STRING,USERNAME,PASSWORD);
          if (con == null){
               System.out.println("Connected to database");
                  
          }
        String Cred;    
        try {
            statement = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Check.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Didnt Werk");
        }
        String name = JOptionPane.showInputDialog("Please enter Tenant Name");     
        int dialogButton = JOptionPane.YES_NO_OPTION;
        int dialogResult = JOptionPane.showConfirmDialog (null, "Was credit check authority approved?","Important",dialogButton);
        if(dialogResult == JOptionPane.YES_OPTION){
             Cred = "Yes";
           
    
  // Saving code here
         }
        else {
            Cred = "No";
          
        }   
           
             double income = Double.parseDouble(JOptionPane.showInputDialog("Please enter tenant income"));
             double BalSet = Double.parseDouble(JOptionPane.showInputDialog("Please Enter tenants balanced settled"));
               double moneyrent = 0.7 * income; 
         if ( Cred == "No"||BalSet < 0|| income < 5000 ){
             JOptionPane.showMessageDialog(null,"The user doesnt qualify for any of the properties");
         }
         else {
                    try {
                        String insertQuery = "SELECT * FROM unit WHERE "+ moneyrent +" => Cost And Occupied = 1;";
                        PreparedStatement pstmt = con.prepareStatement(insertQuery);
                        pstmt.setDouble(1,moneyrent);
                         statement.execute(insertQuery);
                        txtUnit.setText( insertQuery);
                      //statement.execute(insertQuery);
                    } catch (SQLException ex) {
                        Logger.getLogger(Check.class.getName()).log(Level.SEVERE, null, ex);
                        System.out.println("Error");
                    }
    }    
           
        }catch(Exception e) {
            System.out.println("Not Connected");
        }
        
       
        
    }  

最佳答案

您的preparedStatement中没有任何参数,但您尝试填写参数:

pstmt.setDouble(1,moneyrent);

您的准备​​语句应该是:

SELECT * FROM unit WHERE Cost >= ? And Occupied = 1

关于java - 服务器 : Null java. sql.SQLException 参数索引超出范围(1 > 参数数量,为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001997/

相关文章:

java - 创建新的 Grails 2.3 应用程序时出现 ForkedTomcatServer.groovy 错误

java - 从 ListView 启动新 Activity

java - 缺少 cxf jar : Please select the cxf home directory

python - get() 在标记为唯一的字段上排除 MultipleObjectsReturned

php - 使用 mysql 列表中生成的数据

mysql - 如何清除在 CentOS 7 上运行的 mysqld.log 文件

java - 如何从Android上的java webservice获取图像

sql - 通过存储过程从 SQL 导出二进制文件数据(图像)

c# - 使用 SqlBulkCopy 批量插入后获取主键列值

mysql - INSERT IGNORE 插入 0 行