wood burning stoves*
The moose likes JDBC and the fly likes mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; " Watch "mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; " New topic
Author

mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;

Felix Amboga
Greenhorn

Joined: Apr 25, 2013
Posts: 1
Connection cn;
Statement st;
PreparedStatement pstmt=null;
PreparedStatement pst;
ResultSet rs;
Object fname, mname, lname, bdate, nation, statusq,InstNo, photo, combo, place, mimi;
int status;



public Brothers() {
dbconnect _db = new dbconnect();
try {
Class.forName("com.mysql.jdbc.Driver");
//String unicode= "?useUnicode=yes&characterEncoding=UTF-8";
cn = DriverManager.getConnection(_db.getHost(), _db.getUsername(), _db.getPassword());
st=cn.createStatement();
rs=st.executeQuery("select * from countries");



}
catch (Exception e){
e.printStackTrace();
}




private void btnNextMouseClicked(java.awt.event.MouseEvent evt) {


try{

String Sql="INSERT INTO brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) VALUES(?,?,?,?,?,?,?,?,?)";
pstmt=cn.prepareStatement(Sql);
//pstmt.setString(1,txtTrial.getText());
pstmt.setString(1,txtFirtsName.getText());
pstmt.setString(2,txtMiddleName.getText());
pstmt.setString(3,txtLastName.getText());
pstmt.setString(4,((JTextField)chooserBirthDate.getDateEditor().getUiComponent()).getText());
pstmt.setString(5,txtPlacBirth.getText());

String nations=combonation.getSelectedItem().toString();
pstmt.setString(6,nations);
pstmt.setString(7,txtInstituteNo.getText());


pstmt.setObject(8,combostatus.getSelectedItem());
pstmt.setBytes(9, person_image);

pstmt.executeUpdate(Sql);

JOptionPane.showMessageDialog(null, "Saving Successfull");


catch(Exception e){

//JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}

I have a table with ten columns: BrothersID,FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture. BrothersID is the key with an auto increment meaning I didnt have to enter anything on it.

when I run this code ....I get this exception:


[color=orange]com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?,?,?,?)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
at brotherssystem.Brothers.btnNextMouseClicked(Brothers.java:411)
at brotherssystem.Brothers.access$800(Brothers.java:20)
at brotherssystem.Brothers$9.mouseClicked(Brothers.java:295)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.Component.processMouseEvent(Component.java:6266)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
[/color]

But when I use this I am able to save the data in the database except for the picture. Please help!!!show me where I am going wrong

String addrecords="insert into brothers(FirstName, MiddleName, LastName, BirthDate, BirthPlace, Nationality, InstituteNumber, Status, Picture) values('"+
fname +"', '" +
mname +"', '" +
lname +"', '" +
dave +"', '" +
place +"', '" +
isa +"', '" +
InstNo +"', '" +
statusq +"', '" +
photo +"')";

//wrapField();

st.executeUpdate(addrecords);

}
K. Gil
Ranch Hand

Joined: Apr 29, 2011
Posts: 75

pstmt.executeUpdate(Sql);


com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?,?,?,?)' at line 1


are you sure you want to execute your query from defined Sql string instead of prepared statement?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
 
Similar Threads
SQL Remove.
Updating a file from an object (ArrayList)
SQL formatting question
problem with insert and select
submitting the applet input details to database