JDBC
- Is use to connect java application with the database.
- JDBC has multiple APIs (building functionalities) to interact with Database.
- To perform Database connection from java application, you required some of the components, which are as follows
- Database installed on local system
- Required the Driver (.jar)
- Java Application
What is Database and why to use Database?
- Database is the application which is provided by multiple vendors.
- Databases has to install on machine before use.
- To use database, you should have understanding of SQL language.
- Database are used to store different kind of data.
- This will store data in structure format (table format) that is in row and column.
- There are two types of database
- Relation Database – Can store data in table or row and column format and also one table linked with another table.
- Non-relation Database – Data will not store in table format.
- Database are used to store high volume of data which can be further use to generate report or for analysis purpose.
- Also, Database will provide easy way to manage large volume of data.
Database vendors
- Mysql
- Oracle
- MySql Server
- H2 Database (Use as a embedded DB)
- IBM database
- Cybage Database
- Derby Database
- Postgres Database
To Download mysql Jar : https://mvnrepository.com/artifact/mysql/mysql-connector-java
Steps To Add Jar file into core java application
- Right Click on Project
- Build Path -> Configure Build path
- On Window -> got to Libraries tab
- Click on Classpath option and click on Add External Jars button
- Browse the jar file add select and click on Open
- Click on Apply and Close Button
URL:
Protocol + HostName(DNS) + PortNumber + Resources
MySql: jdbc:mysql://localhost:3306/jdbc
Oracle: jdbc:oracle:thing@localhost:1521:xe
Steps to connect Database
Register Driver
- This step is use to load the driver in to memory.
- In Register Driver you have to provide driver class name.
Mysql5: com.mysql.jdbc.Driver
Mysql8: com.mysql.cj.jdbc.Driver
Oracle: oracle.jdbc.driver.OracleDriver
- Example:
Class.forName(“com.mysql.cj.jdbc.Driver”);
Create Connection
- This step is use to connect with the database
- To execute this step you should know URL, Username and Password of the database.
- You have to use Connection and DriverManager JDBC APIs which id build-in java classes and interface.
- Example:
Connection con = DriverManager.getConnection(url,username,password);
Create Statement
- This step is use to hold the sql statement/query.
- There are 3 ways to create Statement.
- Statement (I)
- Using this you can execute the query(DML,DDL).
- Statement may cause SQL-injection issues.
- Cannot write a parameterized query.
- Performance wise it is slower than other 2 options.
- Syntax:
- Statement (I)
Statement stmt = conObj.createStatement();
- PreparedStatement (I)
- Using Prepared Statement can execute DDL, DML queries.
- This option is more secure than the Statement, because there are no chances of SQL injection.
- You can write parameterized query like below.
insert into student values(?,?,?,?)
- Performance wise it is faster than Statement
- Syntax:
PreparedStatement stmt = conObj.prepareStatement(“Query”);
- CallableStatement (I)
- Using this type of statement, you can execute a procedure from PL/sql.
- You can call the procedure using this type of statement
- Syntax:
CallableStatement stmt = conObj.preareCall(“{call procedurename(parameter)}”);
Execute Statement
- To execute the sql statement/query and get result back to java application.
- There are 3 options to execute query
- executeUpdate()
- Is use to execute all DDL and DML type of queries except Select.
- This method returns the int values, which is the number of row affected.
- executeQuery()
- Is use to execute DQL or Select type of query.
- This method return the Object of ResultSet, from which you can get the data selected by your select query.
- executeUpdate()
3. execute()
- Is use to execute all type of query.
- This method return the Boolean value, if it returns true the you will get an int value else it will return a ResultSet
Close Connection
- Close all the database resources.
Example :-
import java.sql.Connection; import java.sql.DriverManager; public class DBConnectionTest { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/jdbc"; String username ="root"; String password ="7028"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection(url, username, password); System.out.println("Connection Success....."); con.close(); }catch(Exception e) { e.printStackTrace(); } } }
Join Telegram : Click Here
All Full Stack Java Study Material
Job’s For Fresher