开发者

Prepared statement for MySQL so that it would pick a row containing the highest Timestamp for column that would be greater from given parameter?

How to write a Java prepared statement for MySQL so that it would pick a row containing the highest Timestamp value from a column that would be greater from given parameter?

I got as far as: SELECT * FROM table WHERE timestamp_col=(S开发者_运维问答ELECT MAX(timestamp_col) FROM table)

But how to convert it to prepared statement?


Since there's not really a parameter, it's pretty simple:

Connection con = null;
PreparedStatement prest;
try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection("jdbc:mysql://<server>:3306/<db>","<login>","<pass>");
  try{
    String sql = "SELECT * FROM table WHERE timestamp_col=(SELECT MAX(timestamp_col) FROM table)";
    prest = con.prepareStatement(sql);
    // prest.setInt(1,12000); // example of using a parameter for prepared statement
    // parameters go in the SQL as ?
    ResultSet rs1 = prest.executeQuery();
  } catch( SQLException s ) { /* etc */ }

You don't need a parameter if you're just asking for a MAX(). It's perfectly safe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜