Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jdbc Jdbc-mysql Connecting to a MySQL Database in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Connecting to a MySQL Database in Java

Advertisement
In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end.

Connecting to a MySQL Database in Java

     

In java we have been provided with some classes and APIs with which we can make use of the database as we like. Database plays as very important role in the programming because we have to store the values somewhere in the back- end. So, we should know how we can manipulate the data in the database with the help of java,  instead of going to database for a manipulation. We have many database provided like Oracle, MySQL etc. We are using MySQL for developing this application. 

In this section, you will learn how to connect the MySQL database with the Java file. Firstly, we need to establish a connection between MySQL and Java files with the help of MySQL driver .  Now we will make our  account in MySQL database so that we can get connected to the database. After establishing a connection  we can access or retrieve data form MySQL database. We are going to make a program on connecting to a MySQL database, after going through this program you will be able to establish a connection on your own PC.

Description of program:

This program establishes the connection between MySQL database and java files with the help of various types of APIs interfaces and methods. If connection is established then it shows "Connected to the database" otherwise it will displays a message "Disconnected from database".

Advertisement

Description of code:

Connection:
This is an interface in  java.sql package that specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface.

Class.forName(String driver):
This method is static. It attempts to load the class and returns class instance and takes string type value (driver) after that matches class with given string.

DriverManager:
It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class.

getConnection(String url, String userName, String password):
This method establishes a connection to specified database url. It takes three string types of arguments like: 

    url: - Database url where stored or created your database
  userName: - User name of MySQL
  password: -Password of MySQL 

con.close():
This method is used for disconnecting the connection. It frees all the resources occupied by the database.

printStackTrace():
The method is used to show error messages. If the connection is not established then exception is thrown and print the message.

Here is the code of program:

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "jdbctutorial";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  conn.close();
  System.out.println("Disconnected from database");
  catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Discuss: Connecting to a MySQL Database in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:46
sowmiya.c
August 18, 2012
i want to jdbc connection program in java

pls reply my email
ace
September 7, 2012
java

which is more easy to use java or php
bhaumik
September 26, 2012
thanks

thank you it is really helpful content
govind
September 30, 2012
sql

kya hum without sql use kiye bina database use nhi kar sakte
prabu
October 25, 2012
MySQL Connection

I downloaded and installed ODBC driver for MySQL in my Windows7 OS.But while adding DSN, the ODBC driver for mySQL is not displaying....Please help....
govind
November 27, 2012
database connectin

it s very useful.
Anand
November 30, 2012
good job..

this site more usefull for beginners..
Balaji
April 1, 2013
Connecting with MySQL

While executing the above program am getting the following error. In my program i removed the UserName and Password and made it as blank. Anybody can suggest Exception in thread "main" java.lang.NoClassDefFoundError: MysqlConnect Caused by: java.lang.ClassNotFoundException: MysqlConnect at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276) at java.lang.ClassLoader.loadClass(ClassLoader.java:251) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Er.Raj Roushan
June 12, 2013
connection

What are the procedure to make a connection between java and mysql in control panel.
DMCA.com