Data base plays very important role for making programmes interactive .Database in only the part that store user valuable information or any data for a nice programme it is necessary to have a database .Database can be used to fetch the information or store the data to it.For making connection to database we should have jdbc driver .
Quick review of JDBC driver.
JDBC stands for java data base connectivity, which is standard java API for the database conectivity between wide range of database and java programming language .JDBC library includes the API for every task that uses database connectivity .
JDBC provide an interface that allow to connect to the underlying database .
Java can be used for making
=> Java application
=>Applet application
=>JSP
=> Enterprise javaBeans(EJB)
For accessing database in all this version we need to have JDBC driver for making connection to data base.
Types of JDBC drivers
1. The type1 JDBC - ODBC bridge driver
When first java came then this time it user ODBC driver to connect to database because java only support this thye of driver .But as the time pass java improved a lot and type 1 became least used drver today.This is used for experimental purpose.
2. The type 2 JDBC -Native driver
This type of driver ,The JDBC API calls are converted into native c/c++ API calls which distinctto the database .This type of drivers are provovided by the vendors it self.
If we cahnge the database we have to alter the native API as it is exclusive to the database.
3. The type 3 JDBC-NET_PURE JAVA driver
Type-3 driver is three tier approach is used to accessing the data base.The JDBC client use standard network sockets to communicate with a Middleware Application Server.The socket application is translated by the middleware application server into the call formats needed by the DBMS and forwarded to database server.
This kind of driver is extremely flexible ,Since wants no code installed on the client and single driver can actually provide the the access to into multiple databases.
4.The type 4 100% pure java
This type -4 is a pure java based driver that informs directly with the vendor's data base through the Socket Connection and this is the highest performance driver available for the data base an is provided by the vendor's.
For providing connection to the database on should have Mysql installed to its system.
While installing MYSQL do not miss to provide password to it and after providing also remember it
we will need it while make the connection to database.
In this tutorial we will learn to make connection with database using Netbeans .First in this tutorials we will learn how to make a setup to install the JDBC driver lets start.
You need to follow these steps to make database connection .
First of all open your's netbeans and follow the following steps.
STEP 1.
When you open the net beans you get similar kind of window.
Clik on file positioned at top left corner of the window .Select new project now go to step 2.
STEP 2.
To new window appeared as in the image below...
Now click on java then the under the projects select java application. now go to step 3.
STEP 3.
After clicking to next ,the following window appear.
Now write the name of the project in project name and click on finish.
Before executing the code of databse interaction it is necessary to add mysql JDBC library to projects
Now here are steps to add mysql jdbc library to your project.
STEP 1.
Right click on the project and select properties from there as shown bellow.
now go to step 2
STEP 2.
Select add library from window appeared.
STEP 3.
From the appeared window select the MYSQL JDBC DRIVER from the list of driver appeared.
and click to finish.
Now you are ready to write and run the code for interaction with the database.
The most important operation performed on the data base are
1.Insert
2.Select
3.Update
now we have to write the following code But be sure you have the database with name sholey and password of mysql is root.(For successful run of the following code .This are used in this code.)
--------------------------------------------------------------------------------------------
import java.sql.*;
public class InsertDemo
{
public static void main(String...args)throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sholey","root","root");
PreparedStatement pst=con.prepareStatement("insert into starcast values(?,?)");
pst.setString(1,"Gabar");
pst.setString(2,"Basanti"]);
int status=pst.executeUpdate();
if(status>0)
{
System.out.println("data stored");
}
else
System.out.println("not");
con.close();
}
}
Quick review of JDBC driver.
JDBC stands for java data base connectivity, which is standard java API for the database conectivity between wide range of database and java programming language .JDBC library includes the API for every task that uses database connectivity .
JDBC provide an interface that allow to connect to the underlying database .
Java can be used for making
=> Java application
=>Applet application
=>JSP
=> Enterprise javaBeans(EJB)
For accessing database in all this version we need to have JDBC driver for making connection to data base.
Types of JDBC drivers
1. The type1 JDBC - ODBC bridge driver
When first java came then this time it user ODBC driver to connect to database because java only support this thye of driver .But as the time pass java improved a lot and type 1 became least used drver today.This is used for experimental purpose.
2. The type 2 JDBC -Native driver
This type of driver ,The JDBC API calls are converted into native c/c++ API calls which distinctto the database .This type of drivers are provovided by the vendors it self.
If we cahnge the database we have to alter the native API as it is exclusive to the database.
3. The type 3 JDBC-NET_PURE JAVA driver
Type-3 driver is three tier approach is used to accessing the data base.The JDBC client use standard network sockets to communicate with a Middleware Application Server.The socket application is translated by the middleware application server into the call formats needed by the DBMS and forwarded to database server.
This kind of driver is extremely flexible ,Since wants no code installed on the client and single driver can actually provide the the access to into multiple databases.
4.The type 4 100% pure java
This type -4 is a pure java based driver that informs directly with the vendor's data base through the Socket Connection and this is the highest performance driver available for the data base an is provided by the vendor's.
For providing connection to the database on should have Mysql installed to its system.
While installing MYSQL do not miss to provide password to it and after providing also remember it
we will need it while make the connection to database.
In this tutorial we will learn to make connection with database using Netbeans .First in this tutorials we will learn how to make a setup to install the JDBC driver lets start.
You need to follow these steps to make database connection .
First of all open your's netbeans and follow the following steps.
STEP 1.
When you open the net beans you get similar kind of window.
Clik on file positioned at top left corner of the window .Select new project now go to step 2.
STEP 2.
To new window appeared as in the image below...
Now click on java then the under the projects select java application. now go to step 3.
STEP 3.
After clicking to next ,the following window appear.
Now write the name of the project in project name and click on finish.
Before executing the code of databse interaction it is necessary to add mysql JDBC library to projects
Now here are steps to add mysql jdbc library to your project.
STEP 1.
Right click on the project and select properties from there as shown bellow.
now go to step 2
STEP 2.
Select add library from window appeared.
STEP 3.
From the appeared window select the MYSQL JDBC DRIVER from the list of driver appeared.
and click to finish.
Now you are ready to write and run the code for interaction with the database.
The most important operation performed on the data base are
1.Insert
2.Select
3.Update
now we have to write the following code But be sure you have the database with name sholey and password of mysql is root.(For successful run of the following code .This are used in this code.)
--------------------------------------------------------------------------------------------
import java.sql.*;
public class InsertDemo
{
public static void main(String...args)throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sholey","root","root");
PreparedStatement pst=con.prepareStatement("insert into starcast values(?,?)");
pst.setString(1,"Gabar");
pst.setString(2,"Basanti"]);
int status=pst.executeUpdate();
if(status>0)
{
System.out.println("data stored");
}
else
System.out.println("not");
con.close();
}
}
-------------------------------------------------------------------------------
When this code is executed then gabar and basanti are entered in the data base table name starcast.
and output is following.







No comments:
Post a Comment