If you are a Windows XP user, it is possible you have to open Performance and Maintenance, before you will see the System icon. You automatically get both JDBC packages java. Install Database The most important thing you will need, of course is an actual running database with a table that you can query and modify. Install a database that is most suitable for you.
We recommend downloading the full Windows installation. These are GUI based tools that will make your development much easier.
Your driver version may vary based on your installation. JDBC drivers are also included as part of the installation. We assume that you have the necessary distribution media to install it. JDBC drivers are also included as a part of the installation. So, you should not worry about this part. When you install any of the above database, its administrator ID is set to root and gives provision to set a password of your choice.
There are various database operations like database creation and deletion, which would need administrator ID and password. You can also check documentation on how to start and stop your database server. Step 2 Start the database server by executing the following command, if it is already not running. Now you are ready to start experimenting with JDBC. This will show you how to open a database connection, execute a SQL query, and display the results.
All the steps mentioned in this template example, would be explained in subsequent chapters of this tutorial. Most often, using import java. Sample Code This sample example can serve as a template when you need to create your own JDBC application in the future.
This sample code has been written based on the environment and database setup done in the previous chapter. Copy and past the following example in FirstExample. Creating statement For example, using JDBC drivers enable you to open database connections and to interact with it by sending SQL or database commands then receiving results with Java. The Java. Third party vendors implements the java. Driver interface in their database driver. When Java first came out, this was a useful driver because most databases only supported ODBC access but now this type of driver is recommended only for experimental use or when no other alternative is available.
The vendor-specific driver must be installed on each client machine. If we change the Database, we have to change the native API, as it is specific to a database and they are mostly obsolete now, but you may realize some speed increase with a Type 2 driver, because it eliminates ODBC's overhead. The JDBC clients use standard network sockets to communicate with a middleware application server.
The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server.
This kind of driver is extremely flexible, since it requires no code installed on the client and a single driver can actually provide access to multiple databases. You can think of the application server as a JDBC "proxy," meaning that it makes calls for the client application.
As a result, you need some knowledge of the application server's configuration in order to effectively use this driver type. Your application server might use a Type 1, 2, or 4 driver to communicate with the database, understanding the nuances will prove helpful.
This is the highest performance driver available for the database and is usually provided by the vendor itself.
This kind of driver is extremely flexible, you don't need to install special software on the client or server.
Further, these drivers can be downloaded dynamically. Because of the proprietary nature of their network protocols, database vendors usually supply type 4 drivers. Which Driver should be Used? If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4.
If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver. Type 2 drivers are useful in situations, where a type 3 or type 4 driver is not available yet for your database. The type 1 driver is not considered a deployment-level driver, and is typically used for development and testing purposes only.
The programming involved to establish a JDBC connection is fairly simple. Import JDBC Packages The Import statements tell the Java compiler where to find the classes you reference in your code and are placed at the very beginning of your source code. To use the standard JDBC package, which allows you to select, insert, update, and delete data in SQL tables, add the following imports to your source code: import java.
Registering the driver is the process by which the Oracle driver's class file is loaded into the memory, so it can be utilized as an implementation of the JDBC interfaces. You need to do this registration only once in your program. You can register a driver in one of two ways. Approach I - Class.
This method is preferable because it allows you to make the driver registration configurable and portable. The following example uses Class. OracleDriver ; DriverManager. For easy reference, let me list the three overloaded DriverManager. A database URL is an address that points to your database. Formulating a database URL is where most of the problems associated with establishing a connection occurs. Using a Database URL with a username and password The most commonly used form of getConnection requires you to pass a database URL, a username, and a password: Assuming you are using Oracle's thin driver, you'll specify a host:port:databaseName value for the database portion of the URL.
It is used to pass driver properties to the driver during a call to the getConnection method. To make the same connection made by the previous examples, use the following code: import java. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects. Relying on the garbage collection, especially in database programming, is a very poor programming practice. You should make a habit of always closing the connection with the close method associated with connection object.
A finally block always executes, regardless of an exception occurs or not. To close the above opened connection, you should call close method as follows: conn. They also define methods that help bridge data type differences between Java and SQL data types used in a database.
The following table provides a summary of each interface's purpose to decide on the interface to use. Interfaces Recommended Use Statement Use this for general-purpose access to your database. Useful when you are using static SQL statements at runtime.
The Statement interface cannot accept parameters. The PreparedStatement interface accepts input parameters at runtime. CallableStatement Use this when you want to access the database stored procedures. The CallableStatement interface can also accept runtime input parameters.
Closing Statement Object Just as you close a Connection object to save database resources, for the same reason you should also close the Statement object. A simple call to the close method will do the job. If you close the Connection object first, it will close the Statement object as well. However, you should always explicitly close the Statement object to ensure proper cleanup.
This sample code has been written based on the environment and database setup done in the previous chapters. Import required packages import java. This statement gives you the flexibility of supplying arguments dynamically. You must supply values for every parameter before executing the SQL statement.
The setXXX methods bind values to the parameters, where XXX represents the Java data type of the value you wish to bind to the input parameter. If you forget to supply the values, you will receive an SQLException. Each parameter marker is referred by its ordinal position. The first marker represents position 1, the next position 2, and so forth. This method differs from that of Java array indices, which starts at 0.
All of the Statement object's methods for interacting with the database a execute , b executeQuery , and c executeUpdate also work with the PreparedStatement object. However, the methods are modified to use SQL statements that can input the parameters. Closing PreparedStatement Object Just as you close a Statement object, for the same reason you should also close the PreparedStatement object.
If you close the Connection object first, it will close the PreparedStatement object as well. However, you should always explicitly close the PreparedStatement object to ensure proper cleanup. Prepare - Example Code Following is the example, which makes use of the PreparedStatement along with opening and closing statments.
The PreparedStatement object only uses the IN parameter. The CallableStatement object can use all the three. The following code snippet shows how to employ the Connection. Using the CallableStatement objects is much like using the PreparedStatement objects. You must bind values to all the parameters before executing the statement, or you will receive an SQLException.
If you have IN parameters, just follow the same rules and techniques that apply to a PreparedStatement object; use the setXXX method that corresponds to the Java data type you are binding.
The registerOutParameter method binds the JDBC data type, to the data type that the stored procedure is expected to return. Once you call your stored procedure, you retrieve the value from the OUT parameter with the appropriate getXXX method.
This method casts the retrieved value of SQL type to a Java data type. Closing CallableStatement Object Just as you close other Statement object, for the same reason you should also close the CallableStatement object. If you close the Connection object first, it will close the CallableStatement object as well. However, you should always explicitly close the CallableStatement object to ensure proper cleanup.
Executing stored procedure The java. ResultSet interface represents the result set of a database query. A ResultSet object maintains a cursor that points to the current row in the result set. The term "result set" refers to the row and column data contained in a ResultSet object. The updates can then be updated in the underlying database as well. The cursor is movable based on the properties of the ResultSet. These properties are designated when the corresponding Statement that generates the ResultSet is created.
Type Description ResultSet. The cursor can scroll forward and backward, and the result set is sensitive to changes made by others to the database that occur after the result set was created.
Concurrency Description ResultSet. This is the default ResultSet. This method returns false if the previous row is off the result set. This method returns false if there are no more rows in the result set. The current cursor location is remembered. For a better understanding, let us study Navigate - Example Code as discussed below. Navigate - Example Code Following is the example, which makes use of few navigation methods described in the Result Set tutorial.
Moving cursor to the last Displaying record For example, if the column you are interested in viewing contains an int, you need to use one of the getInt methods of ResultSet: S. The column index starts at 1, meaning the first column of a row is 1, the second column of a row is 2, and so on.
Similarly, there are get methods in the ResultSet interface for each of the eight Java primitive types, as well as common types such as java. String, java.
Object, and java. There are also methods for getting SQL data types java. Date, java. Time, java. Learning JDBC is very important for java programmers to connect and execute database-related queries. Connections: Before performing any database operation via JDBC, we have to open a database connection. For opening a database connection, we can call the getConnection method of DriverManager class. The Syntax of Connection is as follows:.
We require a statement for all individual queries. ResultSets: A query returns the data in the form of ResultSet. To understand the query result data, ResultSet places a cursor that points to the current row in the result set. The following example makes you understand the basic steps to access a database using JDBC.
0コメント