Обсуждение: Using JDBC
Hi All,
I have been working to make PostgreSQL acessible from java. I have
downloaded and instaled the following:
- j2sdk-1_4_1_01-linux-i586.bin
- jakarta-ant-1.5.1-src.tar.gz
and compiled PostgreSQL as:
# ./configure --with-java
# make
# make install
The driver (postgresql.jar) was compiled properly, and I have configured
some variables:
# export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
# export ANT_HOME=/usr/local/ant
# export JAVA_HOME=/usr/local/j2sdk1.4.1
# export PATH=${JAVA_HOME}:${PATH}:${ANT_HOME}/bin
So I think the environment is almost good, but I can't compile a source
that use jdbc.
Would you send to me a really simple example java source code using jdbc,
acessing a simple table at PostgreSQL? I'm really having problem with
this.
Thanks in advance and
Best Regards,
Marcelo Pereira
-- Remember that only God and ^[:w saves.
__
(_.\ © Marcelo Pereira |
/ / ___ marcelo@pereira.com |
/ (_/ _ \__ [Math|99]-IMECC |
_______\____/_\___)___Unicamp_______________/
Try: http://www.redhat.com/docs/manuals/database/RHDB-2.1-Manual/prog/jdbc.html Um abraco, Fernando Marcelo Pereira wrote:> Hi All, > > I have been working to make PostgreSQL acessible from java. I have > downloaded and instaled the following: > > - j2sdk-1_4_1_01-linux-i586.bin > - jakarta-ant-1.5.1-src.tar.gz > > and compiled PostgreSQL as: > > # ./configure --with-java > # make > # make install > > The driver (postgresql.jar) was compiled properly, and I have configured > some variables: > > # export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:. > # export ANT_HOME=/usr/local/ant > # export JAVA_HOME=/usr/local/j2sdk1.4.1 > # export PATH=${JAVA_HOME}:${PATH}:${ANT_HOME}/bin > > So I think the environment is almost good, but I can't compile a source > that use jdbc. > > Would you send to me a really simple example java source code using jdbc, > acessing a simple table at PostgreSQL? I'm really having problem with > this. > > Thanks in advance and > Best Regards, > > Marcelo Pereira > > -- Remember that only God and ^[:w saves. > __ > (_.\ © Marcelo Pereira | > / / ___ marcelo@pereira.com | > / (_/ _ \__ [Math|99]-IMECC | > _______\____/_\___)___Unicamp_______________/ > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > -- Fernando Nasser Red Hat - Toronto E-Mail: fnasser@redhat.com 2323 Yonge Street, Suite #300 Toronto, Ontario M4P 2C9
Hi Fernando,
I have already read this documents, because it came with PostgreSQL
documentation.
I would like to see a simple source code (and compile line) that open a
conection to the database, run a query and close the database.
I'm new with JDBC and examples are welcome.
These is the versions of the softwares I'm using:
Java: j2sdk-1.4.1
Ant: jakarta-ant-1.5.1
PostgreSQL: 7.3.1
This morning I tried to compile the following code (test.java):
// -- begin
/*
** pgdbping.java - programa para testar uma conexo jdbc com PostGRES
**
** Elielson - 24.07.2002
*/
import java.sql.*;
class pgdbping {
public static void main(String args[]) {
try {
Class.forName("org.postgresql.Driver");
Connection con;
if ( args.length != 3 )
{
String
url =
"jdbc:postgresql://192.168.1.5:5432/test", user = "postgres", password =
"123";
System.out.println("Uso: java dbping URL user password");
System.out.println("URL jdbc:postgresql://host:port/dbname");
System.out.println("Vou usar conexao interna: " + url
+ ", " + user + ", " + "*******");
con = DriverManager.getConnection(url, user, password);
}
else
{
con = DriverManager.getConnection(args[0], args[1], args[2]);
}
System.out.println("Sucesso na conexo!!");
con.close();
}
catch(Exception e) {
System.out.println("Falha na conexao");
e.printStackTrace();
}
}
}
// -- end
$ java test.java
$ javac test.class
Exception in thread "main" java.lang.NoClassDefFoundError: test/class
Is this the right way to compile/run the source codes?
Any ideas??
Thanks in advance,
Regards,
Marcelo Pereira
-- Remember that only God and ^[:w saves.
__
(_.\ © Marcelo Pereira |
/ / ___ marcelo@pereira.com |
/ (_/ _ \__ [Math|99]-IMECC |
_______\____/_\___)___Unicamp_______________/
--- Fernando Nasser, with his fast fingers, wrote:
:> Try:
:>
:> http://www.redhat.com/docs/manuals/database/RHDB-2.1-Manual/prog/jdbc.html
:>
:> Um abraco,
:> Fernando
:>
:> Marcelo Pereira wrote:> Hi All,
:> >
:> > I have been working to make PostgreSQL acessible from java. I have
:> > downloaded and instaled the following:
:> >
:> > - j2sdk-1_4_1_01-linux-i586.bin
:> > - jakarta-ant-1.5.1-src.tar.gz
:> >
:> > and compiled PostgreSQL as:
:> >
:> > # ./configure --with-java
:> > # make
:> > # make install
:> >
:> > The driver (postgresql.jar) was compiled properly, and I have configured
:> > some variables:
:> >
:> > # export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:.
:> > # export ANT_HOME=/usr/local/ant
:> > # export JAVA_HOME=/usr/local/j2sdk1.4.1
:> > # export PATH=${JAVA_HOME}:${PATH}:${ANT_HOME}/bin
:> >
:> > So I think the environment is almost good, but I can't compile a source
:> > that use jdbc.
:> >
:> > Would you send to me a really simple example java source code using jdbc,
:> > acessing a simple table at PostgreSQL? I'm really having problem with
:> > this.
:> >
:> > Thanks in advance and
:> > Best Regards,
:> >
:> > Marcelo Pereira
:> >
:> > -- Remember that only God and ^[:w saves.
:> > __
:> > (_.\ © Marcelo Pereira |
:> > / / ___ marcelo@pereira.com |
:> > / (_/ _ \__ [Math|99]-IMECC |
:> > _______\____/_\___)___Unicamp_______________/
:> >
:> >
:> > ---------------------------(end of broadcast)---------------------------
:> > TIP 2: you can get off all lists at once with the unregister command
:> > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
:> >
:>
:>
:>
:> --
:> Fernando Nasser
:> Red Hat - Toronto E-Mail: fnasser@redhat.com
:> 2323 Yonge Street, Suite #300
:> Toronto, Ontario M4P 2C9
:>
The Java JDBC tutorial will tell you all you need (with examples) http://java.sun.com/docs/books/tutorial/jdbc/index.html -----Original Message----- From: pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org]On Behalf Of Marcelo Pereira Sent: Tuesday, January 14, 2003 10:59 To: Fernando Nasser Cc: pgsql-jdbc@postgresql.org Subject: Re: [JDBC] Using JDBC Hi Fernando, I have already read this documents, because it came with PostgreSQL documentation. I would like to see a simple source code (and compile line) that open a conection to the database, run a query and close the database. I'm new with JDBC and examples are welcome. These is the versions of the softwares I'm using: Java: j2sdk-1.4.1 Ant: jakarta-ant-1.5.1 PostgreSQL: 7.3.1 This morning I tried to compile the following code (test.java): // -- begin /* ** pgdbping.java - programa para testar uma conexo jdbc com PostGRES ** ** Elielson - 24.07.2002 */ import java.sql.*; class pgdbping { public static void main(String args[]) { try { Class.forName("org.postgresql.Driver"); Connection con; if ( args.length != 3 ) { String url = "jdbc:postgresql://192.168.1.5:5432/test", user = "postgres", password = "123"; System.out.println("Uso: java dbping URL user password"); System.out.println("URL jdbc:postgresql://host:port/dbname"); System.out.println("Vou usar conexao interna: " + url + ", " + user + ", " + "*******"); con = DriverManager.getConnection(url, user, password); } else { con = DriverManager.getConnection(args[0], args[1], args[2]); } System.out.println("Sucesso na conexo!!"); con.close(); } catch(Exception e) { System.out.println("Falha na conexao"); e.printStackTrace(); } } } // -- end $ java test.java $ javac test.class Exception in thread "main" java.lang.NoClassDefFoundError: test/class Is this the right way to compile/run the source codes? Any ideas?? Thanks in advance, Regards, Marcelo Pereira -- Remember that only God and ^[:w saves. __ (_.\ © Marcelo Pereira | / / ___ marcelo@pereira.com | / (_/ _ \__ [Math|99]-IMECC | _______\____/_\___)___Unicamp_______________/ --- Fernando Nasser, with his fast fingers, wrote: :> Try: :> :> http://www.redhat.com/docs/manuals/database/RHDB-2.1-Manual/prog/jdbc.html :> :> Um abraco, :> Fernando :> :> Marcelo Pereira wrote:> Hi All, :> > :> > I have been working to make PostgreSQL acessible from java. I have :> > downloaded and instaled the following: :> > :> > - j2sdk-1_4_1_01-linux-i586.bin :> > - jakarta-ant-1.5.1-src.tar.gz :> > :> > and compiled PostgreSQL as: :> > :> > # ./configure --with-java :> > # make :> > # make install :> > :> > The driver (postgresql.jar) was compiled properly, and I have configured :> > some variables: :> > :> > # export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar:. :> > # export ANT_HOME=/usr/local/ant :> > # export JAVA_HOME=/usr/local/j2sdk1.4.1 :> > # export PATH=${JAVA_HOME}:${PATH}:${ANT_HOME}/bin :> > :> > So I think the environment is almost good, but I can't compile a source :> > that use jdbc. :> > :> > Would you send to me a really simple example java source code using jdbc, :> > acessing a simple table at PostgreSQL? I'm really having problem with :> > this. :> > :> > Thanks in advance and :> > Best Regards, :> > :> > Marcelo Pereira :> > :> > -- Remember that only God and ^[:w saves. :> > __ :> > (_.\ © Marcelo Pereira | :> > / / ___ marcelo@pereira.com | :> > / (_/ _ \__ [Math|99]-IMECC | :> > _______\____/_\___)___Unicamp_______________/ :> > :> > :> > ---------------------------(end of broadcast)--------------------------- :> > TIP 2: you can get off all lists at once with the unregister command :> > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) :> > :> :> :> :> -- :> Fernando Nasser :> Red Hat - Toronto E-Mail: fnasser@redhat.com :> 2323 Yonge Street, Suite #300 :> Toronto, Ontario M4P 2C9 :> ---------------------------(end of broadcast)--------------------------- TIP 2: you can get off all lists at once with the unregister command (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
On Tuesday, January 14, 2003, at 10:58 AM, Marcelo Pereira wrote:
<snip>
> /*
> ** pgdbping.java - programa para testar uma conexo jdbc com PostGRES
> **
> ** Elielson - 24.07.2002
> */
>
> import java.sql.*;
>
> class pgdbping {
This is all going to be way OT for this list, but....
Is your file named "pgdbping.java"? There is a 1-to-1 relationship
(case *sensitive*) between class names and filenames.
<snip>
> $ java test.java
"java" runs *compiled* classes. Similar to how "perl" runs scripts.
> $ javac test.class
"javac" compiles java source code into java class files. Similar to
how "gcc" compiles C code into binaries.
> Exception in thread "main" java.lang.NoClassDefFoundError: test/class
>
> Is this the right way to compile/run the source codes?
nope.
> Any ideas??
Assuming your source is named pgdbping.java:
1) compile it:
# export CLASSPATH=.:/path/to/postgres.jar
# javac pgdbping.java
2) run it:
# export CLASSPATH=.:/path/to/postgres.jar
# java pgdbping
You really should read over the tutorials on Java at sun's site
(java.sun.com). They cover the basics of compiling/running classes,
setting classpaths, etc.
You can also just type "java" and "javac" to get help/usage information
for each tool.
eric