AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Simple INSERT question,

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases
View previous topic :: View next topic  
Author Message
Niall
Guest





PostPosted: Fri Apr 16, 2004 2:28 pm    Post subject: Simple INSERT question, Reply with quote



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

Back to top
Dieter Bender
Guest





PostPosted: Fri Apr 16, 2004 2:59 pm    Post subject: Re: Simple INSERT question, Reply with quote



Niall,

Simple Answer, your program is not correct!

Dieter

Niall wrote:

Quote:
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


Back to top
Mark Hansen
Guest





PostPosted: Fri Apr 16, 2004 3:17 pm    Post subject: Re: Simple INSERT question, Reply with quote



On 4/16/2004 07:28, Niall wrote:

Quote:
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


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.

Back to top
Niall
Guest





PostPosted: Fri Apr 16, 2004 5:44 pm    Post subject: Re: Simple INSERT question, Reply with quote


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.

Back to top
Mark Hansen
Guest





PostPosted: Fri Apr 16, 2004 5:50 pm    Post subject: Re: Simple INSERT question, Reply with quote

On 4/16/2004 10:44, Niall wrote:

Quote:
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 ( '?', '?' )");

pstmt.executeUpdate();

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


Quote:

Thanks Again.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java and Databases All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.