0% found this document useful (0 votes)
4 views

Java Project Workshop - JDBC

java jdbc collections framework

Uploaded by

yasah60522
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)
4 views

Java Project Workshop - JDBC

java jdbc collections framework

Uploaded by

yasah60522
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/ 15

JAVA PROJECT WORKSHOP

(JDBC – Part 2)
Lecture 2
Step-3
(Opening The Connection)

• Create the Connection object:


After we have loaded the driver , the next step is to open the connection to
the database.

To do this , the method to be called is getConnection() of the class


DriverManager and it has the following prototype.

• Syntax:
public static Connection getConnection
(String url,String name,String password) throws SQLException

• Example:
Connection conn=DriverManager.getConnection(
"jdbc:oracle:thin:@//localhost:1521/xe",“project",“java");
Step-4
(Create The Statement Object)

• 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:
Statement stmt=conn.createStatement();
Step- 5
(Execute the Query)

• After we have created the Statement object , the next step is to


send the query to the database and this is done by calling either of
the 2 methods of the Statement object.
• These methods are called : executeUpdate( ) and executeQuery( )
and have the following prototypes:

– public int executeUpdate(String sql) throws SQLException

– public ResultSet executeQuery(String sql) throws SQLException


Step- 5
(Execute the Query)

• The first method is used for non-select queries like


CREATE TABLE,ALTER TABLE, DROP TABLE, TRUNCATE
TABLE, INSERT,UPDATE and DELETE.
• It takes any of the above SQL COMMANDS as argument
and returns the number of rows affected.
• The second method is used only for SELECT command.
• It accepts any valid SELECT command as argument and
returns a set of rows selected by that command.
• These rows are returned as an object of type ResultSet
Step- 5
(Execute the Query)
• ResultSet objects provide access to a table.

• Usually they provide access to the pseudo table that is the


result of a SQL query.
Step- 6
(Process The Result)
• ResultSet objects maintain a cursor pointing to the current row of
data „ this cursor initially points before the first row and is moved to
the first row by the next() method.

• Similarly , the ResultSet also has getXXX( ) methods which allow us


to fetch the values of columns of a row.

• Each of these getXXX() methods attempts to convert the column


value to the specified Java type and returns a suitable Java value.

• For example, getInt() gets the column value as an int, getString() gets
the column value as a String, and getDate() returns the column value
as a Date.
Step- 6
(Process The Result)
• These getXXX() methods have 2 versions:

– public ResultSet getXXX(int colpos) throws SQLException


– public ResultSet getXXX(String colname) throws SQLException

• The first version accepts a column position as in the select


query and the second version accepts the column name
Step- 6
(Process The Result)
• Example:

ResultSet rs=st.executeQuery("Select * from employee");


while(rs.next())
{
int eno=rs.getInt("empno");
String name=rs.getString("ename");
int sal=rs.getInt("sal");
System.out.println(eno+"\t"+name+"\t"+sal);
}
Step-7
(Close The Connection)

• Close the Connection object:


The last step in JDBC is to close the connection to the
databse and this is done by calling the method close of
the Connection object whose prototype is:
public void close()throws SQLException
• Example :
conn.close();
The Complete Code
import java.sql.*;
class Demo
{
public static void main(String [] args)
{
try
{
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("class loaded successfully");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@//Sachin-
PC:1521/orcl","project","java");
System.out.println("Connected to the DB successfully");
Statement st=conn.createStatement();
The Complete Code
ResultSet rs=st.executeQuery("Select * from employee");
while(rs.next())
{
int eno=rs.getInt("empno");
String name=rs.getString("ename");
int sal=rs.getInt("sal");
System.out.println(eno+"\t"+name+"\t"+sal);
}
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Error loading the class "+e);
}
catch(SQLException e2)
{
System.out.println("Problem in connectivity "+e2);
}
}
}
An Important Point!

• Before we compile and run the previous code we need to set


the PATH and CLASSPATH variables.

• The PATH variable allows us to use java commands javac and


java from anywhere in our computer i.e. it makes java
accessible from anywhere , analogous to global variables.

• The variable CLASSPATH allows us to access those java classes


which are not a part of standard jdk packages.In our case we
have to set the CLASSPATH to make the class OracleDriver
accessible to our code.
Setting PATH

• Go to "my computer" properties


• Click on "Advanced system settings"
• Click on "Environment variables"
• Click on new tab of user variable
• Write path in variable name
• Copy the path of jdk’s bin folder
• Paste path of bin folder in variable value followed by %path%
as shown below:
C:\Program Files\Java\jdk1.8.0_66\bin;%path%
• Click OK button
Setting CLASSPATH

• Go to "my computer" properties


• Click on "Advanced system settings"
• Click on "Environment variables"
• Click on new tab of user variable
• Write classpath in variable name
• Copy the path of the jar file of oracle driver which is located inside
the folder lib of the folder jdbc in your Oracle installation folder
• Paste this path in variable name followed by .;%classpath% as
shown below:
D:\oracle\product\10.2.0\db_1\jdbc\lib\classes12.jar;.;%classpath%
• Click OK button

You might also like