Re: simple insert operation

From: Diego Gil <diego(at)adminsa(dot)com>
To: Aydın Toprak <aydin(dot)toprak(at)intengo(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: simple insert operation
Date: 2005-09-08 15:04:13
Message-ID: 1126191853.3004.9.camel@roadwarrior.adminsa.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi,

The problem is that you never catch exceptions, specifically
SQLException. It is more a Java problem that a PostgreSQL problem. Maybe
you should read a little more about Java.

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

Look at the modified code below.

Besides, it doesn't make sense to use PreparedStatement if don't use any
variable inside.

Regards,
Diego.

El jue, 08-09-2005 a las 10:58 +0300, Aydın Toprak escribió:
> Hii guys,
>
> I am very newbie about postgresql and I am making practice,
> however I couldnt been able to insert a simple item to my DB via jdbc...
>
>
> here is my code for insertion...
>
try {
> Class.forName("org.postgresql.Driver");
} catch (java.lang.ClassNotFoundException nfe) {
nfe.printStackTrace();
}

try {
> String connectionStr = "jdbc:postgresql://localhost:5432/XXXX";
> Connection connection =
> java.sql.DriverManager.getConnection(connectionStr, "XXXX", "XXXX");
> String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
> (3, 5)";
> PreparedStatement query = connection.prepareStatement(sqlQuery);
> query.executeUpdate();
> connection.close();

} catch (java.sql.SQLException se) {
se.printStackTrace();
}

>
> but I cant compile it
>
> the errors
>
> -------------------------------------------------------------------------------------------
> form.java:26: unreported exception java.lang.ClassNotFoundException;
> must be caught or declared to be thrown
> Class.forName("org.postgresql.Driver");
> ^
> form.java:28: unreported exception java.sql.SQLException; must be caught
> or decl
> ared to be thrown
> Connection connection =
> java.sql.DriverManager.getConnection(con
> nectionStr, "XXXX", "XXXX");
>
> ^
> form.java:30: unreported exception java.sql.SQLException; must be caught
> or decl
> ared to be thrown
> PreparedStatement query =
> connection.prepareStatement(sqlQuery);
>
> ^
> form.java:31: unreported exception java.sql.SQLException; must be caught
> or decl
> ared to be thrown
> query.executeUpdate();
> ^
> form.java:32: unreported exception java.sql.SQLException; must be caught
> or decl
> ared to be thrown
> connection.close();
> ^
> 5 errors
> -------------------------------------------------------------------------------------------
>
> what is the problem I am stuck with it..
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>
>

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message David Wall 2005-09-08 15:22:56 SELECT COUNT(*) does a scan?
Previous Message Gil G 2005-09-08 11:26:23 TimestampUtils.loadCalendar failes for interval type