0

I'm trying to connect my sql database to my java project but for some reason the postgresql.Driver (org.postgresql.Driver) doesn't seem to work.

I've made sure to import all sql methods and such (java.sql.*)

the method i'm trying to run looks like this:

public boolean createUserInDB(String username, String password, boolean b, java.util.Date createdTime, java.util.Date lastLoginTime) {
    try {
        Class.forName("org.postgresql.Driver");
        Connection conn = DriverManager.getConnection(url, username, password);
        Statement st = conn.createStatement();

        String sql = "INSERT INTO login(brugernavn, kodeord, status, created_time, last_login_time) "
                + "VALUES(" + username + ", " + password + ", " + true + ", "
                + createdTime + ", " + lastLoginTime + ");";

        st.executeUpdate(sql);

        System.out.println("Query successfull");


    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    return true;

}

Don't mind the first line of the query (the attributes are in danish).

When running this method the only thing it does is printing out "org.postgresql.Driver" and then returns true hence telling the user (in GUI) that the query has been made.

I've downloaded the newest driver for the postgresql version 42.2.2 but still no luck.

What am I missing?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
CasaRol
  • 39
  • 1
  • 13
  • Concateneting such query in every JDBC driver is wrong. Look query in debugger – Jacek Cz May 12 '18 at 06:59
  • Can you be a little bit more specific? i'm still quite new in this. – CasaRol May 12 '18 at 07:11
  • Nothing here prints "org.postgresql.Driver". Use debbuger, view variables etc. Set breakpoint on exception. I'm almost sure query gives exception – Jacek Cz May 12 '18 at 10:55
  • 1
    It looks to me as if the PostgreSQL JDBC driver JAR isn't on the classpath. The exception you are getting is quite probably a ClassNotFoundException, which typically contain only the name of the missing class in the exception message. (Try writing `e.printStackTrace()` in the `catch` block to confirm this.) Stack Overflow contains plenty of questions to help you add the JAR, for example https://stackoverflow.com/questions/4879903/. – Luke Woodward May 12 '18 at 11:56
  • @LukeWoodward I've now made a few chages and has connection to the db, but it says that SSL if off. org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host " *MyIpAdress* ", user "g", database "ksevsemh", SSL off How do I fix this? – CasaRol May 12 '18 at 14:12
  • @AlexanderMicheelsenRol: sorry, I'm afraid I have no idea. I am not a Postgres expert, just someone who could diagnose your previous problem. I can't offer any more help than to suggest Googling your new error message. – Luke Woodward May 12 '18 at 15:52
  • @LukeWoodward Ok, thanks for your help anyway ;) – CasaRol May 13 '18 at 09:50

2 Answers2

1

It seems your query formation is wrong as you are trying to insert java.util.date to SQL date. Try PrepareStatement instead of CreateStatement like below and it will take care of date for your Postgres database.Hope it will work.

PreparedStatement st = conn.prepareStatement("insert into login (brugernavn, kodeord, status, created_time, last_login_time) values (?,?, ?, ?, ?)");
st.setString(1, username);
st.setString(2, password);
st.setBoolean(3, true);
st.setObject(4, createdTime);
st.setObject(5, lastLoginTime);
st.executeUpdate();
st.close();
utpal416
  • 899
  • 5
  • 15
  • It still only prints out "org.postgresql.Driver". The wierd part is that i've made another project which were able to print out the information on the database and show in my java-consol and it still works with the 42.2.1 Driver. Now when i'm trying to insert things into my database it won't allow it for some wierd reason... – CasaRol May 12 '18 at 13:53
  • Can you check the userid that you are using has the write permission to the database ? Can you please use "e.printstacktrace()" instead System.out.println(e.getMessage()); to print the complete Error.. – utpal416 May 12 '18 at 14:27
  • 1
    Found the problem. i've defined the same name for the username to the db and for the query therefor I was logging in to the db with the user I wanted to create and not the user that was already existing. Thanks for your help :) – CasaRol May 13 '18 at 09:52
  • Great to know that.If you wish you can accept it as answer thread.Thanks!! – utpal416 May 13 '18 at 13:21
1

First problem:

} catch (Exception e) {
    System.out.println(e.getMessage());
}

This is not the right way of listing exceptions as Luke Woodward suggested. You need to use:

} catch (Exception e) {
    e.printStackTrace();
}

This will list the exceptions and not just print out a String for the error.

Second problem:

I'd named the parameters for the login information for the database the same as the user I was trying to create in the method. This caused Java to use the "userName" and "password" (createUserInDB-method parameters) instead of user the database login information.

Thanks for the help to those who came with suggestions along the way :)

CasaRol
  • 39
  • 1
  • 13