Hello Friends,
One of the significant characteristic of any programming language is its connectivity with the database and its operations. Java supports many options to connect to the database and one of the most used database is MySql. JDBC (Java Data Base Connectivity) is used for connecting the java code with the database and it provides cross-DBMS connectivity to a wide range of SQL databases and access to other tabular data sources, such as spreadsheets or flat files. This tutorial shows the jdbc and mySql connection establishment and its use.
You may know, both JDBC and MySQL are freely available for many purposes, the combination of JDBC and MySQL is a powerful combination that should be of interest for a wide variety of applications. This tutorial is about connecting the MySql database with the java code through JDBC.
For this tutorial, I have used the database named code2java and the table that we are using is users.
As you can see, the table contains 2 records with 5 different information. I have used this table for displaying its contents via the java code.
In the following code, “Class.forName(driver).newInstance()” creates the new instance of the JDBC driver and DriverManager.getConnection() function connects to the database. It takes three variable which are required for setting up the connection those are – url to the database, username and password.
A Statement is an interface that represents a SQL statement. You can execute the Statement objects, and they generates the result, these results are called as the ResultSet objects which represents the table from the database. To get the result from the Statement you need the connection object.
The remaining code is self explanatory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.JavaExamples; import java.sql.*; public class JDBCConnectionExample { public static void main(String[] args) { System.out.println("MySQL & JDBC Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "code2java"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; String dbtime = null; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Database Connected"); Statement stmt = conn.createStatement(); String query="Select * from users"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { int srNo = rs.getInt("sr_no"); String fName = rs.getString("firstName"); String lName = rs.getString("lastName"); String uID = rs.getString("uname"); String Pwd = rs.getString("password"); System.out.println("| Sr No: "+srNo+ " | FirstName: "+fName+" | LastName: "+lName+" | UserName: "+uID+" | Password: "+Pwd+" |"); } } catch(Exception e) { e.printStackTrace(); } finally { //This is used for Disconnecting.. try { conn.close(); System.out.println("Database Disconnected"); } catch (SQLException e) { e.printStackTrace(); System.out.println("Error while Disconnecting from database"); } } } } |
Run the program and see the output.
Hope this will help you.
Regards,
admin@code2java.com