Answer the question
In order to leave comments, you need to log in
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
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
"); Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question