This article lists 4 steps for the JDBC link database for your reference:
JDBC: Java access to the database solution.
Several steps: 1. Load the driver class;
2. Establish a connection with the database;
3. Execute SQL statements
4. Treatment results set
5. Close connection
1. The first step : loading the driver class:
Note: Different databases, different reference string, the connection of Oracle is: Class.Forname ("Oracle.jdbc.driver.oderDriver"); After this step is executed, the program may throw it: ClassNotFoundexception.
a. The driver of the database is not imported into the environment variable
b. The string in class.Forname is incorrect
2. Step 2 : Establish a connection with the database via DriverManager:
Its static method getConnection is used to obtain connection. Usually three parameters need to be passed in
Parameter 1: The address and port of the database (different database string contents are different)
Oracle address: JDBC: Oracle: Thin: @Host: Port: SID
Parameter 2: The user name of the database
Parameter 3: The password of the database to the user name
Connection conn = DriverManager.getConnect
("JDBC: Oracle: Thin: @Host: Port: Oracle", "User", "PSD");
3. Step 3 : Java.sql.Statement executes SQL statements and obtains results
Statement state = conn.createStatement ();
String sql = "/*This is SQL statement*/";
Statement provides different execution methods for different SQL statements:
Res)
* This method is specifically used to execute DQL statements, and the returned resultSet represents the foundation set of the query
int ExecuteupDate (String SQL)
* This method is specifically used to execute DML statements, and the returned numbers indicate how many pieces of data in the table affects the statement.
Boolean Execute (String SQL)
* This method can be executed in any theory, but because DQL and DML have special methods to execute, this method is usually used to execute the DDL statement
ResultSet RS = state.executequry (SQL);
Output query results: While (rs.next ()))
{Output statement}
ResultSet provides a method for traversing results:
boolean next ()
*This method has two functions. First of all, when we query the pointer of the RS pointer to the first data after the result set, we need to call the nEXT () to move it to the first data and indicate the article and indicate the article. data.
The second role is to see the return value. If the pointer moves down, it is found that there is no data, and it will return to False. If there is any, it will return True, so we only get the corresponding fields of the current records when the method returns True to return True. The value RS also provides several Getxxx (String Fieldname) methods::
*This series of methods is used to obtain the value corresponding to the given field in the current record of RS. Different fields need to call the corresponding method due to different types.
Step 4 : Close the connection and write it in the Finally block
finally {if (conn! = null) {try {conn.close ();} Catch (sqlexception e) {e.printstacktrace ();}}}
Place the connection of the database in a tool class to achieve the reuse effect
Because access databases are often used operations, in the project, usually write a tool class to access the database. After that, all the operations of the database are obtained from the tool class to achieve two ways of the tool class:
1. Cross the data directly in the tool class dbutil
2. Write the database configuration in a property property file. The tool class is read into the attribute file and obtains the database parameter one by one (generally used)
If you use the first method, you need to modify the database used in the later stage or modify the host, port, database connection name, password, etc., you need to modify the data in the source code, which is not convenient for system maintenance. The two methods database connection tools DBUTIL.JAVA and the main steps of the connection pool:
Properties prop = New Properties (); Prop.load (New FileInputStream ("Config.properties"); // initialize string drivename = Prop.GetProperty ("Driver Name "); string url = prop.getProperty (" url "); String username = prop.getProperty (" username "); string password = prop.getProperty (" password "); // The maximum number of connection INT MAXACTIVE = Integer.PARSEINT (PROP.GetProperty ("Maxactive");/ /The maximum waiting time int maxwait = Integer.parseint (prop.getProperty ("maxwait"); // Initialize the content of the connection connection pool CP = new basicdatasource (); // RiverClassName ( DriverName); cp.seturl (url); cp.setUsername (username); CP.SETPAS Sword (Password); CP.SetmaxAxactive (maxactive); AIT); Public Static Connection GetConnection () Throws Exception {Return CP. getConnection ();}The above is explained to the steps of the JDBC link database. I hope to help everyone!