-->

what is JDBC & How it work with MySql DB

every developer should be having a knowledge with database even if this knowledge limited to a basic stuff in DB or SQL to be able to create a simple project can store some data and manipulate it and then "vaualla" the program has an awesome result to end user.

Summary: we are here to be engagement with JDBC API and brush up some of basic stuff can we do it with MySQL DBM (Database Management System).
Also we going to know how to setup own lab and take code example.

let's do this sh*t Right NOW.


What is JDBC

it is java technology to give you ability to access any Relational Database and work with it.
so that is big picture about JDBC ,but now we are digging into details and some official freaky stuff.

JDBC helps you to write Java applications that manage these three programming activities
  
  • Connect to Database
  • Send Queries and update statements to the database
  • Retrieve and process the results received from database
now we have got a s process steps, but some thing here is missing which is it components we are using for do this steps.



Components of JDBC:

  1. JDBC Drive
  2. Connection
  3. Statement (and Query)
  4. ResultSet (Object)  
JDBC process Flow Diagram - New System Technology
JDBC Process Flow Diagram | new system technology

 what is JDBC Driver :


is a set of java classes that implement JDBC interfaces for interacting with a specific database.
additionally, it provide for you a bunch of different Databases such as (MySQL , Oracle , Sql Server , SQLite and much more) and it is written in pure java.

Driver With MySQL:


it translate JDBC calls into MySQL specific calls and sends the calls directly to a specific database.

How To use JDBC Driver with MySQL:

We need a something called Connector. Connector is a jar file contains of classes to make easy connection with MySQL.
You can Download it from this link Connector/J



What is a Connection:


Connection is a Class we want to use it for establish connection to database.
after you create Connection object and setup username and password now you able to execute query and use database as prefect.
  

Statement:

To execute a SQL queries e.g., SELECT, INSERT, UPDATE, DELETE, etc.
there are a several type of statement JDBC offer to your usage but for now we only use this type


ResultSet:

after you execute statement MySQL send back the result so we need object to store whatever data has backed

that is so sweet but i have realized you just tell me what happen in the back ground ,but i need to know ho to work with coding and setup my lab.

umm ... YaH You are right ,but you are not patient because we are going now to your goals.

setup LAB:


First: should you have a Eclipse or NetBeans 

Second: you need to install a XAMPP

finally: Connector MySQL (Connector/J)

Steps on Eclipse:



  1. open new java project
  2. create Main Class
  3. Extract Connector file (if has .zip or rar extention)
  4. select your project and hit Right Click then select Properties
  5. select (java Build Path => libaraies => add external Jar)
  6. press Apply and then Apply and close
after you install xampp you will get MySQL on your Computer with default user and password
user is root and password is : Empty (Note: password it doesn't set).

now we going to write some code.

YouTube Video :

Coding ....



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main{ 
    public static void main(String []args){
        Connection connect = null;
        // db parameters
        String url       = "jdbc:mysql://localhost:3306/test";
        String user      = "root";
        String password  = "";
        try {    
            // create a connection to the database
            connect = DriverManager.getConnection(url, user, password);
            // create statement
            Statement stat = connect.createStatement();>
            ResultSet rs   = stat.executeQuery("SELECT * from test WHERE 1");
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        } 
 } 
} 


Summary:

now we are answer about couple of question and we can summarize it in points.

  1. what is JDBC:
    Java API that can access any kind of tabular data, especially data stored in a Relational Database.
  2. what is important steps to create application connect with DB:
    Connect to Database, Send Queries and update statements to the database and Retrieve and process the results received from database.
  3. what is component of JDBC : JDBC Drive, Connection Statement and ResultSet.
  4. we know how to import jar files and take code example for a simple program