R
R
random2015-01-01 19:55:48
PostgreSQL
random, 2015-01-01 19:55:48

Can't connect to Postgresql?

Can't connect to Postgresql.
Throws one single error: org.postgresql.util.PSQLException: Connection refused. Check that the host and port are correct and that the postmaster is accepting TCP/IP connections.
So I'm trying to connect. I downloaded PostgreSQL from jdbc.postgresql.org/download.html, uploaded the postgresql-9.3-1102.jdbc41.jar version to the project library.

import java.sql.*;

public class JDBCExample {
   public static void main( String args[] )
     {
       Connection c = null;
       Statement stmt = null;
       try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb","alisher", "123456");
         System.out.println("Opened database successfully");

         stmt = c.createStatement();
         String sql = "CREATE TABLE PHONEBOOK " +
                      "(ID INT PRIMARY KEY   NOT NULL," +
                      " first_name           VARCHAR(20)  NOT NULL, " +
                      " last_name            VARCHAR(20)  NOT NULL, " +
                      " PHONE                VARCHAR(20), " +
                      " EMAIL                VARCHAR(50))";
         stmt.executeUpdate(sql);
         stmt.close();
         c.close();
       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }
       System.out.println("Table created successfully");
     }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Abramov, 2015-01-02
@kivsiak

Shellom through psql clings?

R
random, 2015-01-02
@demon123

This is my first time connecting to a database.
I installed PostgreSQL 9.4 and pgAdmin III, created a database in pgAdmin. And connected to it, everything went well.

Class.forName("org.postgresql.Driver");
Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres","postgres", "123456
");
As I understood, I need to connect to an existing database?
And if I disconnect from the server and close pgAdmin, can I connect to the database?
PS: What can I read to work in psql (on the command line)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question