I'm trying to make a sql query builder type program that uses user input data to build custom queries for the table
我正在尝试创建一个SQL查询构建器类型程序,该程序使用用户输入数据为表构建自定义查询
so far i have
到目前为止我有
public int checkBetweenDates() throws SQLException{
String t1 = "2015-07-08"; //or later some user input variable
String t2 = "2015-07-09";//or later some user input variable
String id = "22 03 E7 99";//or later some user input variable
int rowCount = -1;
//Statement stmt = null;
String dateChoice = "select count(*) "
+ "from dancers "
+ "where ts between (t1) and (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();
}
finally {
rs.close();
preparedStmt.close();
}
return rowCount;
}
So it connects and everything fine but it doesnt execute the query saying something wrong with the sql syntax for values(?,?,?)
所以它连接并且一切正常,但它没有执行查询说sql语法错误的值(?,?,?)
Any help would be awesome thanks guys!!
任何帮助都会很棒,谢谢你!
Carl
1
Try this, Changes in query and in setting prepared statement parameters,
试试这个,查询中的更改以及设置预准备语句参数,
public int checkBetweenDates() throws SQLException{
String t1 = "2015-07-08"; //or later some user input variable
String t2 = "2015-07-09";//or later some user input variable
String id = "22 03 E7 99";//or later some user input variable
int rowCount = -1;
//Statement stmt = null;
String dateChoice = "select count(*) "
+ "from dancers "
+ "where ts between ? and ?"
+ "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();
}
finally {
rs.close();
preparedStmt.close();
}
return rowCount;
}
Share the exact error if doesn't work for you.
如果不适合您,请分享确切的错误。
0
Change this:
String dateChoice = "select count(*) "
+ "from dancers "
+ "where ts between (t1) and (t2)"
+ "and id = (id)"
+ "values (?)";
According to the database syntax that you are using. For example if you using a webserver with Mysql go and type the query to see where the typo is. (if you using mysql it needs dancers
to every table)
根据您使用的数据库语法。例如,如果您使用带有Mysql的Web服务器,请输入查询以查看拼写错误的位置。 (如果你使用mysql它需要每个表的舞者)
0
First, you seem to have edited this method many times to try fix the problem, which has left it in a confused state.
首先,您似乎已多次编辑此方法以尝试修复问题,这使其处于混乱状态。
remove the "values (?)" from the sql statement, it does not belong here, it seems to be left over from a prepared insert statement.
从sql语句中删除“values(?)”,它不属于这里,它似乎是从一个准备好的insert语句中遗留下来的。
call preparedStmt.executeQuery() with zero arguments, you have already supplied it with the sql string and only call it ONCE, you assign a value to rs twice.
调用带有零参数的preparedStmt.executeQuery(),你已经为它提供了sql字符串并且只调用它ONCE,你为rs赋值两次。
your sql statement should contain exactly three question marks, try
你的sql语句应该包含三个问号,试试
select count(*) from dancers where ts between ? and ? and id = ?
next call preparedStmt.setString() three times to supply values t1, t2 and id.
接下来调用prepareStmt.setString()三次来提供值t1,t2和id。
Also, remember to close the connection object in the finally block.
另外,请记住在finally块中关闭连接对象。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/07/10/81f2f338b1bcae0e34bdc666ae7cfc79.html。