File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException" Watch "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException" New topic
Author

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

Krishna Bobba
Greenhorn

Joined: Nov 07, 2012
Posts: 2
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)

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18132
    
    8

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

Joined: Nov 07, 2012
Posts: 2
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.
Irwin Raja
Greenhorn

Joined: Apr 29, 2012
Posts: 11
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

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Looks like you're trying to use a connection URL as a query.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
 
Similar Threads
SQL formatting question
MySQLSyntaxErrorException
How to get input from user and search from database and display results in a table format?
SQLGrammerException in Hibernate
connection of eclipse helios and mysql