Alright, it's time to work on our DB module.
So we're going to create that module then migrate the code for the DB,
the DB related code which was data source class into this new module and of course we want to
got through the steps to make sure that our application still works with we what would then have which
of course would be two modules and code in both of those modules.
So let's see a current status of what we're at or where we're at for this project.
So you can see at the moment, the projects only got the one module called common which we worked on previously.
Now this module reads or requires those other modules and you can see on screen here the ones that we
set up in the last video.
Now on the image you can also see the internal packages, a DB and common while UI is exported.
And if you recall, we had to export ui so that we could get our graphical user interface working.
And this is what we're trying to achieve, this is our goal.
So the main goal is to split the project into three modules.
We have talked about this before.
Currently, if any of the common module put in this video we're going to create the DB module as I mentioned.
And also that's going to contain the DB package.
And of course then this is not finished because we have to also move packages into the third module
as well which we doing in another video.
Let's get back to IntelliJ,
So, first things we need to do is create the new module.
Let's go ahead and do that, File> New> Module.
And we're going to select java module SDK
There's none of Java, next,
And the module name is going to be academy.learnprogramming.db
you can see that's now been created.
And obviously that's going to be a same as that package name as well, we got a source folder there.
We've got our two module now showing up and what we do now is we'll go ahead this early in the process
and create a new model descriptor. Right click the source folder, set new
select module info.java
there's our module descriptor file created which is currently empty.
Now the next step is to move the DB package into the new module.
So what we're going to do to achieve that is come over to the DB module and we're going to right click the source folder
and click on create new package.
new package, I'm going to call this one academy.learnprogramming and then I'm going
to click on ok. Notice I didn't do the .db here.
And what we going to do next is cut and paste the DB package from the commons module into this DB module.
Now we can also move it as we have done previously by drag and drop.
So I just kind of drag this DB package up here
into the package that we already just created. You can drag that down into academy.learnprogramming and release and
click on ok and click on rRefactor. Then you can see now that the DB package has been removed from
our common module and it's now showing in our DB module and there's our data source class.
Let's open this data source class now, double-click, close and then we need to make more space here.
Let me just have it a little bit small, so I can have a bit more screen state.
Now there are compilation errors that we're gonna have to fix, you can see automatically there's is
one up there, java.sql.
In fact if I scroll up, there's a few more there as well. And since the DB module uses the classes album
and artist but also some classes from the sequel module,
you probably know by now that we need to add to requires statements into the module-info.java. So you can
either use the red bulbs or we can just type them manually.
So I can come over here to sql-- sometimes you need to click it twice and click on Add java.sql
directive to module-info.java and I'm going to click on that and I can just type the other one. Now notice
also with these names
If you've got two module descriptor files open, very nicely does show you the name of the module.
So the academy.learnprogramming.common and academy.learn programming.db in front of the module name.
So we got the requires in there, let's add the other one manually,
requires, that's going to be academy.learnprogramming.common, notice that intelliJ found
that for us, very nice. Now we've got an error showing here now at the moment when I've done that and
we wouldn't get an error if we went back and did it with the red bulb option.
And that's because the module common is not in the dependencies in IntelliJ and we have to add the
module as a dependency.
Now the easiest way to do that would be to do that in the code so we could get back to our data source
select the option here, add dependency.
I'm gonna go ahead and click on that. We go back and look at the module info and see that's
now fixed it there.
But since now only the DB module requires sqlite, what we're going to have to do is remove the
sqlite from common and add it to DB module.
But also we need to add the common module to the DB module dependencies.
Now the easiest way to do that is to go back to project structure.
Let's go ahead and do that. File> Project Structure
And looking at our modules here, common, we've got db, you can see that we got academy.learnprogramming in there
also in the common module here,
we've got some sqlite, there's two modules there, common and db, common currently has the dependency on jdbc
sqlite as you can see there.
And obviously db doesn't have that dependency, has the academy.learnprogramming common one because
I use the option just to add that. Now we need access to the classes artist and album that are in common module.
That's why I've added the common as a dependency to the db module, we need to add sqlite as well.
So go ahead and do that, so sqlite just come down and add it, JARs> libs> sqlite.
So that's both of them.
Then we'll just go back to common now and we'll remove that.
So I could have just selected and click the minus sign here, or just right clicked it like I did before and moved
and click remove, either way will work and then click on ok.
So far, our module was fine.
And we've now got the sqlite removed from the commons module and add it to a db module. What we need
to do now is have a dependency for sqlite jdbc.
Now that's an automatic java 9 module.
Now remember, automatic modules are modules that are created when the jars added to the module path.
We had to add the sqlite dependency to the db module which we just did. Now previously without commons
module, we didn't need to add the requires statement for the sqlite jdbc module and that's because we
only had the one module but now we have to add the requires statement to this module descriptor file
for our db module because we've got more than one. We're going to go ahead and do that.
And that, it's going to be requires sqlite.jdbc. So the name of an automatic module by the way is the name as a
jar file without the version, so on the libs folder-- Let me just go back to our file project structure and select db.
You can see that the name of the file was sqlite-jdbc-3.21.0.jar
And what we did was we removed the version,
we'll call it sqlite.jdbc, that's a general rule when you're reading a jar file and you
want to add an automatic object as a requirement to your module or for your module.
Now that we've done with that, let's go back to data source class and you notice that we still got a compilation error
to fix, if you just hover over one of these, you can see
what the problem here is it's telling us that academy.learnprogramming.common is declared
in that module and it doesn't export that currently to our new module, our academy.learnprogramming.db
So what that means is hopefully you can get the gist of this now is we have to export the common package
from the common module so that this module, our db module, can use those classes from the common module and
without exporting packages we can't get it,
we don't get access to classes from another module. So we need to open the module-info java file, the descript
file from the common module.
We're going to select the other module info here, the one that's more common we need to add an export
statement for that.
We're going to come up and add it. I'm going to add it above this UI line, exports academy.learn
programming.common.
So as you can see now, there's different ways to export.
So on line 9 we export it to a specific package using the to keyword.
So exports academy.learnprogramming.ui to and the name of the module but on line eight, we're just exporting
it without specifying the module.
Let's go back to data source.
And you can see those errors have now disappeared.
That's because we've got the correct exports statement in the common module descriptor file.
But another problem here is that the controller and the main class also use the data source class so that
the main class, the main java because they've got some errors here, scrolling up we can see that the issue
really is that the data source-- the import for the data source can't be resolved.
Now this is not as easy as you may think to solve.
That's because adding the module DB has a dependency, just a common module would result in a cyclic dependency.
In other words we'd have two modules where both depend on each other.
Now, cyclic dependencies are not a good practice.
And Java 9 doesn't actually allow these cyclic module dependencies.
Now problem is that our current project won't compile and we can't run it yet.
Now one way to solve this issue is to use transitive dependencies.
But we're going to be talking more about these transitive dependencies later, so we won't get into
too much detail about that now.
But the goal now to get around this is to actually move the UI package to the UI module and the process
of doing that should resolve some of these other problems.
So let's end this video here and in the next one
we'll do just that, we'll move the UI package currently in the commons module to its own new UI module.
And well actually, you will be doing that because the next video I'm going to set that has a challenge for you.
See you in the next video.