【问题标题】:Error ResultSet can not be int错误结果集不能是 int
【发布时间】:2015-11-12 07:09:08
【问题描述】:

我一直在尝试做一个可以接收 SQL 语句并使用两个类执行“ExecuteUpdate”的方法,但是当我声明“ResultSet res = stat.executeUpdate(SQL sentece)”时,Netbeans 告诉我这个错误:' Int 不能转换为 ResultSet',但我不知道为什么。这是两个类:

类连接

package controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.ResultSet;

public class conexion{

Connection con = null;
Statement stat = null;

//Method to star the connection
conexion(){

    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","n0m3l0");   
    }
    catch(ClassNotFoundException | SQLException x){
        System.out.println(x);  
    }
} 

//Method to end the Connection
void endConexion(){
    if(con == null || stat == null){  
        try{
            stat.close();
            con.close();
        }
        catch(SQLException x){
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, x);
        }
    }
    else {
        System.out.println("Connection is already finished");
    }
}
//Methods Set and Get for Stat and Con
void setStat(Statement x){  
    this.stat = x;
}

void setCon(Connection x){ 
    this.con = x;
}

Statement getStat(){
    return this.stat;
}

Connection getCon(){
    return this.con;
}
}

类查询

package controller;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Querys {

//Call class conexion
private conexion mycon = new conexion();

//Method to execute onlye inserts querys
void insertQuerys(String sql){
    try{
        mycon.setStat(mycon.getCon().createStatement());
        mycon.getStat().executeUpdate(sql);
    }

    catch(Exception x){
        System.out.println(x); 
    }
}

 //MEthod to execute query that return results
ResultSet queryResponse(String sql) throws SQLException{

    mycon.setCon(null);
    mycon.setStat(null);

     ****This is the line where is the error*****
    ResultSet res = mycon.getStat().executeUpdate(sql);

    while(res.next()){
        return res;
    }    
}  
}

【问题讨论】:

    标签: java sql int resultset


    【解决方案1】:

    如果你不想维护 ResultSet,你可以使用

    ResultSet rs = mycon.getStat().executeQuery("SELECT a, b FROM TABLE2");
    

    或者改成int

    int rowsUpdated = mycon.getStat().executeUpdate(sql);
    

    【讨论】:

      【解决方案2】:

      嗯,因为executeUpdate()返回的实体更新或删除的数量,确实是int的值。

      【讨论】:

        【解决方案3】:

        因为executeUpdate 方法返回int。这个int 值显示了受您的sql 语句影响的行数。

        例如删除/更新/插入多少行。

        当您的语句不影响任何行时,它会返回 0

        【讨论】:

          【解决方案4】:

          应该是

          int rowsUpdated = mycon.getStat().executeUpdate(sql);
          

          您实际上是在执行更新语句,它只会返回不受影响的行数,而不是 ResultSet

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-02-18
            • 2014-03-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-11-21
            • 1970-01-01
            • 2019-05-16
            相关资源
            最近更新 更多