0% found this document useful (0 votes)
348 views

Java Database Connectivity With MySQL - Javatpoint

This document provides an example of connecting a Java application to a MySQL database. It outlines the 5 steps needed: 1) specifying the MySQL driver class, 2) defining the connection URL with database name and port, 3) setting the username and password, 4) loading the MySQL connector JAR file, and 5) writing Java code to connect and execute a sample query. The example creates a database and table, loads the driver, makes a connection, executes a SELECT statement, and prints the results.

Uploaded by

SRubika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
348 views

Java Database Connectivity With MySQL - Javatpoint

This document provides an example of connecting a Java application to a MySQL database. It outlines the 5 steps needed: 1) specifying the MySQL driver class, 2) defining the connection URL with database name and port, 3) setting the username and password, 4) loading the MySQL connector JAR file, and 5) writing Java code to connect and execute a sample query. The example creates a database and table, loads the driver, makes a connection, executes a SELECT statement, and prints the results.

Uploaded by

SRubika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

3/28/2017 JavaDatabaseConnectivitywithMySQLjavatpoint

Mumbai to 800 off on Domestic


Delhi? Flights

Example to connect to the mysql


databaseinjava
Forconnectingjavaapplicationwiththemysqldatabase,youneedtofollow5
Grab the
stepstoperformdatabaseconnectivity.
Top BI
In this example we are using MySql as the database. So we need to know
followinginformationsforthemysqldatabase:
Tools

1. Driver class: The driver class for the mysql database is


com.mysql.jdbc.Driver.

2. Connection URL: The connection URL for the mysql database is


jdbc:mysql://localhost:3306/sonoowherejdbcistheAPI,mysqlis
Converting Data
thedatabase,localhostistheservernameonwhichmysqlisrunning,
into Business
wemayalsouseIPaddress,3306istheportnumberandsonooisthe
Insights Needs
databasename.Wemayuseanydatabase,insuchcase,youneedto
the Best Training
replacethesonoowithyourdatabasename. & Certication
Get it!
3. Username:Thedefaultusernameforthemysqldatabaseisroot.

4. Password:Passwordisgivenby the user at the time of installing the


mysql database. In this example, we are going to use root as the
password.

Let'sfirstcreateatableinthemysqldatabase,butbeforecreatingtable,we
needtocreatedatabasefirst.


1. createdatabasesonoo
2. usesonoo

3. createtableemp(idint(10),namevarchar(40),ageint(3))

http://www.javatpoint.com/exampletoconnecttothemysqldatabase 1/4
3/28/2017 JavaDatabaseConnectivitywithMySQLjavatpoint

Example to Connect Java Application with mysql


database
In this example, sonoo is the database name, root is the username and
password.

1. importjava.sql.*

2. classMysqlCon{

3. publicstaticvoidmain(Stringargs[]){

4. try{

5. Class.forName("com.mysql.jdbc.Driver")

6. Connectioncon=DriverManager.getConnection(

7. "jdbc:mysql://localhost:3306/sonoo","root","root")

8. //heresonooisdatabasename,rootisusernameandpassword

9. Statementstmt=con.createStatement()

10. ResultSetrs=stmt.executeQuery("select*fromemp")

11. while(rs.next())

12. System.out.println(rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3))

13. con.close()

14. }catch(Exceptione){System.out.println(e)}

15. }

16. }

downloadthisexample

Theaboveexamplewillfetchalltherecordsofemptable.

Toconnectjavaapplicationwiththemysqldatabasemysqlconnector.jarfileis
requiredtobeloaded.

downloadthejarfilemysqlconnector.jar
http://www.javatpoint.com/exampletoconnecttothemysqldatabase 2/4
3/28/2017 JavaDatabaseConnectivitywithMySQLjavatpoint

Twowaystoloadthejarfile:

1.pastethemysqlconnector.jarfileinjre/lib/extfolder

2.setclasspath

1)pastethemysqlconnector.jarfileinJRE/lib/extfolder:

Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the


jarfilehere.

2)setclasspath:

Therearetwowaystosettheclasspath:

temporary

permanent

Howtosetthetemporaryclasspath

opencommandpromptandwrite:

1. C:>setclasspath=c:\folder\mysqlconnectorjava5.0.8bin.jar.

Howtosetthepermanentclasspath

Go to environment variable then click on new tab. In variable name write


classpathandinvariablevaluepastethepathtothemysqlconnector.jarfile
by appending mysqlconnector.jar. as C:\folder\mysqlconnectorjava5.0.8
bin.jar.

prev next

Latest4TutorialsonJavaTpoint

Cassandra JSF

http://www.javatpoint.com/exampletoconnecttothemysqldatabase 3/4
3/28/2017 JavaDatabaseConnectivitywithMySQLjavatpoint

Ruby JavaDate

Share 21

http://www.javatpoint.com/exampletoconnecttothemysqldatabase 4/4

You might also like