1 / 18

Java and Databases

Java and Databases. Getting connected. JDBC Architecture. Java application calls the JDBC library. JDBC loads a driver which talks to the database. We can change database engines without changing database code. Application. JDBC. Driver. Data base. The JDBC Connection.

knut
Download Presentation

Java and Databases

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java and Databases Getting connected

  2. JDBC Architecture • Java application calls the JDBC library. • JDBC loads a driver which talks to the database. • We can change database engines without changing database code. Application JDBC Driver Data base

  3. The JDBC Connection • Before you can create a java jdbc connection to the database, you must first import thejava.sql package. import java.sql.*; • Next is to get a Driver try { Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”); //Or any other driver } catch(Exception x){ System.out.println( “Unable to load the driver class!” ); }

  4. Creating a jdbc Statement object • Once a connection is obtained we can interact with the database. • Connection interface defines methods for interacting with the database via the established connection. • To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method. Statement statement = dbConnection.createStatement(); A statement object is used to send and execute SQL statements to a database.

  5. Three kinds of Statements • Statement: Execute simple sql queries without parameters.Statement createStatement()Creates an SQL Statement object. • Prepared Statement: Execute precompiled sql queries with or without parameters.PreparedStatementprepareStatement(String sql)returns a new PreparedStatement object. PreparedStatement objects are precompiledSQL statements. • Callable Statement: Execute a call to a database stored procedure.CallableStatementprepareCall(String sql)returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements.

  6. Executing a SQL statement with the Statement object, and returning a jdbcresultSet. • Statement interface defines methods that are used to interact with database via the execution of SQL statements. • The Statement class has three methods for executing statements:executeQuery(), • executeUpdate(), • execute(). • For a SELECT statement, the method to use is executeQuery . • For statements that create or modify tables, the method to use is executeUpdate. • Note: Statements that create a table, alter a table, or drop a table are all examples of DDL statements and are executed with the method executeUpdate. • execute() executes an SQL statement that is written as String object.

  7. JDBC drivers • JDBC drivers are divided into four types or levels. • Type 1: JDBC-ODBC Bridge driver (Bridge) • Type 2: Native-API/partly Java driver (Native) • Type 3:AllJava/Net-protocol driver (Middleware) • Type 4: All Java/Native-protocol driver (Pure)

  8. Type 1 JDBC Driver • JDBC-ODBC Bridge driver • The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. ODBC is a generic API. • The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

  9. Advantage • The JDBC-ODBC Bridge allows access to almost any database, since the database’s ODBC drivers are already available. • Disadvantages • 1. Since the Bridge driver is not written fully in Java, Type 1 drivers are not portable. • 2. A performance issue is seen as a JDBC call goes through the bridge to the ODBC driver, then to the database, and this applies even in the reverse process. They are the slowest of all driver types. • 3. The client system requires the ODBC Installation to use the driver. • 4. Not good for the Web.

  10. Type 2 JDBC Driver • The distinctive characteristic of type 2 jdbc drivers are that Type 2 drivers convert JDBC calls into database-specific calls i.e. this driver is specific to a particular database. • Example: Oracle will have oracle native api.

  11. Advantage • The distinctive characteristic of type 2 jdbc drivers are that they are typically offer better performance than the JDBC-ODBC Bridge as the layers of communication (tiers) are less than that of Type1 and also it uses Native api which is Database specific. • Disadvantage • 1. Native API must be installed in the Client System and hence type 2 drivers cannot be used for the Internet.2. Like Type 1 drivers, it’s not written in Java Language which forms a portability issue.3. If we change the Database we have to change the native api as it is specific to a database4. Mostly obsolete now5. Usually not thread safe.

  12. Type 3 JDBC Driver • Type 3 database requests are passed through the network to the middle-tier server. • The middle-tier then translates the request to the database. • If the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers.

  13. Advantage • 1. This driver is server-based, so there is no need for any vendor database library to be present on client machines.2. This driver is fully written in Java and hence Portable. It is suitable for the web.3. There are many opportunities to optimize portability, performance, and scalability.4. The net protocol can be designed to make the client JDBC driver very small and fast to load.5. The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advancedsystem administration such as logging and auditing.6. This driver is very flexible allows access to multiple databases using one driver.7. They are the most efficient amongst all driver types. • Disadvantage • It requires another server application to install and maintain. Traversing the recordset may take longer, since the data comes through the backend server.

  14. Type 4 JDBC Driver • The Type 4 uses java networking libraries to communicate directly with the database server. • Advantage • 1. The major benefit of using a type 4 jdbc drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web.2. Number of translation layers is very less i.e. type 4 JDBC drivers don’t have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.3. You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically. • Disadvantage • With type 4 drivers, the user needs a different driver for each database.

  15. SQL • SQL is a standard language for accessing databases • Most Databases have unique syntax issues, but usually share common language elements such as select, delete, insert, etc. • If you know what database your using, learn the sql that is basic to it. It can help. • Many tutorials exist online. • http://www.w3schools.com/SQl/default.asp

  16. Java • The JDBC API makes it possible to do three things: • Establish a connection with a database or access any tabular data source • Send SQL statements • Process the results

  17. ResultSet • A table of data representing a database result set, which is usually generated by executing a statement that queries the database. • A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

  18. Database MySql • The MySQL® software delivers a very fast, multi-threaded, multi-user, and robust SQL (Structured Query Language) database server. MySQL Server is intended for mission-critical, heavy-load production systems as well as for embedding into mass-deployed software. MySQL is a registered trademark of MySQL AB. • Type 4 Drivers are available

More Related