The JDBC API defines Java classes to represent database connections, SQL statements, result sets, database metadata, etc. It allows a Java programmer to issue SQL statements and process the results. JDBC is the primary API for database access in Java.
The JDBC API is implemented via a driver manager that can support multiple drivers connecting to different databases. JDBC drivers can either be entirely written in Java so that they can be downloaded as part of an applet, or they can be implemented using native methods to bridge to existing database access libraries. For more information about JDBC, visit the JavaSoft JDBC Database Access API Web page at http://splash.javasoft.com/jdbc/.
JBuilder uses the JDBC API to access the information stored in databases. Many of JBuilder's data-access components and classes use the JDBC API. Therefore, these classes must be properly installed in order to use the JBuilder database connectivity components. (See Installing JBuilder, JDBC, and the JDBC-ODBC Bridge.)
In addition you need an appropriate JDBC driver to connect your database application to a remote server. Drivers can be grouped into two main categories: drivers implemented using native methods that bridge to existing database access libraries, or all-Java based drivers. Drivers that are not all-Java must run on the client (local) system. All-Java based drivers can be loaded from the server or locally. The advantages to using a driver entirely written in Java is that it can be downloaded as part of an applet and is cross-platform. Some of the driver options that ship with the JBuilder Client/Server edition are:
DataGateway provides Java developers with a multi-tier, fast, and reliable database connectivity solution adhering to industry-standard JDBC. It consists of an all-Java connection to BDE (the Borland Database Engine), which in turn can access a wide range of SQL Servers and desktop databases. DataGateway provides native connectivity to Oracle, Sybase, DB2, Microsoft SQL Server, Informix, InterBase, Paradox, dBASE, Microsoft FoxPro, and Microsoft Access. DataGateway also provides additional connectivity through standard ODBC drivers. DataGateway's client JDBC driver is written entirely in Java and can run on all Java virtual machines that support JDK 1.1. The DataGateway Server currently supports Windows NT and Windows 95. See Using DataGateway for more information.
As an all-Java JDBC driver for InterBase, InterClient enables platform-independent, client/server development for the Internet and corporate Intranets. The advantage of an all-Java driver versus a native-code driver is that you can deploy InterClient-based applets without having to manually load platform-specific JDBC drivers on each client system (the Web servers automatically download the InterClient classes along with the applets). Therefore, there's no need to manage local native database libraries, which simplifies administration and maintenance of customer applications. As part of a Java applet, InterClient can be dynamically updated, further reducing the cost of application deployment and maintenance. See Using InterClient for more information.
To connect to a remote SQL database, you need either of the following:
The two options when connecting to local, non-SQL databases such as Paradox or Visual dBASE are:
If you encounter any problems connecting to a JDBC database, see the topic Troubleshooting JDBC database connections in the tutorials.
This tutorial assumes you are familiar with the JBuilder design tools. For more information on these tools, see JBuilder's visual design tools in the Getting Started Guide.
This tutorial outlines:
Note: When you no longer need a Database connection, you should explicitly call the Database.closeConnection() method in your application. This ensures that Database classes which hold references to JDBC connections automatically close the connection when the Database object is garbage collected.
The Database component is a JDBC-specific component that manages a JDBC connection. QueryDataSet and ProcedureDataSet components have a database property. Multiple data sets can share the same database, and often will.
To add the Database component to your application,
Database database1 = new Database();
The Database component appears in the Component tree window:
The Database connection property specifies the JDBC connection URL, User Name, Password, and optional JDBC driver(s). The JDBC connection URL is the JDBC method for specifying the location of a JDBC data provider (i.e., SQL server). It can actually contain all the information necessary for making a successful connection, including user name and password.
You can access the ConnectionDescriptor object programmatically or you can set connection properties through the user interface. The following steps describe how to do this in the UI Designer and provide the resulting code.
Property | Description |
---|---|
Connection URL | The Universal Resource Locator (URL) of the database, for example, jdbc:odbc:DataSet Tutorial . Select the Choose URL button to select an URL from a list of previously selected URLs, available data sources, or DataGateway sources. |
Username | The user name authorized to access the server database, for example, SYSDBA . |
Password | The password for the authorized user, for example, masterkey . |
Prompt user for password | Whether to prompt the user for a password each time. |
Driver Class | The class name of the JDBC driver that corresponds to the URL, for example, sun.jdbc.odbc.JdbcOdbcDriver . |
database1.setConnection(new borland.sql.dataset.ConnectionDescriptor("jdbc:odbc:DataSet Tutorial", "SYSDBA", "masterkey", false, "sun.jdbc.odbc.JdbcOdbcDriver"));