Pages

Monday, December 8, 2014

How to Establish a Database Connection with MYSQL Database Using jdbc driver in Java


Lets see how to establish a database connection with mysql database using jdbc driver in java. First you need to download mysql-connector-java.jar. For more convenience create a separate package (in eclipse) inside Java Resources -> SRC by right clicking on SRC folder ->new->package .Named it with a suitable name, in my case I have named it as db.connection .Then add a java class in the same way you have used to add the package (DB_Connection)



If you are creating dynamic web project and, using JSP don’t forget to add mysql-connector-java.jar into your WebContent ->WEB-INF ->lib 

if you forget it you will definitely get the below error ........

"The type com.mysql.jdbc.ResultSet cannot be resolved. It is indirectly referenced from required .class files"

Then I have to ask ;)

Can you tell me where did you placed the mySQL jar file?It should be a part of your application [WEB-INF\lib].)........ 

If you are working with netbeans ensure that you have add your mysql-connector-java.jar into libraries by right clicking on library->add JAR folder.

Let see how to retrieve & Insert data to your database :)

INSERT DATA



RETRIEVE DATA





Happy coding….!