The sample code that comes with this tutorial creates a database that is used by a proprietor of a small coffee house called The Coffee Break, where coffee beans are sold by the pound and brewed coffee is sold by the cup. To keep things simple, the proprietor needs only two tables, one for types of coffee and one for coffee suppliers.
The following steps configure a JDBC development environment with which you can compile and run the tutorial samples:
- Install the latest version of the Java SE SDK on your computer.
- Install your database management system (DBMS) if needed.
You may use Java DB, which comes with the latest version of Java SE SDK. This tutorial has been tested for the following DBMS:
Note that if you are using another DBMS, you might have to alter the code of the tutorial samples.
- Install a JDBC driver from the vendor of your database.
If you are using Java DB, it already comes with a JDBC driver. If you are using MySQL, install the latest version of Connector/J.
To obtain a JDBC driver for a particular DBMS, refer to JDBC Data Access API.
There are many possible implementations of JDBC drivers. These implementations are categorized as follows:
Check which driver type comes with your DBMS. Java DB comes with two Type 4 drivers, an Embedded driver and a Network Client Driver. MySQL Connector/J is a Type 4 driver.
- Type 1: Drivers that implement the JDBC API as a mapping to another data access API, such as ODBC (Open Database Connectivity). Drivers of this type are generally dependent on a native library, which limits their portability. The JDBC-ODBC Bridge driver is an example of a Type 1 driver.
- Type 2: Drivers that are written partly in the Java programming language and partly in native code. These drivers use a native client library specific to the data source to which they connect. Again, because of the native code, their portability is limited. Oracle's OCI (Oracle Call Interface) client-side driver is an example of a Type 2 driver.
- Type 3: Drivers that use a pure Java client and communicate with a middleware server using a database-independent protocol. The middleware server then communicates the client's requests to the data source.
- Type 4: Drivers that are pure Java and implement the network protocol for a specific data source. The client connects directly to the data source.
Installing a JDBC driver generally consists of copying the driver to your computer, then adding the location of it to your class path. In addition, many JDBC drivers other than Type 4 drivers require you to install a client-side API. No other special configuration is usually needed.
- Install Apache Ant.
These steps use Apache Ant, a Java-based tool, to build, compile, and run the JDBC tutorial samples. Go to the following link to download Apache Ant:
http://ant.apache.org/
Ensure that the Apache Ant executable file is in yourPATHenvironment variable so that you can run it from any directory.
- Download the sample code.
The sample code,JDBCTutorial.zip, consists of the following files:
Create a directory to contain all the files of the sample. These steps refer to this directory as
properties
javadb-build-properties.xmljavadb-sample-properties.xmlmysql-build-properties.xmlmysql-sample-properties.xmlsql
javadb
create-procedure-show-suppliers.sqlcreate-tables.sqldrop-tables.sqlpopulate-tables.sqlmysql
create-procedure-show-suppliers.sqlcreate-tables.sqldrop-tables.sqlpopulate-tables.sqlsrc/com/oracle/tutorial/jdbc
CoffeesTable.javaJDBCTutorialUtilities.javaJoinSample.javaStoredProcedureJavaDBSample.javaStoredProcedureMySQLSample.javaSuppliersTable.javabuild.xml<JDBC tutorial directory>. Unzip the contents ofJDBCTutorial.zipinto<JDBC tutorial directory>.
- Modify the
build.xmlfile.
Thebuild.xmlfile is the build file that Apache Ant uses to compile and execute the JDBC samples. The filesproperties/javadb-build-properties.xmlandproperties/mysql-build-properties.xmlcontain additional Apache Ant properties required for Java DB and MySQL, respectively. The filesproperties/javadb-sample-properties.xmlandproperties/mysql-sample-properties.xmlcontain properties required by the sample.
Modify these XML files as follows:
- In the
build.xmlfile, modify the propertyANTPROPERTIESto refer to eitherproperties/javadb-build-properties.xmlorproperties/mysql-build-properties.xml, depending on your DBMS. For example, if you are using Java DB, yourbuild.xmlfile would contain this:
<property name="ANTPROPERTIES" value="properties/javadb-build-properties.xml"/> <import file="${ANTPROPERTIES}"/>Similarly, if you are using MySQL, yourbuild.xmlfile would contain this:
<property name="ANTPROPERTIES" value="properties/mysql-build-properties.xml"/> <import file="${ANTPROPERTIES}"/>- In the
properties/javadb-build-properties.xmlorproperties/mysql-build-properties.xmlfile (depending on your DBMS), modify the following properties, as described in the following table:
Property Description JAVACThe full path name of your Java compiler, javacJAVAThe full path name of your Java runtime executable, javaPROPERTIESFILEThe name of the properties file, either properties/javadb-sample-properties.xmlorproperties/mysql-sample-properties.xmlMYSQLDRIVERThe full path name of your MySQL driver. For Connector/J, this is typically <Connector/J installation directory>/mysql-connector-java-version-number.jar.JAVADBDRIVERThe full path name of your Java DB driver. This is typically <Java DB installation directory>/lib/derby.jar.DB.VENDORA value of either derbyormysqldepending on whether you are using Java DB or MySQL, respectively. The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.DB.DRIVERThe fully qualified class name of the JDBC driver. For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver. For MySQL, this iscom.mysql.jdbc.Driver.DB.HOSTThe host name of the computer hosting your DBMS. DB.PORTThe port number of the computer hosting your DBMS. DB.SIDThe name of the database the tutorial creates and uses. DB.URLThe connection URL used to connect to your DBMS. You do not need to change this value. DB.USERThe name of the user that has access to create databases in the DBMS. DB.PASSWORDThe password of the user specified in DB.USER.DB.DELIMITERThe character used to separate SQL statements. Do not change this value. It should be the semicolon character ( ;).- Modify the tutorial properties file.
The tutorial samples use the values in either theproperties/javadb-sample-properties.xmlandproperties/mysql-sample-properties.xmlfile (depending on your DBMS) to connect to the DBMS and initialize databases and tables, as described in the following table:
Property Description dbmsA value of either derbyormysqldepending on whether you are using Java DB or MySQL, respectively. The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.jar_fileThe full path name of the JAR file that contains all the class files of this tutorial. Only the sample StoredProcedureJavaDBSample.javarequires this value. See Using Stored Procedures for more information.driverThe fully qualified class name of the JDBC driver. For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver. For MySQL, this iscom.mysql.jdbc.Driver.database_nameThe name of the database the tutorial creates and uses. user_nameThe name of the user that has access to create databases in the DBMS. passwordThe password of the user specified in user_name.server_nameThe host name of the computer hosting your DBMS. port_numberThe port number of the computer hosting your DBMS. - Compile the samples.
At a command prompt, change the current directory to<JDBC tutorial directory>. From this directory, run the following command to compile the samples:
ant compile
- Run the samples.
Each target in thebuild.xmlfile corresponds to a Java class or SQL script in the JDBC samples. The following table lists the targets in thebuild.xmlfile, which class or script each target executes, and other classes or files each target requires:
For example, to run the class
Ant Target Class or SQL Script Other Required Classes or Files drop-tablesdrop-tables.sqlNo other required files build-tablesbuild-tables.sqlRuns the target drop-tablespopulate-tablespopulate-tables.sqlRuns the target build-tablesjavadb-create-procedurejavadb-create-procedure.sqlNo other required files mysql-create-procedureSee the build.xmlfile to view SQL statements that are executed.No other required files runJDBCTutorialUtilitiesNo other required classes runctCoffeesTableJDBCTutorialUtilitiesrunstSuppliersTableJDBCTutorialUtilitiesrunjsJoinSampleJDBCTutorialUtilities,CoffeesTable,SuppliersTablerunspjavadbStoredProcedureJavaDBSample
Note: This class requires extra configuration. See Using Stored Procedures for more information.JDBCTutorialUtilities,SuppliersTable,CoffeesTablerunspmysqlStoredProcedureMySQLSampleJDBCTutorialUtilities,SuppliersTable,CoffeesTableCoffeesTable, change the current directory to<JDBC tutorial directory>, and from this directory, run the following command:
ant runct
java complete tutorial, core java, class method, spring, strut, hibernet,j-unit testing, j-script
Search This Blog
Saturday, January 15, 2011
JDBC:Getting Started
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment