* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Java DATABASE CONNECTIVITY JDBC
Extensible Storage Engine wikipedia , lookup
Tandem Computers wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft Access wikipedia , lookup
Ingres (database) wikipedia , lookup
Concurrency control wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Database model wikipedia , lookup
Clusterpoint wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Relational model wikipedia , lookup
JDBC
The JDBC (Java Database Connectivity)
API helps a Java program to access a
database in a standard way
JDBC is a specification that tells the
database vendors how to write a driver
program to interface Java programs with
their database
JDBC
A Driver written according to this
standard is called the JDBC Driver
All related classes and interfaces are
present in the java.sql package
All JDBC Drivers implement the
interfaces of java.sql
Database interaction
The steps involved in a database
interaction are:
– Loading the specific driver
– Making a connection to the database
– Sending SQL statements to the database
– Processing the results
Statement
A statement object is used to send SQL statements to
a database.
Three kinds :
Statement
Execute simple SQL without parameters
PreparedStatement
Used for pre-compiled SQL statements with or
without parameters
CallableStatement
Execute a call to a database stored procedure or
function
JDBC - classes and interfaces
DriverManager class Manages all the JDBC Drivers that are
loaded in the memory
Helps in dynamic loading of Drivers
Methods in DriverManager class getConnection() : to establish a connection
to a database.
○ Connection getConnection(String url,
Properties info)
○ Connection getConnection(String url)
○ Connection getConnection(String url, String
userID, String password)
registerDriver(java.sql.Driver)
The different methods of Connection
interface are:
close() - closes the database connection
createStatement() - creates an SQL
Statement object
prepareStatement() - creates an SQL
PreparedStatement object.
(PreparedStatement objects are precompiled
SQL statements)
prepareCall() - creates an SQL
CallableStatement object using an SQL
string. (CallableStatement objects are SQL
stored procedure call statements)
Statement interface - defines methods that are used to interact
with database via the execution of SQL statements.
The different methods are:
executeQuery(String sql) - executes an SQL statement (SELECT) that
queries a database and returns a ResultSet object.
executeUpdate(String sql) - executes an SQL statement
(INSERT,UPDATE,or DELETE) that updates the database and returns
an int, the row count associated with the SQL statement
execute(String sql) - executes an SQL statement that is written as
String object
getResultSet() - used to retrieve the ResultSet object
ResultSet Interface - maintains a pointer to a
row within the tabular results. The next() method
is used to successively step through the rows of
the tabular results.
The different methods are:
getBoolean(int) - Get the value of a column in
the current row as a Java boolean.
getByte(int) - Get the value of a column in the
current row as a Java byte.
getDouble(int) - Get the value of a column in
the current row as a Java double.
getInt(int) - Get the value of a column in the
current row as a Java int.
import java.sql.*;
class JDBCTest{
public static void main(String args[]) {
try{
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@DB,
IPaddress:port_no:host string",“uid",“password");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery ("select * from
Student");
while (resultSet.next()) {
System.out.println ( resultSet.getInt ("ClassNo"));
}
}
catch (Exception exception) {
System.out.println (exception)
}
}
}
PreparedStatement interface -- helps us
to work with precompiled SQL
statements
Precompiled SQL statements are faster
than normal statements
So, if a SQL statement is to be repeated, it
is better to use PreparedStatement
Some values of the statement can be
represented by a ? character which can be
replaced later using setXXX method
import java.sql.*;
class PreparedStatementTest{
public static void main(String args[]) throws Exception{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection(“url“, “UID", “password ");
PreparedStatement preparedStatement =
connection.prepareStatement("select * from
Emp where ename=?");
preparedStatement.setString(1, 7521);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("ename"));
}
}
catch(Exception exception){
System.out.println(exception);
}
}
}
CallableStatement interface -- helps us
to call stored procedures and functions
CallableStatement callableStatement =
connection.prepareCall(“execute proc ?”);
callableStatement.setInt(50);
callableStatement.execute();
The out parameters are to be registered
callableStatement.registerOutParameter(int
parameterIndex, int SQLType);
To get the value stored in the out parameter-callableStatement.getXXX(int parameterIndex);
Example - Calling a stored procedure named GetSalary. The
procedure queries on the Employee table and returns the salary
of an employee. It has one input parameter that takes the
EmpCode and an out parameter that returns the salary
CallableStatement callableStatement =
connection.("begin GetSalary(?,?); end;");
callableStatement.(1,29418);
// OUT parameters must be registered.
callableStatement.(2,Types.DOUBLE);
callableStatement.execute();
System.out.println("Salary : “ +callableStatement.getDouble(2));
ResultSetMetaData Interface - holds
information on the types and properties of the
columns in a ResultSet. Provides information
about the database as a whole. Constructed from
the Connection object
The different methods are:
getColumnName()
getColumnType()
getColumnLabel(count)