• 连接pgsql


    package com.jpzhutech.select;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class SelectTest {
    
        public static void main(String[] args) {
            Connection connection = null;
            Statement statement = null;
            try {
                //String url = "jdbc:mysql://192.168.101.44/amon";
                String url = "jdbc:postgresql://192.168.100.120:5432/postgres";//换成自己PostgreSQL数据库实例所在的ip地址,并设置自己的端口
                //String user = "root";
                String user = "postgres";
                //String password = "560128";
                String password = "";  //在这里我的密码为空,读者可以自己选择是否设置密码
                //Class.forName("com.mysql.jdbc.Driver");
                Class.forName("org.postgresql.Driver");  //一定要注意和上面的MySQL语法不同
                connection= DriverManager.getConnection(url, user, password);
                System.out.println("是否成功连接pg数据库"+connection);
                String sql = "select * from student";
                statement = connection.createStatement();
                /**
                 * 关于ResultSet的理解:Java程序中数据库查询结果的展现形式,或者说得到了一个结果集的表
                 * 在文档的开始部分有详细的讲解该接口中应该注意的问题,请阅读JDK
                 * */
                ResultSet resultSet = statement.executeQuery(sql);
                while(resultSet.next()){
                     //取出列值
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    System.out.println(id+","+name+",");
    
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally{
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }finally{
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }
    
            }
        }
    
    }
    

      

  • 相关阅读:
    关于中间件(Middleware)的理解
    强类型约束的中间件(IMiddleware)
    常规中间件(Conventional Middleware) 之 自定义中间件
    常规中间件(Conventional Middleware) 之 内联中间件(in-line middleware)
    git 遴选(cherry-pick)
    sql转linq
    python知识体系
    when 的使用
    关于联表查询时NULL值的处理
    $project 选择要显示的字段
  • 原文地址:https://www.cnblogs.com/nicebaby/p/6273055.html
Copyright © 2020-2023  润新知