Re: macaddr data type and prepared statements

From: Steve Foster <s(dot)p(dot)foster(at)leeds(dot)ac(dot)uk>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: macaddr data type and prepared statements
Date: 2008-08-05 08:46:05
Message-ID: 4898134D.8000102@leeds.ac.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi Folks,

OK Think I've got it sorted... At least I'm inserting using a prepared
statement, the following worked fine. Anyone got any suggestions as to
why I shouldn't do it this way, it feels right but its too simple so I'm
being cautious.

Cheers

Steve

----Code----

org.postgresql.util.PGobject MACADDR = new org.postgresql.util.PGobject();
MACADDR.setType("macaddr");
org.postgresql.util.PGobject CIDR = new org.postgresql.util.PGobject();
CIDR.setType("cidr");

try {

Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(jdbc_url, jdbc_user,
jdbc_pass);

PreparedStatement stmt = conn.prepareStatement("insert into
log (date, time, mac, network) values (?,?,?,?)");

while (inputLineIterator.hasNext()) {
String[] line = inputLineIterator.next();
if (line == null) {
continue;
}
MACADDR.setValue(line[2]);
CIDR.setValue(line[3]);

stmt.setDate(1, new java.sql.Date(
dfmt.parse(line[0]).getTime()) );
stmt.setTime(2, new java.sql.Time(
tfmt.parse(line[1]).getTime()) );
stmt.setObject(3, MACADDR);
stmt.setString(4, CIDR);
stmt.execute();
}

stmt.close();
}

----Code----

Oliver Jowett wrote:
> Dave Cramer wrote:
>
>> You can extend PGobject to create a PGMacaddr object to get it to
>> work with prepared statements
>
> Or you can put an explicit cast in your insert statement.
>
> -O
>

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Daniele Depetrini 2008-08-05 17:12:50 Pooled connections idle timeout
Previous Message Kris Jurka 2008-08-05 05:30:39 Re: macaddr data type and prepared statements