This week's team project work is mainly to complete the detailed design, in the implementation of the system these days I mainly learn to use the JDBC Connection database, has not been contacted before, so from the most basic start to understand learning.
What is 1.JDBC?
JDBC represents a Java database connection, which is a Java API that is independent of the database-based connection standard between the Java programming language and a wide range of databases.
The JDBC Library contains APIs for each task that is typically associated with the use of the database:
Make connection to Database
Create SQL or MySQL statements
Execute SQL or MySQL query database
View and modify result records
Fundamentally, JDBC is a specification that provides a complete set of interfaces that allow portable access to the underlying database. You can use Java to write different types of executables, such as:
All these different executables can use the JDBC driver to access the database and take advantage of the stored data.
JDBC provides the same functionality as ODBC, which allows Java programs to contain database-independent code.
Prerequisite:
The following two topics are a good idea for this tutorial as scheduled:
Core Java Programming
SQL or MySQL Database
JDBC Schema:
The JDBC API supports two-tier and three-tier processing models for database access, but in a generic JDBC architecture consists of two tiers:
The JDBC API uses Driver Manager and database-specific drivers to provide transparent connections to heterogeneous databases.
The JDBC Driver Manager ensures that the correct drivers are available to access each data source. The driver Manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases.
Note: More about JDBC content (common JDBC components, packages, etc.) the following links are described in more detail: http://www.yiibai.com/jdbc/jdbc-introduction.html
2.JDBC Connection Database:
After you install the appropriate driver, you can begin establishing a database connection using JDBC.
Programming that involves building a JDBC connection is fairly straightforward. Here are a few simple four steps:
Import the JDBC Package: Add import statements to the Java program to import the required classes in Java code.
registering the JDBC driver: This step causes the required driver for the JVM to load into memory, so it can implement the JDBC request.
Database URL Development: This is to create a properly formatted address that points to the database to which you want to connect.
Create Connection object: Finally, the code calls the Getconnection () method of the DriverManager object to establish the actual database connection.
To import the JDBC package:
The import statement tells the Java compiler where to find the class that is referenced in the code and placed at the beginning of your source.
Using the standard JDBC package, it allows you to select, INSERT, UPDATE and delete data in the SQL table, adding the following import to your source code:
Import java. SQL. *; For standard JDBC programsimport java. Math. *; For BigDecimal and BigInteger Support
To register the JDBC driver:
Before using it, you must register your driver in the program. Registering a driver is a class file that is loaded by the Oracle driver into memory so that it can be used as a JDBC interface implementation.
Need to do this registration only once in your program. You can register one driver in one of the following two ways.
Method (i)-drivermanager.registerdriver ():
The second way that you can use it to register a driver is to use the Staticdrivermanager.registerdriver () method.
Should, if you are using an incompatible JDK JVM, such as Microsoft provides a method using the Registerdriver ().
The following example uses Registerdriver () to register the Oracle driver:
Try { DriverMydriver= NewOracle.Jdbc.Driver.Oracledriver(); DriverManager.Registerdriver( Mydriver ); }catch ( Classnotfoundexception Ex) { system.. Println ( "error:unable to load driver class!" system.1}
Database URL Development:
When loading the driver, you can establish a connection using the Drivermanager.getconnection () method in the program. For easy reference, let's list three overloaded Drivermanager.getconnection () methods:
getconnection (String URL)
getconnection (String URL, Properties prop)
getconnection (string url, string user, string password)
Here, a database URL is required for each form. The URL of the database is the point to the database address.
Developing a database URL is mostly related to establishing a connection.
To create a Connection object: Use the user name and password for the database URL:
The following three forms of the Drivermanager.getconnection () method are used to create a connection object. Getconnection () The most common form requires passing a database URL, user name username and password Password:
Value to the URL database section databasename: Assuming that you are using an Oracle thin driver, you need to specify a host: port.
Assuming that there is a host TCP/IP address 192.0.0.1 and that the hostname and Oracle listener are configured on port 1521, the database name is EMP, and then the full database URL is:
JDBC:oracle:thin:@amrood:1521:EMP
Now, you must call the appropriate user name and password and the getconnection () method to get a connection object, as follows:
StringUrl= "Jdbc:oracle:thin: @amrood: 1521:emp"string USER = "username" ;string PASS = password "connection conn = Span class= "Typ" >drivermanager. (url, USER , Pass Note: The other two forms are: Use only one database URL, use the URL of the database, and a Properties object. See the following links for specific implementation instructions: http://www.yiibai.com/jdbc/jdbc-db-connections.html
To close the JDBC connection:
At the end of the JDBC program, it explicitly requires that all connections to the database be closed to end each database session. However, if you forget, the Java garbage collector will clean up stale objects when it closes the connection.
Relying on garbage collection, especially in database programming, is a very poor programming habit. The habit of closing the close () method associated with the connection object should always be turned off.
To ensure that the connection is closed, you can execute it in the finally block in the code. Finally blocks are executed, whether or not they occur or are no exception.
To close the open connection above, you should call the close () method, as follows:
Conn. Close();
Explicitly close the connection DBMS to conserve resources.
Summary: The first time to use JDBC to connect to the database, in the process did encounter a lot of problems, such as Java, Oracle use of unskilled for the whole process has a great impact, so still need to strengthen their programming ability, for some mistakes in the process need to be a step-by-step patient solution.
Sixth job-database connection