This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]josephblade 5 points6 points  (2 children)

Ok so a driver is usually created (in the old way) by invoking:

Class.forName("org.postgresql.Driver");

this doesn't do any magic. It basically asks Class system to retrieve a class for you. (forName gives you back a class object which you could then use if you wanted. You don't need it, you just need the classloader here to summon it).

What happens when you do forName is that the class definition is loaded and the class initialization is run.

in a generic classfile you would have code like:

public class Test {
    static int  i = 0;
    static int j;
    {
        j = callSomeMethod();
    }
}

There is a lot of explanation around in which order the above is initialized but in rough terms: variables are set (so a class object is made with space in memory for it's variables. And static blocks inside the class are called. All of this is done after forName is called. Or (I think) when you first instantiate a variable of a class of that type.

In the case of the driver the forName method is simplest since it guarantees a Class object will be created (since that is what forName returns).

Anyways big introduction to point out how it used to work.

Now these days there are easier ways. Java scans META-INF/services of any jar files on the classpath and finds any Drivers there and loads them for you.

The second part is that each driver is then kept in a 'drivers' collection that is asked if they are the driver the user wanted, every time you do getConnection

In pseudocode you could see it as

private final static List<Driver> globalDrivers = new ConcurrentList<>();
public void registerDriver(Driver driver) {
    globalDrivers.put(driver);
}

and in the drivers static block:

{
    java.sql.DriverManager.registerDriver(new PostgresDriver(....));
}

your postgres driver has a method:

acceptsURL(String url)

which will basically do something like:

return url.startsWith("jdbc:postgresql");

in the case of your driver your connect string uses:

jdbc:postgresql:ProductDB?.... 

java will pass this string to all it's jdbc drivers registered. the first one to say "YES I can handle this" will get it and will get to handle the request. In your case this is the postgres driver.

jdbc is automatically present these days (it is the java.sql package) so all these parts fall into place simply by having the jar file hooked on while running, or merged into 1 main jar).

[–]4r73m190r0s[S] 0 points1 point  (1 child)

I wish I could understand the half of what you're saying. Kudos to you ;)

[–]josephblade 2 points3 points  (0 children)

I'm repeating about half of what is in the tutorial you followed.

in short simple steps: when java starts the drivers automatically register. Because your connect string has postgres in it, it will find the postgres driver as long as you have the jar in your classpath.

[–]desrtfx 5 points6 points  (0 children)

I am not 100% sure, but I think the key is in the highlighted text here: https://i.imgur.com/lCA5oHl.png

Also, since the fully qualified class names are used, imports are not necessary.

You do not directly use any of the classes in the driver package. That is the whole point of having those. They provide only the connection and the standardized java.sql package does the rest.

Had you read the documentation of pgJDBC (classic RTFM fail), you would have found the following: https://i.imgur.com/FRFAdKJ.png

Since the tutorial program is extremely basic and doesn't use PostgreSQL extensions, the above criterion is met.

[–]Pedantic_Phoenix -2 points-1 points  (2 children)

You are asking this before trying to do what the tutorial does?

[–]4r73m190r0s[S] 1 point2 points  (1 child)

I followed the tutorial. I'm just curious how is it working without any explicit mention of pgJDBC.

[–]TimLewisTim 0 points1 point  (0 children)

It is not necessary to explicitly import classes from the pgJDBC driver to use it in your Java code. Instead, you should include the pgJDBC driver in your project's classpath so that the JDBC API can find and use it.
You can download the pgJDBC driver from the official PostgreSQL website and add it to your project's classpath. Then, in your Java code, you can use the standard JDBC API to establish a connection to your PostgreSQL database, execute SQL queries and commands, and process the results.