|
Home > Archive > PostgreSQL JDBC > September 2005 > simple insert operation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
simple insert operation
|
|
| Aydın Toprak 2005-09-08, 3:24 am |
| 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(connec
tionStr, "XXXX", "XXXX");
String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
(3, 5)";
PreparedStatement query = connection. prepareStatement(sql
Query);
query.executeUpdate();
connection.close();
but I cant compile it
the errors
-------------------------------------------------------------------------------------------
form.java:26: unreported exception java.lang. ClassNotFoundExcepti
on;
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(sql
Query);
^
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
| |
| Csaba Nagy 2005-09-08, 3:24 am |
| Aydin,
You're obviously beginner at Java too :-)
The problem has nothing to do with JDBC, you must catch the Java
exceptions which are declared to be thrown by the methods you use (the
compilation error message is quite explicit about which ones), or
declare them to be thrown by your code. Look up some Java tutorial,
and/or use an intelligent IDE which highlights you the errors and
possibly fixes them too automatically... IntelliJ is a good one (not
free).
Other than that, from a superficial check, I guess your code is Ok.
Cheers,
Csaba.
On Thu, 2005-09-08 at 09:58, Aydın Toprak wrote:
> 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(connec
tionStr, "XXXX", "XXXX");
> String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
> (3, 5)";
> PreparedStatement query = connection. prepareStatement(sql
Query);
> query.executeUpdate();
> connection.close();
>
> but I cant compile it
>
> the errors
>
> -------------------------------------------------------------------------------------------
> form.java:26: unreported exception java.lang. ClassNotFoundExcepti
on;
> 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(sql
Query);
>
> ^
> 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
---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match
| |
| Roland Walter 2005-09-08, 3:24 am |
| 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(connec
tionStr, "XXXX", "XXXX");
> String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
> (3, 5)";
> PreparedStatement query = connection. prepareStatement(sql
Query);
> query.executeUpdate();
> connection.close();
>
> but I cant compile it
>
> the errors
>
> -------------------------------------------------------------------------------------------
>
> form.java:26: unreported exception java.lang. ClassNotFoundExcepti
on;
> 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(sql
Query);
>
> ^
> 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 (ExceptionThrownByMe
thod e) {
actOnExceptionOrNot(
);
...
}
You can omit the try-catch-block, if the method sorounding the called
method throws the exception:
void doCallMethod() throws ExceptionThrownByMet
hod {
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.
------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?
http://www.postgresql.org/docs/faq
| |
| Diego Gil 2005-09-08, 11:25 am |
| 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/tuto...ions/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 & #123;
> Class.forName("org.postgresql.Driver");
} catch (java.lang. ClassNotFoundExcepti
on nfe) {
nfe.printStackTrace();
}
try & #123;
> String connectionStr = "jdbc:postgresql://localhost:5432/XXXX";
> Connection connection =
> java.sql.DriverManager. getConnection(connec
tionStr, "XXXX", "XXXX");
> String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
> (3, 5)";
> PreparedStatement query = connection. prepareStatement(sql
Query);
> 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. ClassNotFoundExcepti
on;
> 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(sql
Query);
>
> ^
> 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
>
>
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
| |
| Kevin Grittner 2005-09-08, 8:24 pm |
| Hi Ayd*n,
Others have addressed your compile errors, but I see something which might cause you problems at run time.
Identifiers which are not "quoted" will be treated as lower case in PostgreSQL, so your query will try to access "passtable"."idcol" instead of "passTable"."idCol". If you created the table without using quotes it will be in lower case and you will be OK
referencing it without quotes no matter what capitalization you use; however, if the table really exists in your database with the camel case identifiers, you must use quotes.
-Kevin
[color=darkred]
String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
(3, 5)";
---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend
| |
| Aydın Toprak 2005-09-09, 3:23 am |
| I have faced & solved all the problems that you have mantioned ..
try-catch statements and this "lowercase" situation.
the lowercase situation was the most annoying one...
Thanks for your help...
Kevin Grittner wrote:
>Hi Ayd*n,
>
>Others have addressed your compile errors, but I see something which might cause you problems at run time.
>
>Identifiers which are not "quoted" will be treated as lower case in PostgreSQL, so your query will try to access "passtable"."idcol" instead of "passTable"."idCol". If you created the table without using quotes it will be in lower case and you will be O
K referencing it without quotes no matter what capitalization you use; however, if the table really exists in your database with the camel case identifiers, you must use quotes.
>
>-Kevin
>
>
>
>
>
> String sqlQuery = "INSERT INTO passTable (idCol , pass) VALUES
>(3, 5)";
>
>
>
>
>
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql
.org so that your
message can get through to the mailing list cleanly
|
|
|
|
|