0% found this document useful (0 votes)
10 views19 pages

Mod5 9

Notes

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Mod5 9

Notes

Uploaded by

pp6524878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

JDBC Connection

Watch video which demonstrates jdbc


connection in ubuntu
https://youtu.be/45383b8dNCI
Steps to connect a Java Application to
Database
1.Register the driver class
2. Create a Connection
3. Create the Statement object
4. Execute the query
5.Close the connection
1.Register the driver class

The forName() method is used to register the


driver class. This method is used to dynamically
load the driver class. driver class for the mysql
database is com.mysql.jdbc.Driver
Syntax of forName() method
public static void forName(String className
) throws ClassNotFoundException
Example to register the MySqlDriver driver class
-
Class.forName("com.mysql.jdbc.Driver”);
2. Create a Connection

The getConnection() method of DriverManager


class is used to establish connection with the
database.
Syntax of getConnection() method
getConnection(String url, String username,
String password) throws SQLException
eg:
Connection con =
DriverManager.getConnection("jdbc:mysql://localh
ost:3306/test", "root", “”);
Username: The default username for the mysql database
is root.
3. Create the Statement object

The createStatement() method of


Connection interface is used to create
statement. The object of statement is
responsible to execute queries with the
database.
Syntax
public Statement
createStatement()throws SQLException
Example to create a SQL statement
Statement stmt=con.createStatement();
4. Execute the query

The executeQuery() method of Statement


interface is used to execute SQL statements.
This method returns the object of ResultSet
that can be used to get all the records of a
table.
Syntax
public ResultSet executeQuery(String
query)throws SQLException
Example to execute a SQL statement
ResultSet rs=stmt.executeQuery("select *
from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+"
"+rs.getString(2));
}
executeUpdate(): This method is used to
execute statements such as insert, update,
delete. It returns an integer value
representing the number of rows affected.
executeQuery(): This method is used to
execute statements that returns tabular data
It executes only select statements
5.Closing the connection

After executing SQL statement you need to


close the connection and release the session.
The close() method is used to close the
connection.
Syntax
public void close()throws SQLException
 eg:
con.close();
Create a mysql table employee(empno
varchar(10) primary key, ename
varchar(10),department varchar(10)) and
insert five records.
Write a java program to display the
employee’s records in the order of
department name.
First create database for eg: office
Create a table named employee with
specified fields
Then insert five records into the table.
import java.io.*;
import java.sql.*;
public class Emp1 {
public static void main(String[] args)throws
ClassNotFoundException {
Connection conn = null;
Statement stmt=null;
String url =
"jdbc:mysql://localhost:3306/office";
try
{
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(url,
"root”, “”);
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from
employee ORDER BY department");
while(rs.next())
{
String s1=rs.getString(“empno");
String s2=rs.getString(“ename");
String s3=rs.getString(“department");

System.out.println(“employee no, name and


department are :"+s1+"\t"+s2 +"\t"+s3);
}
}
catch (SQLException e) {
System.out.println(e);
}
}
}
Program to do …
Create a mysql table mark(regno int primary
key, sname varchar(10),mark int) and insert
five records. Write a Java program to read
and display the content of mark table.

Set database name as student


int regno=rs.getInt("regno");
String str=rs.getString(“sname");
int mark=rs.getInt("mark");

You might also like