Unix Technical Forum

newbie jdbc3 tiny example

This is a discussion on newbie jdbc3 tiny example within the pgsql Interfaces jdbc forums, part of the PostgreSQL category; --> hi everyboy, I'm newbie with the jdbc and I wish to know how must be driven the connection to ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Interfaces jdbc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-15-2008, 11:41 PM
andy petrella
 
Posts: n/a
Default newbie jdbc3 tiny example

hi everyboy,

I'm newbie with the jdbc and I wish to know how must be driven the
connection to a first preparedstatement using jdbc3 (an pg 8.0.3).

My first throw (suspicious) was

[...]

private Connection db;
private Statement sql;
private DatabaseMetaData dbmd;

private PreparedStatement addDVD = null;

[...]
String hostname = "localhost:5432";
String database = "XXX";
String user ="abcdefg";
String psswd ="_______";


try {
Class.forName("org.postgresql.Driver"); //load the driver
db = DriverManager.getConnection("jdbcostgresql://"+hostname+"/"+database+"?"+"user="+username+"&password="+passw ord);
//connect to the db
dbmd = db.getMetaData(); //get MetaData to confirm connection
}catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Error Driver pas
trouvé", JOptionPane.ERROR_MESSAGE);
}catch (SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Error initialisation
connection", JOptionPane.ERROR_MESSAGE);
}

[...]

public PreparedStatement preparedAddDVD(){
try{
String insertion =
"INSERT INTO FICHES.DVD" +
"id," +
"titre_o," +
"titre_f," +
"affiche," +
"duree," +
"genre," +
"resume," +
"visites," +
"cote," +
"imdb," +
"directeurs," +
"scenaristes," +
"acteurs," +
"possession_id," +
"possesseur," +
"acquis," +
"achete," +
"classement" +
"VALUES" +
"(" +
"default," +
"?," + //titreo 1
"?," + //sortie 2
"?," + //fr 3
"?," + //affiche 4
"?," + //duree 5
"?," + //genre 6
"resume," + //TODO null => allocine
"1," + //1 visites a faire après
"0," + //0 cote
"?," + //imdb 7
"?," + //dir 8
"?," + //sce 9
"?," + //act 10
"default," +
"current_user," +
"current_timestamp," +
"?," + //acheté 11
"?," + //classement 12
")" +
";";
addDVD = db.prepareStatement(insertion);
return addDVD;
}catch (SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage(), "Error prepare addDVD",
JOptionPane.ERROR_MESSAGE);
return null;
}

[...]


And, what about the org.postgresql.jdbc3.Jdbc3Connection, ...
the org.postgresql.PGConnection ...

If it's little too long to explain someone can give me some links ?

tks.


--
/**\_____/**\_____/**\_____/**\_____/**\
] Signature :
] Andy Petrella
] Rue dessous l'église, 17
] 4450 Slins
] mail: andy.petrella@gmail.com
] <licencié en math, info>
] dea S.I.G. (2005-2006)
\**/"""""""\**/"""""""\**/"""""""\**/"""""""\**/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 11:41 PM
Andres Olarte
 
Posts: n/a
Default Re: newbie jdbc3 tiny example

It seems ok to me. The idea with JDBC is that you don't have to deal
with implementation specific classes, such as PGConnection,
Jdbc3Connection, etc. It's all done on the background. If you are
doing client side prepared statements, your code looks OK. Prepared
Statements are a JDBC feature, common to all JDBC implementations, so
you don't need any PG specific code. If you are trying to do Server
side prepared statements, that's something else. But if you're new to
this, stick to JDBC prepared statements, and keep your code portable
that way.

On 10/18/05, andy petrella <andy.petrella@gmail.com> wrote:
> hi everyboy,
>
> I'm newbie with the jdbc and I wish to know how must be driven the
> connection to a first preparedstatement using jdbc3 (an pg 8.0.3).
>
> My first throw (suspicious) was
>
> [...]
>
> private Connection db;
> private Statement sql;
> private DatabaseMetaData dbmd;
>
> private PreparedStatement addDVD = null;
>
> [...]
> String hostname = "localhost:5432";
> String database = "XXX";
> String user ="abcdefg";
> String psswd ="_______";
>
>
> try {
> Class.forName("org.postgresql.Driver"); //load the driver
> db =
> DriverManager.getConnection("jdbcostgresql://"+hostname+"/"+database+"?"+"user="+username+"&password="+passw ord);
> //connect to the db
> dbmd = db.getMetaData(); //get MetaData to confirm connection
> }catch (ClassNotFoundException e){
> JOptionPane.showMessageDialog(null, e.getMessage(), "Error
> Driver pas trouvé", JOptionPane.ERROR_MESSAGE);
> }catch (SQLException e){
> JOptionPane.showMessageDialog(null, e.getMessage(), "Error
> initialisation connection", JOptionPane.ERROR_MESSAGE);
> }
>
> [...]
>
> public PreparedStatement preparedAddDVD(){
> try{
> String insertion =
> "INSERT INTO FICHES.DVD" +
> "id," +
> "titre_o," +
> "titre_f," +
> "affiche," +
> "duree," +
> "genre," +
> "resume," +
> "visites," +
> "cote," +
> "imdb," +
> "directeurs," +
> "scenaristes," +
> "acteurs," +
> "possession_id," +
> "possesseur," +
> "acquis," +
> "achete," +
> "classement" +
> "VALUES" +
> "(" +
> "default," +
> "?," + //titreo
> 1
> "?," + //sortie
> 2
> "?," + //fr
> 3
> "?," + //affiche
> 4
> "?," + //duree
> 5
> "?," + //genre
> 6
> "resume," + //TODO null => allocine
> "1," + //1 visites a faire après
> "0," + //0 cote
> "?," + //imdb
> 7
> "?," + //dir
> 8
> "?," + //sce
> 9
> "?," + //act
> 10
> "default," +
> "current_user," +
> "current_timestamp," +
> "?," + //acheté
> 11
> "?," + //classement
> 12
> ")" +
> ";";
> addDVD = db.prepareStatement(insertion);
> return addDVD;
> }catch (SQLException e){
> JOptionPane.showMessageDialog(null, e.getMessage(), "Error
> prepare addDVD", JOptionPane.ERROR_MESSAGE);
> return null;
> }
>
> [...]
>
>
> And, what about the org.postgresql.jdbc3.Jdbc3Connection,
> ...
> the org.postgresql.PGConnection ...
>
> If it's little too long to explain someone can give me some links ?
>
> tks.
>
>
> --
> /**\_____/**\_____/**\_____/**\_____/**\
> ] Signature :
> ] Andy Petrella
> ] Rue dessous l'église, 17
> ] 4450 Slins
>
> ] mail: andy.petrella@gmail.com
> ] <licencié en math, info>
> ] dea S.I.G. (2005-2006)
> \**/"""""""\**/"""""""\**/"""""""\**/"""""""\**/


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com