【问题标题】:How to make a connection localhost and my database如何连接本地主机和我的数据库
【发布时间】:2018-01-13 14:55:20
【问题描述】:

我正在使用 NetBeans 制作一个 Web 应用程序,并将 pgadmin4 用于我的数据库。问题是当我创建连接池时。 这是我的数据库http://prntscr.com/hzwn9h,我认为问题在于我想要类似http://prntscr.com/hzwnj2 的东西,但我有这个http://prntscr.com/hzwnrw。我已经尝试了很多解决方案,但它不起作用,我不知道我还需要做什么。我尝试过的其中一件事是https://rivaso.wordpress.com/2012/02/19/how-to-setup-a-new-database-in-postgresql-and-glassfish/,但不幸的是没有成功

【问题讨论】:

    标签: database postgresql netbeans web-applications


    【解决方案1】:

    以下代码 sn-p 显示如何使用 jdbc 驱动程序为 PostgreSQL 建立连接。确保将jdbc 驱动程序添加到库中。

    public Connection DBConnect() {
        try {
            String host = "localhost";//host
            String port = "5432";//db port
            String db = "exp";//database name
            String user = "root";//database username
            String pass = "1234";//password
    
            //connection url
            String url = "jdbc:postgresql://" + host + ":" + port + "/" + db;
    
            //initialize jdbc driver for postger sql
            Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection(url, user, pass);
    
            //return connection
            return conn;
    
        } catch (Exception e) {
            System.out.println("Error : " + e.getMessage());
            return null;
        }
    
    }
    

    参考:jdbc.postgresql.org

    【讨论】:

      【解决方案2】:
      public static void main (String[] args) { 
      
          try {
              Class.forName("com.mysql.jdbc.Driver");
              String url = "jdbc:mysql://localhost:3306/yourdatabasename"; 
              Connection conn = DriverManager.getConnection(url, user,password);
      
              conn.close(); 
          } catch (Exception e) { 
              System.err.println("Got an exception! "); 
              System.err.println(e.getMessage()); 
          } 
      
      }
      

      【讨论】:

      • 这是mysql连接
      猜你喜欢
      • 2019-03-25
      • 2013-11-29
      • 2018-09-16
      • 2019-07-14
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多