We
have six steps to connect to the database in java. The following steps are used
by Java programmers while using jdbc in their java programs.
2. Creating connection
3. Creating statement
4. Executing queries
5. Retrieving the Results
6. Closing connection
1. Registering the driver :
There are several drivers available in the market we should
first declare a driver which is going to be used for communication with the
database server in a java program.
Registering the driver in 4 ways:
1. By creating an object to Driver class of the driver software.
com.mysql.jdbc.Driver driver obj=new com.mysql.jdbc.Driver();
2. By sending the driver class object to registerDriver ()
method of DriverManger class.
Example :
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
3. By sending the driver class name directly to forName ()
method.
Example :
Class.forName(com.mysql.jdbc.Driver());
4. By passing the driver at the time of running the program we
can use getProperty () method of the System class.
Example :
String dName = System.getProperty("driver");
Class.forName(dName);
We pass the command line arguments
Example :
c:\> java-d driver = driverclassname program name.
2. Creating connection :
The getConnection () method of DriverManager class is used to
establish connection with the database.
We pass three arguments through the getConnection () method.
URL of the Database.
Username.
Password.
Example :
DriverManager.getConnection("com.mysql.jdbc.Driver",
"root", "root");
3. Creating statement :
The createStatement () method of Connection interface is used to
create Statement.
The object of the statement is responsible to execute queries to
the database.
Example :
Statement st=con.createStatement();
4. Executing queries :
The executeQuery () method of Statement interface is used to
execute Queries to the database.
This method returns the object of ResultSet that can be used to
get all the records of a table.
Example :
public ResultSer rs=st.executeQuery("select * from
Emp");
5. Retrieving the Results :
The Result obtained by executing SQL Statements can be stored in
an object with the help of interfaces like ResultSet () method,
resultSetMetaData () and dataBaseMetaData ().
Example :
ResultSer
rs=st.executeQuery("select * from Emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+""+rs.getString(2));
}
6. Closing connection :
By using the close() method of Connection interface is used to
close the
connection.
By closing the connection object statement and ResultSet will be
closed automatically.
Example :
con.close();
Connect to the database in jdbc :
Example :
package com.wins.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connectiondatabase
{
public static void main(String[]
args) throws SQLException
{
System.out.println("This
is simple jdbc connection program to connect the mysql database");
String username="root";
//
the username is mysql database login username
String password="root";
//
the password is mysql database login password
String dbname="test";
//
the dname is the database name or schema
String url="jdbc:mysql://localhost:3306/";
//
the url is the address of the mysql database
with port number the default port number is 3306
Connection conn=null;
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//
registering the driver using second method
conn=DriverManager.getConnection(url+dbname,username,password);
//
Creating the connection
System.out.println("successfully
connected");
conn.close();
//Closing
the connection
System.out.println("connection closed");
}
}
Output :
Successfully connected
source technologiesleader
No comments:
Post a Comment