Here is an original eco-JDBC syntax
Public Static voidMain (string[] args) {Connection Connection=NULL; PreparedStatement PreparedStatement=NULL; ResultSet ResultSet=NULL; Try { //1. Load Database driverClass.forName ("Com.mysql.jdbc.Driver"); //2. Get the database link through the Drive management classConnection = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mybatis?characterencoding=utf-8", "Root", " Root); //3. Define SQL statements? represents placeholdersString sql = "SELECT * from user where username =?"; //4. Get preprocessing statementPreparedStatement =connection.preparestatement (SQL); //5, set the parameter, the first parameter is the ordinal of the parameter in the SQL statement (starting from 1), the second parameter is the set parameter valuePreparedstatement.setstring (1, "Harry"); //6. Issuing SQL execution query to the database, querying the result setResultSet =Preparedstatement.executequery (); //7. Traverse Query Result set while(Resultset.next ()) {User User System.out.println (resultset.getstring ("id") + "" +resultset.getstring ("username"))); } } Catch(Exception e) {e.printstacktrace (); }finally{ //8. Releasing Resources if(resultset!=NULL){ Try{resultset.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(preparedstatement!=NULL){ Try{preparedstatement.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(connection!=NULL){ Try{connection.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } } }
Summary of issues that arise:
1. In code 1, it can be seen that there is a hard code when creating a connection. (To be resolved by configuration)
2. There is a hard code when executing statement. (To be resolved by configuration)
3. Frequent opening and closing of database connections can result in degraded database performance.
Database connection pool (global configuration file)
The original ecological JDBC and the problem