Java Database Connection (JDBC)
Introduction to JDBC
- JDBC stands for java Database connectivity in java.
- It is set of java ApIs (application programming interface) used to carry out SQL queries or statements.
- A set of classes and interfaces make up this API, which enables developers to create pure Java Database applications.
- In computer science, a database connection is a feature that enables software to communicate with database server software. whether or not on the same computer.
- To transmit a command and receive an answer, you need a connection.
Types Of JDBC drivers
There are 4 types of JDBC drivers :
JDBC type 1 driver- They are JDBC-ODBC bridge drivers, which convert JDBC to ODBC and use the built-in driver for Windows ODBC. The slowest of all are they.
JDBC type 2 driver - They only seldom employ native API for data access, converting JDBC to native SQL calls from data base suppliers and offering Java wrapper classes. Installing binaries on each client is necessary in order for a JDBC driver, like type 1 driver, to be used..
JDBC type 3 driver-To an independent network protocol, use JDBC. They are entirely developed in Java and connect to a remote listener using a network protocol that is independent of the vendor.
JDBC type 4 driver - They are the most effective driver kinds and are almost entirely built in Java. The application receives the compilation. Except for the Java virtual machine, no other aplet or servlet needs to be installed on the client computer (JVM). Additionally, it directly translates JSBC to the RDBMS's native API.
Connections can be open with these steps
1.import packages
2.Register the JDBC drivers
3.Open a connection to the database .
4.Create a Statement object
5.Executing a query and returning a Result set object for further process.
6.Processing the result sets and statements Object.
7.Close the result set and statement object.
8.Finally close the connection.
Step 1
To establish a connection to a JDBC database, import the following.
java.sql.
java.math.
java.io.
oracle.jdbc.driver.
Step 2
Registering the JDBC Drivers
JDBC Driver registration requires the following 4 parameters.
- Database URL
- JDBC Driver's name
- Username of database
- password of database
There are several ways to register JDBC drivers.
- Class drvClass- Class.forName(m_drivername);
- Drivermanager.registerDriver(Driver)drvClass.newInstance());
Step 3: Opening a Database Connection
Using Connection, a connection to the underlying database can be established. m_con=DriverManager.getConnection(m_url,m_username,m_password);
Step 4: Creating a statement Object
SQL Statements
once a connection has been made. It is used to transmit SQL commands to the database that it is based on. For transmitting SQL queries or statements to the database, JDBC offers three classes: PreparedStatement extends Statement, and CallableStatement extends from PreparedStatement.
- Statement : For example SQl statements
- PreparedStatement : for straightforward SQL queries that use one or more IN arguments, or for straightforward SQL queries that are executed as procedures.
- CallableStatement : For executing SQL stored procedure .
Three distinct ways to execute SQL statements are provided via the statement interface.
- executeQuery: For statement that procedure a single result set
- executeUpdate: For carrying out SQL DDL, or data definition language, statements as well as INSERT, UPDATE, and DELETE statements.
- execute: for the execution of statements that produce multiple result sets. a combination of the two, multiple update counts, or both
The steps below include the use of a statement object.
Statement
Statement stmt= m_con.createStatement();
statement.stmt=m.con.createStatement(int resultSettype,int resultSetConcurrency);
PreparedStatement
preparedsatement = m_con.prepareStatement(String sql)
prepareStatement=m_con.prepareStatement(String sql ,int resultSetType,int resultSetConcurrency)
CallableStatement
CallableStatemet = m_conn.CallableStatement(String sql);
CallableStatement= m_conn.CallableStatement(string sql ,int resultSetType,int resultSetConcurrency),
Step 5 Query execution and return of a result set object AND
Step 6:processing The Result Set
Execute the command.
Statement :
ResultSe res = stmt.executeQuery(String sql);
int rowCount=stmt.executeUpdate(String sql);
boolean result= stmt.execute(String sql);
PreparedStaement
REsultSet res= pstmt.executeQuery()
int rowCount =pstmt.executeUpdate();
boolean result-cstmt.execute();
CallableStatmenet
ResultSet res= cstm.executeQuery();
int rowCount = cstmt.executeUpdate();
boolean result=cstmt.execute();
Processing the Result Set
- A resultset includes every row that met the criteria in a SQL query and makes the data in those rows accessible through getxyzmethods that let you access different columns of the current row.
- To go to the next row in the Result set, use the ResultSet.next() function. the subsequent row was changed to the present row.
- If the new current row is legitimate, the function ResultSet.next() returns true. if there are no more rows, false.
- Once all tasks have been completed, use the Close() function to close the result collection.
Step 7:Closing the statement and result set
Close the statement
The following code should be used to close the result set and statistics once all work has been completed..
Resultset:res.close();
Statement:stmt.close();
PreparedStatment: pastm.close();
CallableStatement : clstm.close();
Step 8: Closing the Connection
The following code should be used to close the code once all work has been completed.
m_conn.close();
Example
Write a JDBC program to create a student table in the database
import java.sql.*;
class EmployeeTable{
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","techknowledge4k");
Statement stmt= con.createStatement();
String CreateQuery = "craete table Studentinfi(name varchar2(20),rollno number(15),mobile number(12))";
stmt.executeQuery(CreateQuery);
System.Out.Println("Student table successfully created......");
con.close();
}
catch(Exception e)
{
System.Out.Println("e");
}
}
}
}
Thanks for visiting our site any query or question plz comment bellow or contact us .
Tags:
java