Обсуждение: st.executeQuery("SELECT NEXTVAL('seq_suporte') AS valor");

Поиск
Список
Период
Сортировка

st.executeQuery("SELECT NEXTVAL('seq_suporte') AS valor");

От
Elielson Fontanezi
Дата:
Hi Folks!

    I need some help.
    See this code parcel:

    ...
    ...
      ResultSet rs =
        st.executeQuery("SELECT NEXTVAL('seq_suporte') AS valor");
    ...
    ...

    It produces: java.lang.NullPointerException

    What is problem?
    I am used to do SELECT commands like this with any table I´ve
created so far,
but with this sequence no data comes from.
    I am uncapable of guessing what can be.

    I´d appreciate suggestions.

    Below there is a short program that reproduces this case.

Thanks!

-----------------------------------------------------------------
import java.sql.*;

class pgdbping
    {
    public static void main(String args[])
        {
        try
            {
            Class.forName("org.postgresql.Driver");

            Connection con = DriverManager.getConnection(
  "jdbc:postgresql://10.10.96.147:5432/suporte",
  "suporte", "suporte");

            System.out.println("Connection successful!!");

            Statement st = con.createStatement();

            ResultSet rs = st.executeQuery(
               "SELECT NEXTVAL('seq_suporte') AS valor");

            int novoCodigoSuporte =
               rs.getInt("valor");

            st.close();
            con.close();
            }
        catch(Exception e)
            {
            e.printStackTrace();
            }
        }
    }


+--------------------------------------------------------+
|   _    Elielson Fontanezi (elielsonf@prodam.sp.gov.br) |
|  °v°   PRODAM - VSE Department +55 11 5080-9283        |
| /(_)\  May your trails be crooked, winding, lonesome,  |
|  ^ ^   dangerous, leading to the most amazing view.    |
| May your mountains rise into and above the clouds.     |
+--------------------------------------------------------+

Re: st.executeQuery("SELECT NEXTVAL('seq_suporte') AS valor");

От
Kris Jurka
Дата:
You are not doing a rs.next() before rs.getInt().

Kris Jurka

On Mon, 3 Feb 2003, Elielson Fontanezi wrote:

> Hi Folks!
>
>     I need some help.
>     See this code parcel:
>
>     ...
>     ...
>       ResultSet rs =
>         st.executeQuery("SELECT NEXTVAL('seq_suporte') AS valor");
>     ...
>     ...
>
>     It produces: java.lang.NullPointerException
>
>     What is problem?
>     I am used to do SELECT commands like this with any table I�ve
> created so far,
> but with this sequence no data comes from.
>     I am uncapable of guessing what can be.
>
>     I�d appreciate suggestions.
>
>     Below there is a short program that reproduces this case.
>
> Thanks!
>
> -----------------------------------------------------------------
> import java.sql.*;
>
> class pgdbping
>     {
>     public static void main(String args[])
>         {
>         try
>             {
>             Class.forName("org.postgresql.Driver");
>
>             Connection con = DriverManager.getConnection(
>   "jdbc:postgresql://10.10.96.147:5432/suporte",
>   "suporte", "suporte");
>
>             System.out.println("Connection successful!!");
>
>             Statement st = con.createStatement();
>
>             ResultSet rs = st.executeQuery(
>                "SELECT NEXTVAL('seq_suporte') AS valor");
>
>             int novoCodigoSuporte =
>                rs.getInt("valor");
>
>             st.close();
>             con.close();
>             }
>         catch(Exception e)
>             {
>             e.printStackTrace();
>             }
>         }
>     }
>
>
> +--------------------------------------------------------+
> |   _    Elielson Fontanezi (elielsonf@prodam.sp.gov.br) |
> |  �v�   PRODAM - VSE Department +55 11 5080-9283        |
> | /(_)\  May your trails be crooked, winding, lonesome,  |
> |  ^ ^   dangerous, leading to the most amazing view.    |
> | May your mountains rise into and above the clouds.     |
> +--------------------------------------------------------+
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>