· 9 years ago · Aug 05, 2017, 04:54 AM
1import java.sql.*;
2
3public class TestJDBC {
4
5 private static String table = "create table if not exists test(date timestamp,value int)";
6
7 public static void main(String... args) throws SQLException {
8 try (Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "test", "test"); Statement stmCreate = con.createStatement()) {
9 stmCreate.executeUpdate(table);
10 try (PreparedStatement pstmt1 = con.prepareStatement("select date_trunc('month', date) test, count(*) from test group by date_trunc('month', date)");
11 PreparedStatement pstmt2 = con.prepareStatement("select date_trunc(?, date) as col_0_0_, count(*) from test group by col_0_0_");
12 PreparedStatement pstmt3 = con.prepareStatement("select date_trunc(?, date) from test order by date_trunc(?, date)");
13 PreparedStatement pstmt4 = con.prepareStatement("select date_trunc(?, date) as col_0_0_, count(*) from test group by date_trunc(?, date)");
14 ) {
15 pstmt1.executeQuery();
16 System.out.println("pstmt1 ok");
17 pstmt2.setString(1, "month");
18 pstmt2.executeQuery();
19 System.out.println("pstmt2 ok");
20
21 pstmt3.setString(1, "month");
22 pstmt3.setString(2, "month");
23 pstmt3.executeQuery();
24 System.out.println("pstmt3 ok");
25
26 //not working
27 pstmt4.setString(1, "month");
28 pstmt4.setString(2, "month");
29 pstmt4.executeQuery();
30 System.out.println("pstmt4 ok");
31 }
32 }
33 }
34}