Transcript
3/28/2017
Java Database Connecti vi ty w i th M ySQL - j avatpoi nt
Mumbai to Delhi?
Example to connect database in java
to
₹800
off on Domestic Flights
the
mysql
For connecting java application with the mysql database, you need to follow 5
Grab the
steps to perform database connectivity.
Top BI In this example we are using MySql as the database. So we need to know
Tools
following informations for the mysql database: 1. Driver
class:
The
driver
class
for
the
mysql
database database
is
com.mysql.jdbc.Driver. com.mysql.jdbc.Driver. 2. Connection URL: The connection URL for the the mysql database database is jdbc:mysql://localhost:3 jdbc:mysql://localhost:3306/sonoo 306/sonoo where jdbc jdbc is the API, API, mysql is Converting Data
the database, localhost is the server name on w hich mysql is i s running,
into Business
we may also use IP address, 3306 is the port nu mber and sonoo sonoo is the
Insights Needs
database name. We may use any database, in s uch case, you you needthe toBest Training replace the sonoo with your database name.
& Certiŀcation –
3. Username: The default username for the mysql database is root. oot.
Get it!
the time of installing installing the 4. Password: Password is given by the user at the mysql database. In this example, we are going going to use root ro ot as the password. Let's first create a table in the mysql database, but before be fore creating creating table, we need to create database first.
create create database sonoo; use sonoo; int(10),name int(3)); create cr eate table emp(id int(10),n ame varchar(40),age varchar(40),age int(3));
http://ww w.j avatpoi nt.com /exam pl e- to- connect- to- the- m ysql - database
1/4
3/28/2017
Java Database Connectivity with MySQL - javatpoint
Example to Connect Java Application with mysql database In this example, sonoo is the database name, root is the username and password.
import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/sonoo","root","root"); //here sonoo is database name, root is username and passwor d Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); }catch(Exception e){ System.out.println(e);} } }
download this example The above example will fetch all the records of emp table.
To connect java application with the mysql database mysqlconnector.jar file is required to be loaded.
download the jar file mysql-connector.jar http://www.javatpoint.com/example-to-connect-to-the-mysql-database
2/4
3/28/2017
Java Database Connectivity with MySQL - javatpoint
Two ways to load the jar file: 1. paste the mysqlconnector.jar file in jre/lib/ext folder 2. set classpath
1) paste the mysqlconnector.jar file in JRE/lib/ext folder: Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar fil e here.
2) set classpath: There are two ways to set the classpath: temporary permanent
How to set the temporary classpath open command prompt and write: C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
How to set the permanent classpath Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8bin.jar;.;
← prev
next →
Latest 4 Tutorials on JavaTpoint Cassandra
http://www.javatpoint.com/example-to-connect-to-the-mysql-database
JSF
3/4
3/28/2017
Java Database Connectivity with MySQL - javatpoint
Ruby
Share
Java Date
21
http://www.javatpoint.com/example-to-connect-to-the-mysql-database
4/4