1

I am browsing this website for quite some time now. The community helped me quite a lot, even though I never registered. Thanks for that.

However, this time I can't find a solution to my problem just by browsing, so I decided to register here and state my question so that someone might be able to help me.

First of all I'd like to say, that I'm basically at the beginning of my studies, so I'm not very knowledgeable yet. At the moment I'm learning for a upcoming exam and I need to know about databases. The Database of choice is Postgresq. But to really understand something like that, you need to try it out and not just read about it.

I'm not exactly sure what information are exactly required. But I'm using Windows 7, pgadmin and eclipse for that, all of them should be up to date. If you need further information, please ask for it.

I used a Tutorial to get it up and running, however I'm not able to establish a connection to the database using eclipse. The DriverManager.getConnection() causes a UTF-8 error I'm not understanding nor able to fix it.

org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:280)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:67)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:212)
at org.postgresql.Driver.makeConnection(Driver.java:407)
at org.postgresql.Driver.connect(Driver.java:275)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.hywy.TestConnection.main(TestConnection.java:23)
Caused by: java.io.IOException: Illegal UTF-8 sequence: initial byte is 11111xxx: 252
at org.postgresql.core.UTF8Encoding.decode(UTF8Encoding.java:131)
at org.postgresql.core.PGStream.ReceiveString(PGStream.java:331)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:447)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:223)
... 7 more

The Code I'm using is:

import java.sql.*;
import java.util.Properties;
//import com.hywy.db.DbContract;
public class TestConnection {
public static void main(String[] args) {
    Properties properties = new Properties();
    properties.setProperty("user", "postgre");
    properties.setProperty("password", "random_password");
    //properties.setProperty("ssl", "true");

    String url = "jdbc:postgresql://localhost:1112/people";
    try {
        Class.forName("org.postgresql.Driver");
    //  Connection c = DriverManager.getConnection(
    //          DbContract.HOST+DbContract.DB_NAME,
    //          DbContract.USERNAME,
    //          DbContract.PASSWORD);

        Connection con = DriverManager.getConnection(url, properties);
        //Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:1112/people" , "postgre" , "random_password");

        System.out.println("DB connected");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}
}

The code is a bit of a mess, since I Googled some time for it and tried some stuff. I was not sure if I should paste the out-commented code with the rest of it, but at least you can see that I already tried some things. I also tried a different port, at first I was using the standard port (5432) but it made no difference.

I also linked the up-to-date jdbc jar file in my build path.

I hope someone knows (about) this problem and is willing to help me. Thanks in advance.

Excuse my English, it's not my native tongue.

6
  • What's the jdbc driver version your are using ? Commented Jul 20, 2016 at 14:12
  • hi, "postgresql-9.4.1209.jar" at the moment. I also tried the "postgresql-9.4.1208.jar" since the 1209 is very new, but it's no different Commented Jul 20, 2016 at 14:17
  • Refer stackoverflow.com/questions/34729845/… Commented Jul 20, 2016 at 14:24
  • You are right. I saw this thread actually when I was looking for a solution. But I'm not quite sure what I am supposed to do, since none of the 2 answers were helpful to me. Should I just state my question with all the code and informations as an answer to this topic and hope someone who hasn't seen the question the first time might see it now, since it's updated? That's a honest question, since this is my first post. If that's how it should be done I can do that and my topic can be deleted. Commented Jul 20, 2016 at 14:38
  • I tested in my local machine. I had a problem with the jdbc version postgresql-9.4.1209.jar and my JDK 1.7 JVM. I tested the version9.4-1206-jdbc42 and every thing is okay. What JDK version you are using ? Commented Jul 20, 2016 at 15:22

1 Answer 1

2

Ok guys, I just wanted to say that I fixed the problem. I also wanted to thanks anyone who tried to help me.

It's only logical that no one was really able to help me, because it was only due to my stupidity. The mistake happened at

"jdbc:postgresql://localhost:1112/people"

Only when I did the whole thing on another computer, to see if it's my system, I realized I'm not supposed to write the actual table name at the end of the line, but instead the name of the Database, so I just changed "people" into "postgres" and it worked.

Sorry for the stupid question, I hope my next one will be better :)

*Topic can be deleted or closed or whatever you want to do with it.

Thanks anyone!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.