Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合框中出错:java.sql.SQLException:无效的光标状态-无当前行_Java_Sql_Jdbc_Combobox_Informix - Fatal编程技术网

组合框中出错:java.sql.SQLException:无效的光标状态-无当前行

组合框中出错:java.sql.SQLException:无效的光标状态-无当前行,java,sql,jdbc,combobox,informix,Java,Sql,Jdbc,Combobox,Informix,我一直在犯这个错误 我有一个组合框,里面装满了数据库的元素,我想让选中的选项出现在文本框中,但我得到了这个错误。它仅适用于第一个结果: 代码如下: private void cmbx_vendActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: try{ String

我一直在犯这个错误

我有一个组合框,里面装满了数据库的元素,我想让选中的选项出现在文本框中,但我得到了这个错误。它仅适用于第一个结果:

代码如下:

private void cmbx_vendActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
   try{
      String url = "jdbc:informix-sqli://192.168.2.3:1525/cubo:INFORMIXSERVER=myserver;user=infx;password=infx";
    Connection con1 = DriverManager.getConnection(url);  
    System.out.println("Cubo conectada combobox");
    Statement st1= con1.createStatement();
    ResultSet rs1=st1.executeQuery("SELECT * FROM cartsusc WHERE vendedor = '"+this.cmbx_vend.getSelectedItem()+"'");
    rs1.next();
    this.txt_dato.setText(rs1.getString("vendedor"));

    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}
您必须使用:

if(rs1.next()){
    this.txt_dato.setText(rs1.getString("vendedor"));
}

不要这样设置参数,这可能会导致语法错误或SQL注入,相反,您必须使用PreparedStatement,例如:

String query = "SELECT * FROM cartsusc WHERE vendedor = ?";
try (PreparedStatement pstm = connection.prepareStatement(query)) {

    pstm.setString(1, this.cmbx_vend.getSelectedItem());
    ResultSet rs1 = pstm.executeQuery();
    if(rs1.next()){
       this.txt_dato.setText(rs1.getString("vendedor"));
       //this.txt_dato.setText(rs1.getString(1));//or you can get your result like this
    }
}
完成后不要忘记关闭语句和连接。

您必须使用:

if(rs1.next()){
    this.txt_dato.setText(rs1.getString("vendedor"));
}

不要这样设置参数,这可能会导致语法错误或SQL注入,相反,您必须使用PreparedStatement,例如:

String query = "SELECT * FROM cartsusc WHERE vendedor = ?";
try (PreparedStatement pstm = connection.prepareStatement(query)) {

    pstm.setString(1, this.cmbx_vend.getSelectedItem());
    ResultSet rs1 = pstm.executeQuery();
    if(rs1.next()){
       this.txt_dato.setText(rs1.getString("vendedor"));
       //this.txt_dato.setText(rs1.getString(1));//or you can get your result like this
    }
}
完成后不要忘记关闭语句和连接。

检查
ResultSet\next()
以查看是否有可用的行

此外,请始终使用PreparedStatement。它们有助于降低SQL注入攻击的风险

常用的成语是:

try {
    Connection conn = getConnection();
    try {
        String sql = "SELECT * FROM cartsusc WHERE vendedor = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        try {
            ps.setString(1, this.cmbx_vend.getSelectedItem());
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    this.txt_dato.setText(rs1.getString("vendedor"));
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    } finally {
        conn.close();
    }
} catch (SQLException e) {
    //handle
}
从Java 7开始,您可以使用try with resources自动关闭资源:

String sql = "SELECT * FROM cartsusc WHERE vendedor = ?";
try (
    Connection conn = getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.setString(1, this.cmbx_vend.getSelectedItem());
    try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) {
            this.txt_dato.setText(rs1.getString("vendedor"));
        }
    }
} catch (SQLException e) {
    //handle
}
检查
ResultSet#next()
以查看是否确实有可用的行

此外,请始终使用PreparedStatement。它们有助于降低SQL注入攻击的风险

常用的成语是:

try {
    Connection conn = getConnection();
    try {
        String sql = "SELECT * FROM cartsusc WHERE vendedor = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        try {
            ps.setString(1, this.cmbx_vend.getSelectedItem());
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    this.txt_dato.setText(rs1.getString("vendedor"));
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    } finally {
        conn.close();
    }
} catch (SQLException e) {
    //handle
}
从Java 7开始,您可以使用try with resources自动关闭资源:

String sql = "SELECT * FROM cartsusc WHERE vendedor = ?";
try (
    Connection conn = getConnection();
    PreparedStatement ps = conn.prepareStatement(sql);
    ) {
    ps.setString(1, this.cmbx_vend.getSelectedItem());
    try (ResultSet rs = ps.executeQuery()) {
        if (rs.next()) {
            this.txt_dato.setText(rs1.getString("vendedor"));
        }
    }
} catch (SQLException e) {
    //handle
}

您的查询未返回任何行。。您应该检查
rs1.next()
是否为真。。然后只提取值。如何提取?你能解释一下吗?你的查询没有返回任何行。。您应该检查
rs1.next()
是否为真。。然后只提取值。如何提取?你能给我解释一下吗?