• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having trouble getting connection

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are missing the parens before the semi-colon.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 218
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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())
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic