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.