java - Using variables to create SQL statements -


i'm trying make sql query builder type program uses user input data build custom queries table

so far have

public int checkbetweendates() throws sqlexception{         string t1 = "2015-07-08"; //or later user input variable         string t2 = "2015-07-09";//or later user input variable         string id = "22 03 e7 99";//or later user input variable         int rowcount = -1;         //statement stmt = null;                  string datechoice = "select count(*) "                  + "from dancers "                  + "where ts between (t1) , (t2)"                 + "and id = (id)"                 + "values (?)";          connection conn = drivermanager.getconnection(host, username, password);         system.out.println("connected:");          preparedstatement preparedstmt = (preparedstatement) conn.preparestatement(datechoice);          preparedstmt.setstring    (1, t1); //       preparedstmt.setstring    (2, t2); //       preparedstmt.setstring    (3, id);         // stmt = conn.createstatement();         resultset rs = preparedstmt.executequery(datechoice);          try {                 rs = preparedstmt.executequery(datechoice);                 rs.next();                 rowcount = rs.getint(1);                 system.out.println(rowcount);             }          catch (sqlexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         {             rs.close();             preparedstmt.close();         }         return rowcount;       } 

so connects , fine doesnt execute query saying wrong sql syntax values(?,?,?)

any awesome guys!!

carl

try this, changes in query , in setting prepared statement parameters,

public int checkbetweendates() throws sqlexception{         string t1 = "2015-07-08"; //or later user input variable         string t2 = "2015-07-09";//or later user input variable         string id = "22 03 e7 99";//or later user input variable         int rowcount = -1;         //statement stmt = null;                  string datechoice = "select count(*) "                  + "from dancers "                  + "where ts between ? , ?"                 + "and id = ?";          connection conn = drivermanager.getconnection(host, username, password);         system.out.println("connected:");          preparedstatement preparedstmt = (preparedstatement) conn.preparestatement(datechoice);        preparedstmt.setstring    (1, t1);        preparedstmt.setstring    (2, t2);        preparedstmt.setstring    (3, id);         // stmt = conn.createstatement();         resultset rs = preparedstmt.executequery(datechoice);          try {                 rs = preparedstmt.executequery(datechoice);                 rs.next();                 rowcount = rs.getint(1);                 system.out.println(rowcount);             }          catch (sqlexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         {             rs.close();             preparedstmt.close();         }         return rowcount;       } 

share exact error if doesn't work you.


Comments