查询数据

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

/*
表及数据
create table select_normal(f1 int not null ,f2 date not null,f2 varchar(10));
insert into select_normal values(1,'2020-01-01','tbase'),(1,'2020-02-01','tbase'); 
*/
import java.sql.*;     

public class select_bind {
    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 {       
            stmt = c.createStatement();
            ResultSet resultSet = stmt.executeQuery("select * from public.select_normal where f2 >= '2020-01-01' and f2< '2020-02-01'");
            while(resultSet.next()){
            System.out.println(resultSet.getString(1));
            System.out.println(resultSet.getString(2)); 
            System.out.println(resultSet.getString(3)); 
            }    
            resultSet.close();
            stmt.close(); 
            c.close(); 
        } 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");
    }
}