You need a different SQL statement. A valid insert statement is:


INSERT INTO table_name ( column_name ) VALUES ( column_value )

So, in your case:

updateInfo = con.prepareStatement("insert into info ( referrer ) values ( ? )");

Also, in your try/catch block, don't throw that new Exception, you will just confuse things by adding an additional layer. All you need is:

try {

 ....
 ....

} catch (SQLException sqle) {

System.err.println("SQL Error: " + sqle.printStackTrace());

}

If you need to catch other exceptions, just list them in additional catch blocks:

try {

} catch (SQLException sqle) {

} catch (IOException ioe) {

}

John

On Mon, 24 Mar 2003 20:38:14 -0800, Wileynet <[EMAIL PROTECTED]> wrote:

Ok, I tried that. Here is the new code....
public void addInfo( String referer )
throws SQLException, Exception {
if (con != null) {
try{
PreparedStatement updateInfo;
updateInfo = con.prepareStatement(
"insert into info(?);");
updateInfo.setString(1, referer );
updateInfo.execute();


} catch (SQLException sqle) { System.err.println( "HELP" + sqle ); } } else { error = "Connection with database was lost."; throw new Exception( error ); } }

Now, I get no error but the database does not get updated ? This is
basically my database - >
mysql> desc info;
+-------------+-----------+------+-----+---------+-------+
| Field       | Type      | Null | Key | Default | Extra |
+-------------+-----------+------+-----+---------+-------+
| PRIMARY_KEY | int(11)   |      | PRI | 0       |       |
| REFERER     | char(200) | YES  |     | NULL    |       |
+-------------+-----------+------+-----+---------+-------+

Please help again :)




-----Original Message-----
From: Mark Matthews [mailto:[EMAIL PROTECTED] Sent: Monday, March 24, 2003 7:26 PM
To: Wileynet
Cc: [EMAIL PROTECTED]
Subject: Re: java.sql.SQLException: Update failed, possible duplicate
entry


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Wileynet wrote:
Here is another beginner question of sorts I imagine.
I receive this error.
java.sql.SQLException: Update failed, possible duplicate entry



I've got this code in a .jsp page.

<% String ref=request.getHeader("Referer");%>
<BR>
<%   count.addInfo( ref );
%>

count is calling the method addInfo
in a java bean...


public void addInfo( String Referer ) throws SQLException, Exception { if (con != null) { try{ PreparedStatement updateInfo; updateInfo = con.prepareStatement( "insert into info(?);"); updateInfo.setString(1,Referer); updateInfo.execute(); } catch (SQLException sqle) { error = "Update failed, possible duplicate entry"; throw new SQLException( error ); } } else { error = "Connection with database was lost."; throw new Exception( error ); } }

Can anyone see the mistake I am making here. My database has one table
now called info with one String field which is the primary key also.

Thanks if anyone can help or point me to a webpage or something.

-wiley


Also, looking at your code, you have a SQL syntax error. Only the mysql command-line client requires statements terminated with a ";", so you should write:

updateInfo = con.prepareStatement("insert into info(?)");

not:

updateInfo = con.prepareStatement("insert into info(?);");

-Mark


- -- MySQL 2003 Users Conference -> http://www.mysql.com/events/uc2003/


For technical support contracts, visit https://order.mysql.com/?ref=mmma

__  ___     ___ ____  __
/  |/  /_ __/ __/ __ \/ /  Mark Matthews <[EMAIL PROTECTED]>
/ /|_/ / // /\ \/ /_/ / /__ MySQL AB, Full-Time Developer - JDBC/Java
/_/  /_/\_, /___/\___\_\___/ Flossmoor (Chicago), IL USA
<___/ www.mysql.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.1.90 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE+f8xFtvXNTca6JD8RAhArAJ98UE5FFTYc4iok9r62OgjJiIFm+QCgnnhk
oxDuUCf1Qz/tOIzNCkEQcV4=
=VwpN
-----END PGP SIGNATURE-----


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]





-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to