Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2004

Tip: Looking for answers? Try searching our database.

Simple INSERT question,

Thread view: 
Niall - 16 Apr 2004 15:28 GMT
Hi, I was wondering if anyone could halp with this simple question which is
causing me much pain. Im trying to insert two items into a access database
and cant heres what I have done.

con = DriverManager.getConnection(conStr);/
Statement stmt = con.createStatement();
 
    stmt.executeUpdate("INSERT INTO Login (Server_GUI.log,
Server_GUI.pass);");

I've never done this before( as you can see) would really appreceate some
help.

Thanks In advance
Niall Mulhare
Dieter Bender - 16 Apr 2004 15:59 GMT
Niall,

Simple Answer, your program is not correct!

Dieter

> Hi, I was wondering if anyone could halp with this simple question which
> is causing me much pain. Im trying to insert two items into a access
[quoted text clipped - 11 lines]
> Thanks In advance
> Niall Mulhare
Mark Hansen - 16 Apr 2004 16:17 GMT
> Hi, I was wondering if anyone could halp with this simple question which is
> causing me much pain. Im trying to insert two items into a access database
[quoted text clipped - 11 lines]
> Thanks In advance
> Niall Mulhare

I hope this will get you going ... you've got a way to go...

First, you can execute a statement like this:

  stmt.executeUpdate("INSERT INTO Login VALUES ( 'foo', 'bar' )");

These are static statements, in that the values are hard coded at
the time the Java source was compiled.

If you need to provide Dynamic values (that is, value that are not
known until the program is run) then you need to use Dynamic
statements.

The statement will now look like this:

  "INSERT INTO Login VALUES (?, ?)"

and you will need to use a Prepared Statement, rather than a statement:

  PreparedStatement pstmt = con.prepareStatement("INSERT INTO Login VALUES ( ?, ? )");

then bind the variable values using the pstmt.setXXX methods, like
this:

  pstmt.setString(1, stringVariableName);
  pstmt.setString(2, secondStringVariableName);

etc. There are different pstmt.setXXX methods for each data type. The
first parameter is the index of the place holder (?) in the statement
text, indexed from 1.

See the Javadoc for java.sql.PreparedStatement, which shows all of this.
Niall - 16 Apr 2004 18:44 GMT
This is what I have going now, its just geting stuck on the executeUpdate
and not updateing atall, would it be due to not having access on this pc,
I didn't think that would be issue since Im able to read and print to
screen the contence of the DB.

PreparedStatement pstmt = con.prepareStatement("INSERT    INTO Login
VALUES ( ?, ? )");   
   
pstmt.setString(1, Server_GUI.log);
pstmt.setString(2, Server_GUI.pass);
   
pstmt.executeUpdate("INSERT INTO Login VALUES ( '?', '?' )");

Thanks Again.
Mark Hansen - 16 Apr 2004 18:50 GMT
> This is what I have going now, its just geting stuck on the executeUpdate
> and not updateing atall, would it be due to not having access on this pc,
[quoted text clipped - 8 lines]
>    
> pstmt.executeUpdate("INSERT INTO Login VALUES ( '?', '?' )");

  pstmt.executeUpdate();

Have a look at the Javadoc for the java.sql.PreparedStatement class.
Javadoc is your friend.

> Thanks Again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.