JDBC-1
Networking
Basics
JDBC, often
known as Java Database Connectivity, provides a Java API for updating and
querying relational databases using Structured Query Language(SQL).
Different type of JDBC Driver
vJDBC - ODBC
Bridge
-Provides a
Java bridge to ODBC
vType 2
Native - API (Partly Java)
-uses
native code to access the Dob with a thin Java wrapper
vType 3 Net -
protocol (All Java)
- defines a generic network protocol that interfaces with some middle ware that
accesses the DB
vType 4
Native-protocol (All Java)
- written entirely in Java
ODBC
Open Database Connectivity: is used
as an interface between Windows applications and different type of databases.
When using Java under Windows it is not possible to access existing
ODBC Driver because ODBC was created
using C with pointers. JDBC-ODBC Driver is used as an interface between Java
Applications and ODBC Driver.
INTRODUCTION TO DATABASE
A database is a collection of tables or relations. Each table
contains set of rows which contains information in the form of Row and Column.
Each column contains meaningful information of specific type.
SQL-Structured Query Language
SQL is a standard
language to manipulate database.
Important SQL Statements
1.To Create Table
CREATE: TABLE STUDENT
(NAME Varchar(32),COURSE varchar(12),FEES
2.To retrieve information
SELECT*FROM STUDENT
WHERE COURSE = 'JAVA'
3. To insert information
INSERT INTO STUDENT
VALUES ('Kumar','JAVA',4800.0,1001')
4. To update information
UPDATE STUDENT SET
COURSE ='DJP'WHERE STUDNO =1001
5. To update information
DELETE * FROM STUDENT
WHERE STUDNO =1001
Six Basic Steps in Using JDBC
1. Load the driver
If you want to use the JDBC - ODBC Bridge driver, the following code
will load it:
Class.for
Name("sun.jdbc.odbc.JdbcOdbcDriver");
You do not need to create an instance of a driver and registger it
with the DriverManager because calling the method Class. for Name will do that
for you automatically.
2.Establish the Connection
Connection
con = DricverManager.get Coonection(url,
"myLogin", "myPassword");
The getConnection method of Driver Manager class establishes a
connectin to the specified ODBC data source name specified in the URL. For JDBC-ODBC
bridge driver,the JDBC URL Will jdbc: odbc:data Source Name. The remaining two
arguments are user name and password of the database.
3. Create a Statement object
Statement stmt = con.createStatement();
A Statement object is used to send SQL statements to the Databa
4. Execute a query
Use the execute Update() method of the Statement object to execute
DDL(Data Definition Language) and SQL commands that update a table (INSERT,
UPDATE,DELETE).
Eg:statement.
execute Update("INSERT INTO STUDENT VALUES('Babu G
C',"+"'HDCA',9600.00'1002')");
Use the executeQuery(...)method of the Statement object to executive a
SQL statement the data retrieved from
database.
Eg:ResultSet rs = statement.execteQuery("SELECT *FROM STUDESNT");
6.Close the connection
Connection.close();
Using Prepared Statements
The Prepared Statement object contains not just an SQL statement, but
an SQL statement that has been precompiled. This means that when the Prepared
Statement is executed, the DBMS can just run the prepare Statement's SQL
STATEMENT without having to complete it
first. The advantage of using prepared Statement is it can take parameters.
Creating a Prepared Statement Object
prepared
Statement preStmt;
String qry = "UPDATE STUDENT SET COURSE=? WHERE STUDNO=?"
preStmt = con.prepare Statement(qry);
Supplying Values for Prepared
Statement parameters
You will need to supply values to be used in place of the question
mark placeholders, if there are any, before you can execute a Prepared
Statement object. You do this by calling one of the setter methods defined in
the interface Prepared Statement. If the value you want to substitute for a
question mark is a Java String, you call the method set String, and so on.
PreStmt.setString(1,
"HDCA");
PreStmt.setInt(2, 1001);
After these values have been set for its two input parameters, Use the
execute Update method to execute the Prepared Statement.
0 comments:
Post a Comment