• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi someone please help me with the code sample. I have also attached the ERROR message I recieved. Thank you!

CODE_

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;




public class DatbaseTester {

public static void main(String args[]){
//Date dbtime = null;
String dbUrl = "jdbc:mysql://SAmple:3306/DB";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM transaction_log_extract_tb";
String sql = "select * from wdw_grs_std_tb dm join transaction_log_tb t on dm.transaction_id = t.transaction_id " +
"where date(t.server_tmstmp) between ? and ? ";

Calendar Cal = Calendar.getInstance();
Cal.set(2012,10,01);
Date startDate = new Date(Cal.getTime().getTime());
Cal.set(2012,11,07);
Date endDate = new Date(Cal.getTime().getTime());
System.out.println(startDate + " " + endDate);

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl,"usernamer","password");
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setDate(1, startDate);
stmt.setDate(2, endDate);
ResultSet rs = stmt.executeQuery(sql);

while (rs.next()) {
String dbtime = rs.getString(1);

System.out.println(dbtime);
} //end while
/*
String query2 = "insert into transaction_log_extract_tb values(?)";
stmt = con.prepareStatement(query2);
stmt.setDate(1, dbtime);
stmt.executeUpdate();
*/
con.close();
} //end try

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

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

} //end main

} //end class


ERROR-
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 '? and ?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2537)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2466)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1383)
at DatbaseTester.main(DatbaseTester.java:37)

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got the part about setting up the PreparedStatement all correct. Just one minor problem with how you tried to run it... instead of this:



do this:

 
Krishna Bobba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You got the part about setting up the PreparedStatement all correct. Just one minor problem with how you tried to run it... instead of this:



do this:




Thank you very much, somehow I wasted an hour on this one. It was hard to find.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your precious time..
But after changing those details, I'm getting new error
::
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 'jdbc:mysql://localhost:3306/sikka' at line 1

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you're trying to use a connection URL as a query.
 
reply
    Bookmark Topic Watch Topic
  • New Topic