简体   繁体   中英

Error ResultSet can not be int

I have been trying to do a method that would can recieve a SQL sentence and do a 'ExecuteUpdate' with two classes but when I declare 'ResultSet res = stat.executeUpdate(SQL sentece)' Netbeans says me this error: 'Int cannot be convert to ResultSet', but I don't know why. This are the two classes:

Class conexion

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

Class Querys

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

嗯,因为executeUpdate()返回的是更新或删除的实体数,而且它真的是int值。

Because executeUpdate method returns int .This int value shows how much rows are affected by your sql statement.

for example how much rows are deleted / updated / inserted.

it return 0 when your statement does not affect any row .

It'd be

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

you're actually executing update statement, it'll only return no of rows affected not the ResultSet

if you wont to mantain the ResultSet you can use

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

or change to int

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM