JDBC foundation (2)
Because it is the basic article, it is still a brief explanation for each step:
As mentioned earlier, the registration driver has a multi-way method, class.Forname (); is an explicit load. When a drive
After the ClassLoader is loaded by the ClassLoader, DRIVERMANAGER will register this driver class during the dissolution process.
This call is automatically occurring, that is, the drivermanager.registerdriver () method is called automatically, of course
We can also directly call DriverManager.RegisterDriver () to register drivers, but in my experience.
The applet cannot succeed when calling this method in the browsing of MS, that is, the JVM built in the browser is the method.
Implementation is invalid.
In addition, we can also use the system properties JDBC.DRIVERS to load multiple drivers:
System.SetProperty ("JDBC.DRIVERS", DRIVER1: DRIVER2: .....: drivern "); multiple drivers
Use ":" separated, this JDBC is searched in order until the first one can successfully connect the specified URL driver.
program.
In the basic article, we will not introduce these advanced features of DataSource.
After successful registration of the driver, we can use DriverManager's static method getConnection.
Reference to and database links:
Connection conn = drivermanager.getConnection (URL);
If the link is successful, return the Connection object conn, if you are null or throw an exception,
Establish a connection with the database.
There are three overloaded methods for the getConnection () method, one is the simplest to give the data source:
GetConnection (URL), the other is to give some data source information, which is GetConnection (URL, Properties),
The other is to give data sources, usernames, and passwords: GetConnection (URL, User, Passwod), for data source information.
If we want to give more information while connecting, you can press this information into a Properties, of course, you can directly press
Enter the username password, you can also press some other information such as specified character sets, encoding methods, or default operations.
After getting a link, there is also a channel with the database to find the channel. We can do what we want.
.
Still first introduce some general operations:
If we want to operate on the table in the database, you have to bind a statement at once:
Statement Stmt = conn.createstatement ();
Then use this statement to perform the operation. At the fundamental operation, there can be two results returned, if the execution query
Operation, return to the result set RESULTSET, if an update operation is performed, the record number INT is returned.
Note that SQL operation is strictly divided by only two, one is reading operation (query operation), the other is write operation (more
New operations), so CREATE, INSERT, UPDATE, DROP, DELETE, etc. Operation for data to rewrite behavior is an update operation.
ResultSet RS = Stmt.executeQuery ("Select * from table where xxxxx");
INT x = stmt.executeUpdate ("delete from table where ...);
If you are hard to perform an update operation with ExecuteQuery, don't give it to a handle,
Of course, a little experienced programmer will not do this.
As for the processing of the result set, we are discussed in the next section, because it is an optional option, only query operations
The result set is returned. For the completion of the action process, a very necessary step is to close the database link, you are not
To solve more JDBC knowledge, you first take this step as the most important step in JDBC operation, in the later introduction, I will
Constantly remind you to close the database link !!!!!!!!!!!
According to the steps described above, a completed example is this: (Note, in order to press the steps, this example
The child is not the best)
Try {
Class.Forname ("org.gjt.mm.mysql.driver");} catch (exception e) {
System.out.println ("No successful loading driver:" E.TOSTRING ());
Return;
} // For experience like me, you can judge an exception reason directly from the simple words of e.tostring ().
// If you are a novice, you should choose the subclass of it, how do you know which exception to capture? A simple
// method is to first do not add TRY {}, directly class.Forname ("Org.gjt.mm.mysql.driver);
// The translator will tell you what exception is you caught, of course, this is a method of stealing, it is best to
// Go see the JDK document, which will tell you what exceptions must be captured.
Connection conn = NULL;
Try {
Conn = drivermanager.getConnection
"JDBC: MySQL: // Host: 3306 / MySQL",
"User",
"passwd");
Statement Stmt = conn.createstatement ();
ResultSet RS = Stmt.executeQuery ("SELECT * from Table");
// RS processing
[rs.close ();]
[stmt.close ();]
}
Catch (Exception E) {
System.out.println ("Database operation is exception:" E.TOSTRING ());
}
Finally {
Try {conn.close ();} catch (exception) {}
} // No matter what you have learned about how the database process is operated, if you believe me, start now,
/ / Please write the code close to the FINALLY block, cut!