package mytest; import com.mysql.jdbc.util.BaseBugReport; import java.sql.Connection; import java.util.Properties; import java.sql.*; import java.util.ArrayList; import java.util.Calendar; import java.util.logging.*; public class mytest extends BaseBugReport { private static final Properties properties = new Properties(); { properties.put("autoReconnect","true"); properties.put("characterEncoding","UTF-8"); properties.put("characterSetResults","UTF-8"); properties.put("connectionCollation","utf8_slovenian_ci"); } public mytest() { } /** * Override this method with code that demonstrates the bug. * * @throws Exception if an error occurs during your test run. */ public void runTest() throws Exception { Logger.getLogger(this.getClass().toString()).entering("runTest",""); Connection conn = getConnection(getUrl(), properties); Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.set(2006,2,3); Date date = new java.sql.Date(calendar.getTime().getTime()); PreparedStatement pstmt = conn.prepareStatement("select * from testdata"); ResultSet rs = pstmt.executeQuery(); ArrayList r1=new ArrayList(); rs.beforeFirst(); while (rs.next() && !rs.isAfterLast()) { r1.add(rs.getObject(1)); } rs.close(); Logger.getLogger(this.getClass().toString()).log(Level.INFO, "select * from testdata returned "+Integer.toString(r1.size())+" rows"); /**BUG HERE**/ pstmt = conn.prepareStatement("select * from testdata where ? between a and b"); pstmt.clearParameters(); pstmt.setObject(1, date); rs = pstmt.executeQuery(); ArrayList r2=new ArrayList(); rs.beforeFirst(); while (rs.next() && !rs.isAfterLast()) { r2.add(rs.getObject(1)); } rs.close(); Logger.getLogger(this.getClass().toString()).log(Level.INFO, "select * from testdata where ? between a and b returned "+Integer.toString(r2.size())+" rows"); pstmt = conn.prepareStatement("select * from testdata where '"+date.toString()+"' between a and b"); rs = pstmt.executeQuery(); ArrayList r3=new ArrayList(); rs.beforeFirst(); while (rs.next() && !rs.isAfterLast()) { r3.add(rs.getObject(1)); } rs.close(); Logger.getLogger(this.getClass().toString()).log(Level.INFO, "select * from testdata where '"+date.toString()+"' between a and b returned "+Integer.toString(r3.size())+" rows"); if (r1.size()!=r2.size() && r2.size()!=r3.size()) { Logger.getLogger(this.getClass().toString()).log(Level.SEVERE, "FAILED"); } conn.close(); Logger.getLogger(this.getClass().toString()).exiting("runTest",""); } /** * Override this method with code that sets up the testcase for * demonstrating your bug (creating tables, populating data, etc). * * @throws Exception if an error occurs during the 'setUp' phase. */ public void setUp() throws Exception { Logger.getLogger(this.getClass().toString()).entering("setUp",""); Connection conn = getConnection(getUrl(), properties); Statement stmnt = conn.createStatement(); stmnt.execute("create table testdata ( id int not null auto_increment primary key, a date, b date ) "); stmnt.execute("insert into testdata values(null, '2006-01-01', '2006-05-05')"); conn.close(); } /** * Override this method with code that cleans up anything created in the * setUp() method. * * @throws Exception if an error occurs during the 'tearDown' phase. */ public void tearDown() throws Exception { Logger.getLogger(this.getClass().toString()).entering("tearDown",""); Connection conn = getConnection(getUrl(), properties); Statement stmnt = conn.createStatement(); stmnt.execute("drop table testdata"); conn.close(); } public static void main(String[] args) { mytest mytest = new mytest(); try { mytest.setUp(); try { mytest.runTest(); } catch (Exception ex) { ex.printStackTrace(); } mytest.tearDown(); } catch (Exception ex) { ex.printStackTrace(); } } }