mybatis basis _ using JDBC problems encountered

Using JDBC connection to query the data:

import java.sql.*;

public class JDBCDemo {
    /*连接参数常量*/
    private static final String JDBC_URL = "jdbc:mysql:///mybatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8";
    private static final String USER = "root";
    private static final String PWD = "root";

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        The resultSet the ResultSet = null ;
         the try {
             / * load driving * / 
            the Class.forName ( "com.mysql.jdbc.Driver" );
             / * get the connection object * / 
            Connection = the DriverManager.getConnection (JDBC_URL, the USER, the PWD);
             / * SQL * / 
            String SQL = "the SELECT * from the User the WHERE the above mentioned id =?" ;
             / * the incoming SQL * / 
            preparedStatement = Connection.prepareStatement (SQL);
             / * set the SQL parameters * /
            preparedStatement.setInt ( . 1,. 1 );
             / * execute SQL return data * / 
            the resultSet = PreparedStatement.executeQuery ();
             / * traverse the result set * / 
            the while (ResultSet.next ()) {
                System.out.println("id = " + resultSet.getInt("id") + "name = " + resultSet.getString("username"));
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace ();
        } The finally {
             / * Close all connections * / 
            IF (the resultSet! = Null ) {
                 the try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace ();
                }
            }

            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace ();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace ();
                }
            }
        }
    }
}

Summary JDBC use of inconvenience to use four points:

  1, create frequent use, release the connection, resulting in waste of system resources.

  2, SQL is hard-coded in the code which is not conducive to post-maintenance and expansion, coupled too strong.

  3, using a placeholder way to set parameters in the latter part of the maintenance is too much trouble (to modify the SQL conditions, placeholder parameters necessary to modify).

  4, the processing of the results is hard-coded, is not conducive to maintenance of late.

Guess you like

Origin www.cnblogs.com/l48x4264l46/p/10926894.html