Connecting to MySQL (JDBC) using Java

Hereby we will discuss about Java Database Connectivity. As we have created a database named dbjavademo in MySQL. And a table named tblData with following fields also created.

JDBC Connection with MySQL

And also added some records like

Now Start the Netbeans and Create a Java Project (Java Application)

From Projects Panel/Sidebar Right Click on your project name.

And Select Properties you will get Screen Like

Netbeans Project Properties Window

Now Select Library and From Right Side Click on Add Library Button and from the list of Libraries Select MySQL JDBC Driver and Press Add Library in Dialog.

Now Driver for MySQL Has been loaded.

Now in your Java Program you have to import proper packages for various interfaces and Classes of Connectivity

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;

Below is the entire Java Program to Select the Rows from table tblData of database dbjavademo.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

 * @author jadeja
public class Jdbcdemo {

     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
        Connection con;
        PreparedStatement ps;
        ResultSet rs;
        String dbusername="root";
        String dbpassword="";
        String databasename="dbjavademo";
        String port="3306" ;// mostly common for mysql
        try {
            Class.forName("com.mysql.jdbc.Driver"); //Loads the Driver
            con=DriverManager.getConnection("jdbc:mysql://localhost:"+ port+"/"+ databasename,dbusername,dbpassword); //Connecting to Database
            String q="Select * from tblData"; //Query
            ps= con.prepareStatement(q) ;// PreparedStatement Created
            rs= ps.executeQuery(); //For Select Query executeQuery is used
            while(rs.next()) //While has rows in Query Result
                System.out.println(rs.getString("intsrno")+" "+ rs.getString("strWebsiteName")+" "+ rs.getInt("intRank")); //Which Column to Display
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Jdbcdemo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Jdbcdemo.class.getName()).log(Level.SEVERE, null, ex);

Output of the Program

Output of JDBC Select Query

Output of Program After Run in NetBeans

1 https://ocjp.in 1 Programming Guides
2 https://gujjupedia.com 10 General Purpose
3 http://csitpark.net 5 Domain and Hosting purposes
BUILD SUCCESSFUL (total time: 0 seconds)

This is the basic program to get data from MySQL using Java Code, Please Subscribe to get more examples related to Java Programming

Leave a Reply

Your email address will not be published. Required fields are marked *

× How can I help you?