扩展协议查询数据

最近更新时间: 2024-10-17 17:10:00

import java.sql.*;
import java.text.SimpleDateFormat;   
public class select_normal {
    public static void main(String args[]) {
    Connection c = null;
    Statement stmt = null;
    try {
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://172.16.0.30:11345/postgres","tbase", "tbase");
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");
        long start_time = System.currentTimeMillis();
        PreparedStatement preparedStatement = null;
        try {             
            String sql = "select * from public.select_normal where f2 >= ? and f2 < ? " ;   
            preparedStatement = c.prepareStatement(sql);
            String deskdate_start = "2020-01-01";
            String deskdate_end = "2020-02-01";
            Date deskdate_start_date = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(deskdate_start).getTime());
            Date deskdate_end_date = new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd").parse(deskdate_end).getTime());
            preparedStatement.setDate(1,deskdate_start_date);
            preparedStatement.setDate(2,deskdate_end_date);
            ResultSet resultSet = preparedStatement.executeQuery();
            System.out.println("执行查询耗时:"+(System.currentTimeMillis()-start_time)+"ms");
            while(resultSet.next()){
            System.out.println(resultSet.getString(1));
            System.out.println(resultSet.getString(2)); 
            System.out.println(resultSet.getString(3)); 
        }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        System.err.println( e.getClass().getName()+": "+ e.getMessage() );
        System.exit(0);
    }
        System.out.println("Records created successfully");
    }
}