Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun JDBC Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun JDBC The database is the heart of any business system. The business objects that make up an enterprise need some way to make sure they are saved across time. The database engine provides that storage mechanism. SQL was a key first step in simplifying database access. Java’s API builds on that foundation and provides you with shared language through which your application can talk to database engine. Java Data Base Connectivity (JDBC) accomplish its goal through a set of Java interfaces, each implemented differently by individual vendors. The set of classes that implement the JDBC interfaces for a particular database engine is called a JDBC driver. The JDBC API support both two tier and three – tier processing architecture for the database access. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun JDBC Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Database Driver JDBC Drivers are of four types as follows: Type 1 These drivers use a bridging technology to access a database. The JDBC-ODBC bridge that comes with JDK. Advantages: It acts as a good approach for learning JDBC. It may be useful for companies that already have ODBC drivers installed on each client machine — typically Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun the case for Windows-based machines running productivity applications. It may be the only way to gain access to some low-end desktop databases. Disadvantage: It is not suitable for large-scale applications. They are the slowest of all. The performance of system may suffer because there is some overhead associated with the translation work to go from JDBC to ODBC. It doesn’t support all the features of Java. User is limited by the functionality of the underlying ODBC driver, as it is product of different vendor Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Type 2 Type 2 driver are native API drivers. This means that the driver contains Java code that calls native C or C++ methods provided by the individual database vendors that performs database access. Advantage: It has a better performance than that of Type 1, in part because the Type 2 driver contains compiled code that's optimized for the back-end database server’s operating system. Disadvantage: For this, User needs to make sure the JDBC driver of the database vendor is loaded onto each client machine. Must have compiled code for every operating system that the application will run on. Best use is for controlled environments, such as an intranet. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Type 3 The type 3 drivers provide a client with a generic network API that is then translated into database – specific access at the server level. In other words, the JDBC driver on the client uses socket to call middleware application on the server that translates the client requests into an API specific to the desired driver. Advantage: It has better performance than Types 1 and 2. It can be used when a company has multiple databases and wants to use a single JDBC driver to connect to all of them. Since, it is server-based, so there is no requirement for JDBC driver code on client machine. For performance reasons, the back-end server component is optimized for the operating system that the database is running on. Disadvantage: It needs some database-specific code on the middleware server. If the middleware is to run on different platforms, then Type 4 driver might be more effective. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Type 4 Type 4 uses network protocols built into the database engine. Fully Java technology enabled .Type 4 drivers talk directly to the database using Java sockets. This is the most direct pure Java solution. Advantage: It again has better performance than Types 1 and 2 and there is no need to install special software on client or server. It can be downloaded dynamically. Disadvantage: It is not optimized for server operating system, so the driver can’t take advantage of operating system features. For this, user needs a different driver for each different database. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun The JDBC API Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun The important interfaces are: java.sql.DriverManager which handles loading of driver and helps in creating new database connection java.sql.Connection establish connection to a particular database. java.sql.Statement used to execute SQL queries java.sql.ResultSet help to control database record access. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Establishing connection to the Databse Loading MS-Access Driver through JDBC-ODBC try { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); } catch (ClassNotFoundException e) - - - Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Define the connection URL JDBC url are defined as: jdbc: <subprotocol> :<subname> Connection k = DriverManager.getConnection(url, userid, passwd); The DriverManager passes that Connection object back to the application. The url specifies the data source, userid is users login name to the database. Connection k = DriverManager.getConnection(“jdbc:odbc:test”, “harish”,”sgrrits”); Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Create a statement object Statement st = k.createStatement( ); The connection class actually has two version of createStatement( ) the zero parameter version and two parameter version that supports the creation of Statement instances that generates Scrollable ResultSet objects. The default call translates to the following call: k.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); A TYPE_SCROLL_INSENSITIVE ResultSet is unware of in lace edits made to modifiable instances. TYPE_SCROLL_SENSITIVE, on the other hand, means that you can see changes made to the results if you scroll back to the modified row at later times Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Execute a query String str = “select * from emp”; ResultSet rs = st.executeQuery(str); Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun
Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun Process the ResultSet while ( rs.next()) { System.out.println(rs.getString(1) + “ ”+ rs.getString(2)); } Close the connection k.close( ); Connection to the database is closed. Mr. Harish Sharma, Asst. prof., Dept. of CA & IT, SGRRITS, Dehradun