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