A network relational database application system is a three-level structure. The client and server are connected via a network. The client application program communicates with the server-side database program according to the communication protocol; the database service program communicates with the database management system through SQL commands.
There are two ways to connect Java programs to databases. One is to use a JDBC-ODBC bridge to connect to the database, and the other is to use a pure Java JDBC driver to connect to the database.
Connect to database using JDBC-ODBC bridge
The Java program uses the JDBC-ODBC bridge to connect to the database. The process of communicating between the Java program and the database is:
First, the database application issues an API call to the ODBC driver manager. The ODBC driver manager converts this call into an ODBC driver call to the database management system. The database management system in turn converts this call into data input/output to the operating system. call. Finally, the operating system gets the actual data from the database and returns it step by step.
Database programming must first set up the data source. The steps to set up the data source in ODBC are as follows:
Open the Administrative Tools in Windows Control Panel. For Windows XP: Select "Performance Maintenance" >> "Management Tools" >> "Data Source (ODBC)"; For Windows 2000: Select "Management Tools" >> "Data Source".
Open "Data Source". The ODBC Data Source Manager dialog box appears, displaying the existing data source names.
Select "User DSN", click the "Add" button, and the dialog box for installing the data source driver will appear. Access (*.mdb) data source, click the "Finish" button, the "Create Data Source dialog box will appear, type in the name of the data source to be created, and select a database table for the created data source.
Click the "Select" button in the database area and select the required database table. When you need to authorize the access level for the data source, click the Advanced button. After setting the login name and password, click the "OK" button to complete the configuration of the Access database in the ODBC manager.
If a database table does not exist yet, you need to create one.
The data source is the database. After setting the data source, the Java program needs to access the database table and establish a JDBC-ODBC bridge to connect the program to the database. After that, the program can send SQL statements to the database and process the results returned by the database. Java Database Connection JDBC (Java DataBase Connectivity) consists of a set of classes and interfaces written in Java language. JDBC is an API for connecting Java programs to databases. It can do the following three things: establish a connection with a database, send SQL statements to the database, and process the results returned by the database.
Calling the class method Class.forName(String s) can establish a JDBC-ODBC bridge. For example, code:
try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(Exception e){}
Drivers are loaded for Java programs.
[Example] Describe the method connectByJdbcOdbc() to connect to the database. This method connects to the database according to the given database URL, user name and password. If the connection is successful, the method returns the connection object. If the connection is unsuccessful, it returns empty.
public static connection connectByjdbcOdbc(String url, String username, String password){ Connection con = null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load ODBC driver} catch (Exception e){ e.printStackTrace(); return null; //Connection failed} try{ con = DriverManager.getConnection(url, username, password); } catch (SQLExceotuib e){ e.printStackTrace(); return null; //Connection unsuccessful} return con; //Connection successful}
The following code is a call to the connectByJdbcOdbc() method. If the database connection is successful, the database connection success message window will pop up. Otherwise, the database connection failure message window will pop up.
if ((con = connectByJdbcOdbc("jdbc:odbc:redsun", "xia", "1234")) != null){ JoptionPane.showMessageDialog(null, "Database connection successful"); try{ con.close(); con = null; } catch (SOLException e){}}else JOptionPane.showMessageDialog(null, "Database connection failed");
Use pure Java JDBC driver to connect to database
Java programs can also use pure Java JDBC drivers to connect to the database. This method is widely used, but it requires downloading the corresponding driver package, because the connection codes of different databases may be different, and the drivers loaded may also be different when connecting to different databases. For example, the driver to connect to SQLServer is downloaded from the www.msdn.com website. There are three packages: msbase.jar, mssqlserver.jar and msutil.jar, and these three packages are required to be placed in jdk/jre/lib/ext/ directory, or set its placement in CLASSPATH.
The process of using a pure Java JDBC driver to connect to the database is as follows:
Load the driver. There are two ways to load the driver:
One is to add the driver to the property jdbc.drivers of java.lang.System. This is a list of driver class names loaded by the DriverManager class, separated by colons.
Another way is to use the Class.forName() method to load the specified driver in the program after downloading the driver from the relevant website. For example:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Creates a URL for the specified database. The URL object of the database is similar to the uniform resource locator of the network, and its format is:
jdbc:subProtocol:subName://hostname:port:Databasename=XXX
Among them, subprotocol is a database connection mechanism supported by a certain driver; subName is the specific name under the current connection mechanism; hostName is the host name; port is the corresponding connection port; DatabaseName is the name of the database to be connected. For example, the following code could be a database URL:
jdbc:Microsoft:sqlserver://localhost:1433;Databasename=ksinfo
The URL description of the database uses the mechanism provided by Miscrosoft and uses the sqlserve driver to access the ksInfo database on the local machine through port 1433.
Establish a connection. The method getConnection() of the DriverManager establishes a connection.
[Example] Describe the static method connectByJdbc() that connects to the database. This method connects to the database according to the given database URL, user name and password. If the connection is successful, the method returns true. If the connection is unsuccessful, it returns false.
public static Connection conectByJdbc(String url, String username, String password){ Connection con = null; try{ Class.forName( //Load the specific driver "com.microsoft.jdbc.sqlserver.SQLServerDriver"); } catch (Exception e){ e.printStackTrace(); return null; //Connection failed} try{ con = DriverManage.getConnection(url, username, password); } catch (SQLException e){ e.printStackTrace(); return null; //Connection failed} return con; //Connection successful}