Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

Java Database Connectivity

JDBC (Java Database Connectivity) is an API that allows Java programs to connect to databases. It provides methods for querying and updating data. Developers can use JDBC to obtain connections, execute SQL statements, and process the results through result sets. Key classes include Connection, Statement, PreparedStatement, and CallableStatement. Set methods are used to insert data and get methods to retrieve data from the result set.

Uploaded by

michael.ferraris
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Java Database Connectivity

JDBC (Java Database Connectivity) is an API that allows Java programs to connect to databases. It provides methods for querying and updating data. Developers can use JDBC to obtain connections, execute SQL statements, and process the results through result sets. Key classes include Connection, Statement, PreparedStatement, and CallableStatement. Set methods are used to insert data and get methods to retrieve data from the result set.

Uploaded by

michael.ferraris
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA DATABASE CONNECTIVITY(JDBC)

JDBC

The Java Database Connectivity (JDBC) API is the industry standard for database-independent
connectivity between the Java programming language and a wide range of databases – SQL
databases and other tabular data sources, such as spreadsheets or flat files. The JDBC API
provides a call-level API for SQL-based database access.

JDBC technology allows you to use the Java programming language to exploit "Write Once, Run
Anywhere" capabilities for applications that require access to enterprise data. With a JDBC
technology-enabled driver, you can connect all corporate data even in a heterogeneous
environment.

Functionality

JDBC allows multiple implementations to exist and be used by the same application. The API
provides a mechanism for dynamically loading the correct Java packages and registering them
with the JDBC Driver Manager. The Driver Manager is used as a connection factory for creating
JDBC connections.

JDBC connections support creating and executing statements. These may be update statements
such as SQL's CREATE, INSERT, UPDATE and DELETE, or they may be query statements
such as SELECT. Additionally, stored procedures may be invoked through a JDBC connection.
JDBC represents statements using one of the following classes:

• Statement – the statement is sent to the database server each and every time.
• PreparedStatement – the statement is cached and then the execution path is pre
determined on the database server allowing it to be executed multiple times in an efficient
manner.
• CallableStatement – used for executing stored procedures on the database.

Update statements such as INSERT, UPDATE and DELETE return an update count that
indicates how many rows were affected in the database. These statements do not return any other
information.
Query statements return a JDBC row result set. The row result set is used to walk over the result
set. Individual columns in a row are retrieved either by name or by column number. There may
be any number of rows in the result set. The row result set has metadata that describes the names
of the columns and their types.

Eg> 1) Connection con= DriverManager.getConnection();

PreparedStatement ps = con.prepareStatement( "SQL QUERY" );

2)Connection con= DriverManager.getConnection();

Statement stmt=con.createStatment();

Class.forName():

The method Class.forName(String) is used to load the JDBC driver class. The line below
causes the JDBC driver from some jdbc vendor to be loaded into the application.

Eg> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection

When a connection is needed, one of the DriverManager.getConnection() methods is used to


create a JDBC connection.

Eg> Connection conn = DriverManager.getConnection(


"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" );

SET Methods Used in JDBC:

Set methods are used in JDBC for creating a new row or to update an existing row in a result set.

Set methods:

CHAR setString() VARCHAR2 setString()

NUMBER setBigDecimal() setBoolean() setByte()setShort() setInt() setLong() setFloat()


setDouble()

CLOB setClob()
BLOB setBlob()

RAW setBytes()

LONGRAW setBytes()

DATE setDate() setTime() setTimestamp()

Eg> PreparedStatement ps = null;

try {
ps = c.prepareStatement("INSERT INTO authors VALUES (?, ?, ?)");
ps.setInt(1, 495);
ps.setString(2, "Light-Williams");
ps.setString(3, "Corwin");
ps.executeUpdate();
}
catch (SQLException se)
{
System.out.println("We got an exception while preparing a statement:" +
"Probably bad SQL.");
System.exit(1);
}

setInt(1, 495);

First parameter is the column number.

Second parameter is the value.

GET Method

Get methods are used to retrieve values from the result set.

Eg> ResultSet rs=stmt.executeQuery("select * from tablename");

while(rs.next())

System.out.println(rs.getString("username"));

Username is the column name in the table.


Creation Steps For Connecting Microsoft Access and JAVA:

Step1: Create a Database with a table in Microsoft Access and save it.

Step2: Goto controlpanel>System and Security> Administrative tools>Odbc

And create a datasource.

Step3: Create a Java program and use the created datasource name in getConnection() function.

A Program For JDBC:

import java.io.*;

import java.sql.*;

public class Egjdbc

public static void main(String[ ] args)

Connection con=null;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:DataSourceName ");

Statement stmt=con.createStatment();

ResultSet rs=stmt.executeQuery("select * from TABLENAME");

while(rs.next())

System.out.println(rs.getString("COLUMNNAME"));

You might also like