Code example for PreparedStatement

Methods: executeUpdatesetIntsetStringclosegetGeneratedKeys

0
        Connection con = Conexao.getConnection();
        PreparedStatement ps = con.prepareStatement(
                "insert into meta(Program_idProgram,meta) values (?,?)", 
                Statement.RETURN_GENERATED_KEYS);
 
        ps.setInt(1, obj.getIteractions().get(i).getProgram().getIdProgram());
        ps.setString(2, obj.getIteractions().get(i).getProgram().getMetas().get(k).getMeta());
 
        ps.executeUpdate();
 
        ResultSet rskey = null;
        rskey = ps.getGeneratedKeys();
 
        if (rskey != null && rskey.next()) {
            obj.getIteractions().get(i).getProgram().getMetas().get(k).setIdMeta(rskey.getInt(1));
        } 
 
        ps.close();
        con.close();
    } 
} 
Stop searching for code, let great code find you!  Add Codota to your java IDE