pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Roland Walter <rwa@mosaic-ag.com>
To: Aydın Toprak <aydin.toprak@intengo.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: simple insert operation
Date: Thu, 08 Sep 2005 10:23:06 +0200
Message-ID: <431FF4EA.7070508@mosaic-ag.com> (raw)
In-Reply-To: <431FEF3D.6050906@intengo.com>
References: <431FEF3D.6050906@intengo.com>

Aydın Toprak schrieb:
> 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...
> 
>        Class.forName("org.postgresql.Driver");
>        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();
> 
> but I cant compile it
> 
> the errors
> 
> ------------------------------------------------------------------------------------------- 
> 
> form.java:26: unreported exception java.lang.ClassNotFoundException; 
> must be cau
> ght 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..
> 

The problem is, that all methods mentioned in the error messages
throw exceptions. Exceptions in java must be catched in try-catch-block

try {
	method();
	...
} catch (ExceptionThrownByMethod e) {
   	actOnExceptionOrNot();
	...
	
}

You can omit the try-catch-block, if the method sorounding the called 
method throws the exception:

void doCallMethod() throws ExceptionThrownByMethod {
	method();
	...
}

But then you need to catch the Exception at the place where 
doCallMethod() is called.

Look in a documentation for the java programming language for more 
information.


-- 
Roland Walter                  phone: +49 (0) 22 25 / 88 2-41 1
MOSAIC SOFTWARE AG               fax: +49 (0) 22 25 / 88 2-20 1
Am Pannacker 3                mailto: rwa (at) mosaic-ag (dot) com
D-53340 Meckenheim              http://www.mosaic-ag.com

-------  L E G A L    D I S C L A I M E R  ---------

Die Informationen in dieser Nachricht sind vertraulich
und ausschliesslich fuer den Adressaten bestimmt.
Kenntnisnahme durch Dritte ist unzulaessig. Die
Erstellung von Kopien oder das Weiterleiten an weitere,
nicht originaere und benannte Adressaten ist nicht
vorgesehen und kann ungesetzlich sein. Die Meinungen
in dieser Nachricht stellen lediglich die Meinungen
des Senders dar. Falls Sie vermuten, dass diese
Nachricht veraendert wurde, setzen Sie sich mit dem
Absender in Verbindung. Der Absender uebernimmt ohne
weitere Ueberpruefung keine Verantwortung fuer die
Richtigkeit und Vollstaendigkeit des Inhalts. Unbefugte
Empfaenger werden gebeten, die Vertraulichkeit der
Nachricht zu wahren und den Absender sofort ueber
einen Uebertragungsfehler zu informieren.
------------------------------------------------------




view thread (6+ messages)  latest in thread

Message-ID: <431FF4EA.7070508@mosaic-ag.com>
Permalink:  ../431FF4EA.7070508@mosaic-ag.com/
Also on:    postgresql.org/message-id/431FF4EA.7070508@mosaic-ag.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: rwa@mosaic-ag.com, aydin.toprak@intengo.com
  Subject: Re: simple insert operation
  In-Reply-To: <431FF4EA.7070508@mosaic-ag.com>
  References: <431FEF3D.6050906@intengo.com> <431FF4EA.7070508@mosaic-ag.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox