Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Having trouble getting connection  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
First topic, so I expect to be in the right place for questions.

So I'm making a simple java CRUD.
I made a first class called BaseDAO, that connects to the DataBase through JDBC.
(Any thoughts on making it better would help to)



Then I'm about to call it in my next class, this method that I created (getConnection).
The problem is, what I would usualy do is, create a object of that class and then call the method on it. Like this:

It isnt working.


This is what is working:


Can someone please explain to me, why I don't need to create a object BaseDAO?
And why in the second option I don't need to iniciate the Connection conn as "new Connection", and just as null?


 
Bartender
Posts: 5632
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Marshal
Posts: 63352
205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the tutorials about connecting to a database; this is one. I believe nobody uses Class.forName(...) any more.
 
Sheriff
Posts: 5745
148
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer to use a utility class to get a DB connection:
Some things to note:

* the connection object is reused so you're not recreating it over and over.
* it uses the Optional class so you don't return nulls (some may take exception to this)
* it does not deal with concurrecy

You would use it like this:
 
Ranch Hand
Posts: 135
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please go through the tutorials about connecting to a database; this is one. I believe nobody uses Class.forName(...) any more.


Even it might be true on "normal" jdbc4 drivers - it wouldnt return the driver he wants.
In standard mysql connector only com.mysql.jdbc.Driver gets autoloaded - but he wants another driver located in com.mysql.cj.jdbc.Driver wich seems to be another driver and therefor needs explicit loading.
The reason why in jdbc4 class.forname isnt used anymore is use of serviceloader - but if the driver doesnt implement serviceloader correctly it has to be loaded otherwise - maybe as oldschool as drivermanager.adddriver(new mydriver())
 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matt Wong wrote:Even it might be true on "normal" jdbc4 drivers - it wouldnt return the driver he wants.
In standard mysql connector only com.mysql.jdbc.Driver gets autoloaded - but he wants another driver located in com.mysql.cj.jdbc.Driver wich seems to be another driver and therefor needs explicit loading.


It all depends on the library being used.  For example, using mysql-connector-java-8.0.11.jar, com.mysql.cj.jdbc.Driver is automatically registered/loaded via the SPI.
Result:
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!