pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
2+ messages / 2 participants
[nested] [flat]

* Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
@ 1999-02-18 00:46  Peter Garner <peter_garner@yahoo.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Peter Garner @ 1999-02-18 00:46 UTC (permalink / raw)
  To: Atika <agoswa@essex.ac.uk>; pgsql-sql@postgreSQL.org

Hi Atika!  :-)

> I am basically trying to do something like this:
> 
> String input = "This is a String";
> 
> myConn.updateQuery("insert into myDB values(input)");
> 
> but get an error when I  run it saying:
> ERROR:  attribute input not found
> java.sql.SQLException: ERROR:  attribute input not found

Try 

String input = "This is a String"
String query = insert into myDB value ('" + input + "')"

myConn.updateQuery(query);
==
Peace,
Peter

We are Microsoft of Borg, you will be assimilated!!!
Resistance is fut...  ***BZZZRT***  THUD!!!
[General Protection Fault in MSBorg32.DLL]
Please contact the vendor for more information
_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [SQL] INSERT query (using insert from a Java application, concerning String input)
@ 1999-02-21 15:45  Herouth Maoz <herouth@oumail.openu.ac.il>
  parent: Peter Garner <peter_garner@yahoo.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Herouth Maoz @ 1999-02-21 15:45 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: Atika <agoswa@essex.ac.uk>; Peter Garner <peter_garner@yahoo.com>

(Continue this thread on the INTERFACES list, where it belongs)

At 2:46 +0200 on 18/2/99, Peter Garner wrote:


> Hi Atika!  :-)
>
> > I am basically trying to do something like this:
> >
> > String input = "This is a String";
> >
> > myConn.updateQuery("insert into myDB values(input)");
> >
> > but get an error when I  run it saying:
> > ERROR:  attribute input not found
> > java.sql.SQLException: ERROR:  attribute input not found
>
> Try
>
> String input = "This is a String"
> String query = insert into myDB value ('" + input + "')"
>
> myConn.updateQuery(query);

Ahem. No, this may work, but that's not the way to go about it. In any
case, where did that "updateQuery" come from?

Atika, I have a feeling you are not well-acquainted with JDBC. Reccomended
reading:

http://www.javasoft.com/products/jdk/1.2/docs/guide/jdbc/getstart/introTOC.doc.h
tml

(All on the same line. Not my fault the URLs are that long at Javasoft).

The basic idea is to use a PreparedStatement. These statements include
placeholders, which you fill before you execute them. This enables you to
(a) reuse the same statements with different values, (b) use things other
than strings and (c) have the strings properly quoted and escaped for you.

Thus:

    PreparedStatement stmt =
        myConn.prepareStatement( "INSERT INTO myDB VALUES ( ? ) " );

    String input = "This is a string";

    stmt.setString( 1, input );

    stmt.executeUpdate();

Note that Peter's solution would not have worked if your input was:

    String input = "It's a beautiful day today."

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~1999-02-21 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-02-18 00:46 Re: [SQL] INSERT query (using insert from a Java application, concerning String input) Peter Garner <peter_garner@yahoo.com>
1999-02-21 15:45 ` Herouth Maoz <herouth@oumail.openu.ac.il>

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