Vanavah Edwards
asked on
How to Fix Insert Statement into SQLite3 DB
I am trying to insert the fields from my JComponents into a table SOHEAD. I keep getting the error below on line 49 which is the "Insert into SOHEAD" line. The only JFormattedTextField in that line is SODATE. The table column is DATE type size 10.
Snippet ID=8241213 shows how I have that component delcared.
Snippet ID=8241224 shows the insert statement.
ERROR ON THE INSERT LINE -->
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
java.sql.SQLException: near "'javax.swing.JFormattedTe xtField[,1 14,54,80x2 0,layout=j avax.swing .plaf.basi c.BasicTex tUI$Update Handler,al ignmentX=0 .0,alignme ntY=0.0,bo rder=javax .swing.pla f.BorderUI Resource$C ompoundBor derUIResou rce@1f0aec c,flags=29 6,maximumS ize=,minim umSize=,pr eferredSiz e=,caretCo lor=sun.sw ing.PrintC olorUIReso urce[r=51, g=51,b=51] ,disabledT extColor=j avax.swing .plaf.Colo rUIResourc e[r=184,g= 207,b=229] ,editable= true,margi n=javax.sw ing.plaf.I nsetsUIRes ource[top= 0,left=0,b ottom=0,ri ght=0],sel ectedTextC olor=sun.s wing.Print ColorUIRes ource[r=51 ,g=51,b=51 ],selectio nColor=jav ax.swing.p laf.ColorU IResource[ r=184,g=20 7,b=229],c olumns=0,c olumnWidth =0,command =,horizont alAlignmen t=LEADING] '": syntax error
at org.sqlite.DB.throwex(DB.j ava:288)
at org.sqlite.NativeDB.prepar e(Native Method)
at org.sqlite.DB.prepare(DB.j ava:114)
at org.sqlite.Stmt.executeUpd ate(Stmt.j ava:102)
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:49 )
at MOCA$ActionPerformer.actio nPerformed (MOCA.java :124)
at javax.swing.AbstractButton .fireActio nPerformed (Unknown Source)
at javax.swing.AbstractButton $Handler.a ctionPerfo rmed(Unkno wn Source)
at javax.swing.DefaultButtonM odel.fireA ctionPerfo rmed(Unkno wn Source)
at javax.swing.DefaultButtonM odel.setPr essed(Unkn own Source)
at javax.swing.plaf.basic.Bas icButtonLi stener.mou seReleased (Unknown Source)
at java.awt.Component.process MouseEvent (Unknown Source)
at javax.swing.JComponent.pro cessMouseE vent(Unkno wn Source)
at java.awt.Component.process Event(Unkn own Source)
at java.awt.Container.process Event(Unkn own Source)
at java.awt.Component.dispatc hEventImpl (Unknown Source)
at java.awt.Container.dispatc hEventImpl (Unknown Source)
at java.awt.Component.dispatc hEvent(Unk nown Source)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Unknow n Source)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Unknown Source)
at java.awt.LightweightDispat cher.dispa tchEvent(U nknown Source)
at java.awt.Container.dispatc hEventImpl (Unknown Source)
at java.awt.Window.dispatchEv entImpl(Un known Source)
at java.awt.Component.dispatc hEvent(Unk nown Source)
at java.awt.EventQueue.dispat chEvent(Un known Source)
at java.awt.EventDispatchThre ad.pumpOne EventForFi lters(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForFilt er(Unknown Source)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Unkn own Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.pumpEve nts(Unknow n Source)
at java.awt.EventDispatchThre ad.run(Unk nown Source)
Snippet ID=8241213 shows how I have that component delcared.
Snippet ID=8241224 shows the insert statement.
ERROR ON THE INSERT LINE -->
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
java.sql.SQLException: near "'javax.swing.JFormattedTe
at org.sqlite.DB.throwex(DB.j
at org.sqlite.NativeDB.prepar
at org.sqlite.DB.prepare(DB.j
at org.sqlite.Stmt.executeUpd
at insertNewMoccaRecord.<init
at MOCA$ActionPerformer.actio
at javax.swing.AbstractButton
at javax.swing.AbstractButton
at javax.swing.DefaultButtonM
at javax.swing.DefaultButtonM
at javax.swing.plaf.basic.Bas
at java.awt.Component.process
at javax.swing.JComponent.pro
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Window.dispatchEv
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
public static JFormattedTextField sodate, delivdate;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
sodate = new JFormattedTextField(df);
sodate.setValue(new Date());
// INSERT MEMORY VARIABLES FROM JComponents INTO TABLE
int rsInsert = st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) " +
"VALUES '"+sodate+"', '"+custno+"', '"+mimporter+"', keycolumn");
ResultSet rsKey = st.getGeneratedKeys();
rsKey.next();
System.out.println("Generated key - "+rsKey);
("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) " +
" VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"', keycolumn");
st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"', keycolumn)");
for howmany columsn you want to insert?
what is keycolumn in VALUES ?Is it a column or value?
what is keycolumn in VALUES ?Is it a column or value?
why dont you use preparedstatement?
http://stackoverflow.com/questions/2256504/insert-java-variable-using-java-in-sql
http://stackoverflow.com/questions/2256504/insert-java-variable-using-java-in-sql
ASKER
You were right. Howevef, I am now getting a syntax error. By the way, what is the purpose of the keycolumn in the insert line.
NEW ERROR -->
java.sql.SQLException: near "keycolumn": syntax error
at org.sqlite.DB.throwex(DB.j ava:288)
at org.sqlite.NativeDB.prepar e(Native Method)
at org.sqlite.DB.prepare(DB.j ava:114)
at org.sqlite.Stmt.executeUpd ate(Stmt.j ava:102)
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:49 )
NEW ERROR -->
java.sql.SQLException: near "keycolumn": syntax error
at org.sqlite.DB.throwex(DB.j
at org.sqlite.NativeDB.prepar
at org.sqlite.DB.prepare(DB.j
at org.sqlite.Stmt.executeUpd
at insertNewMoccaRecord.<init
ASKER
It is the values from the fields in my form that I am inserting into the table.
ASKER
The data from the fields in the form are of different data types.
Sodate is a JFormattedTexField Date; buyer, buyer are text; some are numeric
Sodate is a JFormattedTexField Date; buyer, buyer are text; some are numeric
add one more column in INSERT statement.
st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer,keycolumn) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"'", keycolumn+")");
if you are inserting 4 columns use above one;
as i told you instead of Statement use prepared statement.
st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer,keycolumn) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"'," keycolumn+")");
ASKER
I will take your advice and use prepared statements. I removed the keycolumn as not in the table. Below is the new error. The "'SOLAR DYNAMICS LTD.'" is the value in the '"+mimporter+"' section of the insert.
java.sql.SQLException: near "'SOLAR DYNAMICS LTD.'": syntax error
at org.sqlite.DB.throwex(DB.j ava:288)
at org.sqlite.NativeDB.prepar e(Native Method)
at org.sqlite.DB.prepare(DB.j ava:114)
at org.sqlite.Stmt.executeUpd ate(Stmt.j ava:102)
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:49 )
java.sql.SQLException: near "'SOLAR DYNAMICS LTD.'": syntax error
at org.sqlite.DB.throwex(DB.j
at org.sqlite.NativeDB.prepar
at org.sqlite.DB.prepare(DB.j
at org.sqlite.Stmt.executeUpd
at insertNewMoccaRecord.<init
paste your code.
st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer,keycolumn) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"')");
sorry remove keycolumn
pstmt.executeUpdate("INSER T INTO SOHEAD " +
"(sodate, buyerno, buyer,keycolumn) VALUES (?,?,?)");
"(sodate, buyerno, buyer,keycolumn) VALUES (?,?,?)");
pstmt.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) VALUES (?,?,?)");
ASKER
I changed to the prepared statement. Same error -->
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
java.sql.SQLException: near "'SOLAR DYNAMICS LTD.'": syntax error
at org.sqlite.DB.throwex(DB.j ava:288)
at org.sqlite.NativeDB.prepar e(Native Method)
at org.sqlite.DB.prepare(DB.j ava:114)
at org.sqlite.PrepStmt.<init> (PrepStmt. java:37)
at org.sqlite.Conn.prepareSta tement(Con n.java:231 )
at org.sqlite.Conn.prepareSta tement(Con n.java:224 )
at org.sqlite.Conn.prepareSta tement(Con n.java:213 )
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:49 )
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
java.sql.SQLException: near "'SOLAR DYNAMICS LTD.'": syntax error
at org.sqlite.DB.throwex(DB.j
at org.sqlite.NativeDB.prepar
at org.sqlite.DB.prepare(DB.j
at org.sqlite.PrepStmt.<init>
at org.sqlite.Conn.prepareSta
at org.sqlite.Conn.prepareSta
at org.sqlite.Conn.prepareSta
at insertNewMoccaRecord.<init
PreparedStatement pst = connection.prepareStatement(
"INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"'");
int rsInsert = pst.executeUpdate();
ResultSet rsKey = pst.getGeneratedKeys();
rsKey.next();
System.out.println("Generated key - "+rsKey);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i dont understand your last post. You have values and pst.setSTring(1,sodate) etc. You are saying I have to use both or use the setString syntax.
ASKER
I change to your new code here is the new error -->
Connection successful!
java.sql.SQLException: near "pst": syntax error
at org.sqlite.DB.throwex(DB.j ava:288)
at org.sqlite.NativeDB.prepar e(Native Method)
at org.sqlite.DB.prepare(DB.j ava:114)
at org.sqlite.PrepStmt.<init> (PrepStmt. java:37)
at org.sqlite.Conn.prepareSta tement(Con n.java:231 )
at org.sqlite.Conn.prepareSta tement(Con n.java:224 )
at org.sqlite.Conn.prepareSta tement(Con n.java:213 )
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:51 )
at MOCA$ActionPerformer.actio nPerformed (MOCA.java :124)
Connection successful!
java.sql.SQLException: near "pst": syntax error
at org.sqlite.DB.throwex(DB.j
at org.sqlite.NativeDB.prepar
at org.sqlite.DB.prepare(DB.j
at org.sqlite.PrepStmt.<init>
at org.sqlite.Conn.prepareSta
at org.sqlite.Conn.prepareSta
at org.sqlite.Conn.prepareSta
at insertNewMoccaRecord.<init
at MOCA$ActionPerformer.actio
PreparedStatement pst = connection.prepareStatement(
"INSERT INTO SOHEAD " +
"pst.setString(1,sodate);" +
"pst.setString(2,custno);" +
"pst.setString(3,mimporter);");
int rsInsert = pst.executeUpdate();
ResultSet rsKey = pst.getGeneratedKeys();
rsKey.next();
System.out.println("Generated key - "+rsKey);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I studied the examples and change the code. But I am getting this error.
ERROR -->>
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB oundsExcep tion: 3
at org.sqlite.PrepStmt.batch( PrepStmt.j ava:131)
at org.sqlite.PrepStmt.setStr ing(PrepSt mt.java:18 6)
at insertNewMoccaRecord.<init >(insertNe wMoccaReco rd.java:55 )
ERROR -->>
Clicked - Save
Table CELL_SOHEAD exists
Connection successful!
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfB
at org.sqlite.PrepStmt.batch(
at org.sqlite.PrepStmt.setStr
at insertNewMoccaRecord.<init
PreparedStatement pst = connection.prepareStatement(
"INSERT INTO CELL_SOHEAD " +
"(sonum, sodate, buyerno, buyer) " +
"VALUES (?, ?, ?, ?)");
pst.setString(2,"+sonum+");
pst.setString(3,"+sodate+");
pst.setString(5,"+custno+");
pst.setString(6,"mimporter+");
ASKER
I never heard back from you when you kenw I neded help. I am new to Java. However, I tried the code you posted and studied the code on the site you suggested. By trial and error based upon all your recommendations, I am happy to say I have solved the problem. I would like to close this session and award your points. But there is one thing I need your help. How can I use
pst.setString(2, sodate.getText()); to store a date.
Unless I set the date in the database to DATETEXT it wouldn't work. If I say sodate.getDate() I get a String Error on that line. Is it possible to set a real date instead of a DATETEXT.
pst.setString(2, sodate.getText()); to store a date.
Unless I set the date in the database to DATETEXT it wouldn't work. If I say sodate.getDate() I get a String Error on that line. Is it possible to set a real date instead of a DATETEXT.
ASKER
Didi not get ever back to me after a while?
pst.setDate
or
pst.setTimestamp methods are there;check the methods in prepared statements
or
pst.setTimestamp methods are there;check the methods in prepared statements
ASKER
i tried pst.setDate(2, sodate); and I got this error -->
The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (int, JFormattedTextField)
My sodate is a JFormattedTextField
For the timestamp , I got this error --->>
The method setTimestamp(int, Timestamp) in the type PreparedStatement is not applicable for the arguments (int, JFormattedTextField)
Only pst.setString(2, sodate.getText()); works and I must set the DB to DATETEXT
The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (int, JFormattedTextField)
My sodate is a JFormattedTextField
For the timestamp , I got this error --->>
The method setTimestamp(int, Timestamp) in the type PreparedStatement is not applicable for the arguments (int, JFormattedTextField)
Only pst.setString(2, sodate.getText()); works and I must set the DB to DATETEXT
VALUES(
st.executeUpdate("INSERT INTO SOHEAD " +
"(sodate, buyerno, buyer) " +
"VALUES ('"+sodate+"', '"+custno+"', '"+mimporter+"', keycolumn");