0% found this document useful (0 votes)
25 views10 pages

JDBC Junit

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

JDBC Junit

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

What Is Java JDBC?

JDBC is an abbreviation for the term Java Database Connection. JDBC is a


software tool, also known as an application programming interface that establishes
a connection between a standard database and Java application that intends to use
that database.
Need for Java JDBC
JDBC is used for multiple applications to connect to various types of data storage
units that are both standard and advanced. We need JDBC for the following
purposes.
 For establishing stable database connectivity with the application API.
 To execute SQL(Structured Query Language) queries and DDL/DML
commands.
 For viewing and modify data records in the data storage units.
Java JDBC Architecture
The Java JDBC architecture consists of the following primary segments. They are:
 JDBC Application
 JDBC API
 JDBC Manager
 JDBC Drivers
 Data Storage Units

1
Steps to Connect Java JDBC
There are eight crucial steps to be followed to establish the JDBC connection and
process the data. Those steps are as follows:
 Import-Package Dependencies
 Load and Register the Driver
 Connect to the Database
 Frame Query
 Execute Query
 Process Result
 Close Statement
 Close connection

2
Establishing Connection in JDBC

Class.forName ():
This method loads the driver class files into memory during the run time. You don't
have to use new objects or create objects. In the following example uses
Class.forName () to load the Oracle driver as follows:
The syntax for this is Syntax:
 Class.forName(“oracle.jdbc.driver.OracleDriver”)
Establish a connection using the Connection class object
After loading the driver, establish connections as shown below as follows:
Connection con = DriverManager.getConnection(url,user,password)

• user: Username from which your SQL command prompt can be accessed.
• password: password from which the SQL command prompt can be accessed.
• con: It is a reference to the Connection interface.
• Url: Uniform Resource Locator which is created as shown below:
String url = “ jdbc:oracle:thin:@localhost:1521:xe”
Example:
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system
","root");
 jdbc:oracle:thin:@loca;
 lhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver,
localhost is the server name on which oracle is running, we may also use IP
address, 1521 is the port number and XE is the Oracle service name.

3
Connection Establishment:
class.forName("oracle.jdbc.driver.OracleDriver");

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system
","root");
System.out.println("Connection established");
Inserting record In to table:
A PreparedStatement is a pre-compiled SQL statement. It is a subinterface of
Statement.
Advantages of PreparedStatement
 When PreparedStatement is created, the SQL query is passed as a parameter.
This Prepared Statement contains a pre-compiled SQL query, so when the
PreparedStatement is executed, DBMS can just run the query instead of first
compiling it.
 We can use the same PreparedStatement and supply with different
parameters at the time of execution.
Methods of PreparedStatement:
 setInt(int, int): This method can be used to set integer value at the given
parameter index.
 setString(int, string): This method can be used to set string value at the given
parameter index.
 setFloat(int, float): This method can be used to set float value at the given
parameter index.
 setDouble(int, double): This method can be used to set a double value at the
given parameter index.
 executeUpdate(): This method can be used to create, drop, insert, update,
delete etc. It returns int type.

4
 executeQuery(): It returns an instance of ResultSet when a select query is
executed.
PreparedStatement psmt=con.prepareStatement("insert into emps
values(?,?,?)");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true) {
System.out.println("enter emp no");
int eno=Integer.parseInt(br.readLine());
System.out.println("enter emp name");
String name=br.readLine();
System.out.println("enter salary");
int sal=Integer.parseInt(br.readLine());
psmt.setInt(1, eno);
psmt.setString(2, name);
psmt.setInt(3, sal);
int count=psmt.executeUpdate();
if(count>0)
System.out.println(count+" Records are inserted");
else
System.out.println("No recoed inserted");
System.out.println("Do you want continue(yes/no)?");
String ch=br.readLine();
if(ch.equalsIgnoreCase("no"))
break;
}

5
For select statement
PreparedStatement pstmt = null;
ResultSet rst = null;
String myQuery = "select Id,Name,Age from JavaPreparedStatement";
try {
con = DriverManager.getConnection(JdbcURL, Username, password);
pstmt = con.prepareStatement(myQuery);
rst = pstmt.executeQuery();
System.out.println("Id\t\tName\t\tAge
");
while(rst.next()) {
System.out.print(rst.getInt(1));
System.out.print("\t\t"+rst.getString(2));
System.out.print("\t\t"+rst.getInt(3));
System.out.println();
}
} catch(Exception exec) {
exec.printStackTrace();
}
}
}

6
String query = "select * from tblemployee";
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
Integer empId = rs.getInt(1);
String firstName = rs.getString(2);
String lastName = rs.getString(3);
Date dob = rs.getDate(4);
System.out.println("empId:" + empId);
System.out.println("firstName:" + firstName);
System.out.println("lastName:" + lastName);
System.out.println("dob:" + dob);
System.out.println("");

7
Create table Statement
Statement smt=con.createStaetment();
smt.Executeupdate("create table emp(eno number(5).ename char(10),sal
number(10));
Insert statement
PreparedStatement psmt=con.prepareStatement("insert into emps values(?,?,?)");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true) {
System.out.println("enter emp no");
int eno=Integer.parseInt(br.readLine());
System.out.println("enter emp name");
String name=br.readLine();
System.out.println("enter salary");
int sal=Integer.parseInt(br.readLine());
psmt.setInt(1, eno);
psmt.setString(2, name);
psmt.setInt(3, sal);
int count=psmt.executeUpdate();

8
Select staement
String query = "select * from tblemployee";
Staement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
Integer empId = rs.getInt(1);
String firstName = rs.getString(2);
String lastName = rs.getString(3);
Date dob = rs.getDate(4);
System.out.println("empId:" + empId);
System.out.println("firstName:" + firstName);
System.out.println("lastName:" + lastName);
System.out.println("dob:" + dob);
System.out.println("");

9
JUnit Tutorial
https://www.javatpoint.com/junit-tutorial

10

You might also like