Convert String to INT

0

How to convert the string of the variable fg to an integer?

if (connectionIsOpened)
    {
String s = "INSERT INTO jogador(nome) VALUES " + "('" + this.playerName + "'" + ")";
        connection.executeUpdate(s);

        String fg = "SELECT cod_jogador FROM jogador WHERE jogador.nome = " + "'" + this.playerName + "'"; //buscar cod_jogador onde nome_jogador = this.playerName
        connection.executeQuery(fg);
        int gh = Integer.parseInt(fg);
        String a ="INSERT INTO 'jogos'( 'cod_jogador', 'pontuacao') VALUES " + "(" + gh + "," + this.points + ")"; //Inserir cod_jogador e pontuação onde nome_jogador = this.playernames
        connection.executeUpdate(a);
        connection.close();

    }  
    
asked by anonymous 04.02.2018 / 17:08

1 answer

1

If your table column is of type int , you need to get the value of Resultset ", using the getInt() :

if (connectionIsOpened)
    {
        String s = "INSERT INTO jogador(nome) VALUES " + "('" + this.playerName + "'" + ")";
        connection.executeUpdate(s);

        String fg = "SELECT cod_jogador FROM jogador WHERE jogador.nome = " + "'" + this.playerName + "'"; //buscar cod_jogador onde nome_jogador = this.playerName
        ResultSet rs = connection.executeQuery(fg);

        if(rs.next()){
            int gh = rs.getInt(1);
            String a ="INSERT INTO 'jogos'( 'cod_jogador', 'pontuacao') VALUES " + "(" + gh + "," + this.points + ")"; //Inserir cod_jogador e pontuação onde nome_jogador = this.playernames
            connection.executeUpdate(a);    
        }

        connection.close();

}  

Just remembering that you need to add the import: import java.sql.ResultSet; .

Note: it should be noted that, as warned by @ThiagoLoureiro Your code may be vulnerable if these variables that you are concatenating in the query are coming from user input, such as text fields. This answer has an example of the use of PreparedStatement and other helpful links.

    
04.02.2018 / 17:23