>>PREVIOUS>>
12. JDBC-ODBC
JDBC is a set of Java API for executing SQL statements.
Two-Tier Model
----------------------
Client Machine
-----------------------
DBMS propictary protocol
Database server
Three-Tier Model
Client Machine(GUI)
HTTP, RMI, CORBA
Server Machine
DBMS-proprictary protocol
Database Server
Two-Tier Model
In Two-Tier Model, a Java applet or application talks directly to the database. This requires a JDBC driver that can communicate with the particular database management systems accessed. Users SQL statement is delivered to the database and the results of those statements are sent to the user. This is referred to as client/server configuration.
Three-Tier Model
In this a middle tier is introduced for fast performance. It sends the SQL statements to the databases. The results of the query are send to middle tier, which sends them to user.
1. GetConnection( ) of DriverManager class is used to get connection object.
2. It prepares Statement Object to prepare SQL statement
3. The method executeQuery( ) is used to obtain ResultSet and the method executeUpdate( ) is used to for deleting, updating or inserting records.
4. Rollback( ) and commit( ) are used to undo or permanent save the changes.
Example :
a) Create a data base students in Ms-Access with table student containing the following fields studid number, sname text, course text and marks number.
Create a datasource studentdsn in ODBC for the above database
Write the source file , compile and execute it. import java.sql.*;
class dbappn
{
static connection con;
public static void main(String a[ ]) throws Exception
{
class.forName("sun.jdbc.odbc.JdbcOdbcdriver");
open( );
select( );
insert( );
delete( );
update( );
select( );
close( );
}static void open( ) throws SQLException
{
/*con = DriverManger.getConnection("dsn","username","pwd"); */
con = DriverManager.getConnection("jdbc:odbc:student","palani","kumar");
con.setAutoCommit(false);
}
static void close( ) throws SQLException
{
con.commit( );
con.close( );
}
static void select( ) throws SQLException
{
Statement stmt = con.createStatement( );
ResultSet rs = stmt.executeQuery("Select * from student");
Boolean more = rs.next( );
If (!more)
{
System.out.println("No rows found");
Return ;
}
while(more)
{
System.out.println("ID " : " + rs.getString("studid"));
System.out.println("Name : " + rs.getString("sname"));
System.out.println("Course : " + rs.getstring("course"));
System.out.println("Marks : " + rs.getString("marks"));
more = rs.next( );
}
rs.close( );
stmt.close( );
}
static void insert( )
{
try{
Statement stmt = con.createStatement( );
int rows = stmt.executeUpdate("Insert into student
values(100, ‘Subash’,’Java’,80)");con.commit( );
stmt.close( );
System.out.println(rows + " row added");
} catch(SQLException s) { System.out.println("Error"); }
}
static void delete( )
{
try{
Statement stmt = con.createStatement( );
int rows = stmt.executeUpdate("Delete from student
where id = 100;con.commit( );
stmt.close( );
System.out.println(rows + " row deleted");
} catch(SQLException s) { System.out.println("Error"); }
}
static void update( )
{
try{
Statement stmt = con.createStatement( );
int rows = stmt.executeUpdate("Update student
set marks = 90 where id =100 ;con.commit( );
stmt.close( );
System.out.println(rows + " row added");
} catch(SQLException s) { System.out.println("Error"); }
}
}
>>>NEXT>>>
No comments:
Post a Comment