I believe that through the analysis, reading and understanding of the articles in front of Xiaobian, readers can have a certain content understanding. Let's have a more in-depth understanding of JDBC tools together with Xiaobian!
catalogue
Encapsulation of JDBC tool class
Why are methods in tool classes generally privatized (!!!)?
Encapsulate the driver (use the configuration file to obtain the driver dynamically)
Encapsulating database operation objects
Encapsulation of JDBC tool class
Why encapsulate tool classes?
In order to facilitate the later development, we need to encapsulate a tool class.
Encapsulated tool class (in the tool class with no end of the same program, the tool class is loaded only once!!!)
Preparatory work: create a utils (name can be changed, but it is better to be meaningful) folder under the created jdbc folder, and create a tool class of JdbcUtil
With the tool class ready, you can start coding
Why are methods in tool classes generally privatized (!!!)?
The privatization of methods in tool classes is to prevent new objects in tool classes, because the methods of tool classes are static, do not need new objects, and use class names directly Mode call (as shown in Figure Arrays tool class).
Encapsulate the driver (use the configuration file to obtain the driver dynamically)
package com.luosf.jdbc.utils; import java.sql.*; import java.util.ResourceBundle; /** * This is the tool class of Jdbc * */ public class JdbcUtil { //Suppresses default constructor, ensuring non-instantiability. //Suppress the default constructor to ensure non instantiation. private JdbcUtil(){ } //Use static variables, execute when the class is loaded, and precede the static code block (because the static code block needs to be used) //Use static variables to implement dynamic registration driver private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db"); //Use static code blocks: "ensure that the registration driver is executed when the class is loaded and only once" //Register driver static{ try { Class.forName(bundle.getString("driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
Encapsulating database operation objects
package com.luosf.jdbc.utils; import java.sql.*; import java.util.ResourceBundle; /** * This is the tool class of Jdbc * */ public class JdbcUtil { //Suppresses default constructor, ensuring non-instantiability. //Suppress the default constructor to ensure non instantiation. private JdbcUtil(){ } //Use static variables, execute when the class is loaded, and precede the static code block (because the static code block needs to be used) //Use static variables to implement dynamic registration driver private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db"); //Get the database link object and return a new link object (generally, if there are errors outside, all errors will be thrown directly) public static Connection getConnection() throws SQLException { //Get the required content from the configuration file String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); //Get database operation object Connection conn = DriverManager.getConnection(url,user,password); //Return database operation object return conn; } }
Encapsulate and release resources (other steps can also be encapsulated, depending on your needs. Generally, encapsulate the above three steps)
package com.luosf.jdbc.utils; import java.sql.*; import java.util.ResourceBundle; /** * This is the tool class of Jdbc * */ public class JdbcUtil { //Suppresses default constructor, ensuring non-instantiability. //Suppress the default constructor to ensure non instantiation. private JdbcUtil(){ } //Release resources //When you do not need to process the query result set, you can enter close(conn, stat, null) in this way public static void close(Connection conn, Statement stat, ResultSet res){ if (res != null){ try { res.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat != null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
Complete JDB tool class code
package com.luosf.jdbc.utils; import java.sql.*; import java.util.ResourceBundle; /** * This is the tool class of Jdbc * */ public class JdbcUtil { //Suppresses default constructor, ensuring non-instantiability. //Suppress the default constructor to ensure non instantiation. private JdbcUtil(){ } //Use static variables, execute when the class is loaded, and precede the static code block (because the static code block needs to be used) //Use static variables to implement dynamic registration driver private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db"); //Use static code blocks: "ensure that the registration driver is executed when the class is loaded and only once" //Register driver static{ try { Class.forName(bundle.getString("driver")); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //Get the database link object and return a new link object (generally, if there are errors outside, all errors will be thrown directly) public static Connection getConnection() throws SQLException { //Get the required content from the configuration file String url = bundle.getString("url"); String user = bundle.getString("user"); String password = bundle.getString("password"); //Get database operation object Connection conn = DriverManager.getConnection(url,user,password); //Return database operation object return conn; } //Release resources //When you do not need to process the query result set, you can enter close(conn, stat, null) in this way public static void close(Connection conn, Statement stat, ResultSet res){ if (res != null){ try { res.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (stat != null){ try { stat.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if (conn != null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
Test of JDBC tool class
Now that the tool class has been encapsulated, you can start to use and test whether the code can run normally!
package com.luosf.jdbc; import com.luosf.jdbc.utils.JdbcUtil; import java.sql.*; /** * JDBC Test class of tool class */ public class JdbcUtilTest { public static void main(String[] args) { Connection conn = null; PreparedStatement stat = null; ResultSet res = null; try { //1. The driver has been registered when the class is loaded //2. Get links conn = JdbcUtil.getConnection(); //sql instruction String sql = "select * from t_shuihuo where id < ? "; //3. Compile SQL statements stat = conn.prepareStatement(sql); //Value placeholders //JDBC subscripts start with 1 stat.setInt(1,16); //1 represents the first question mark System.out.println(sql); //4. Execute sql res = stat.executeQuery(); //5. Processing query result set while (res.next()){ int id = res.getInt("id"); String name = res.getString("name"); String nickname = res.getString("nickname"); System.out.println("id :"+ id + " name :" +name + " Nickname?"+nickname); } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { //6. Release resources JdbcUtil.close(conn,stat,res); //null is passed in when there is no query result set // JdbcUtil.close(conn,stat,null); } } }