Query Database Connectivity (Statement), the whole process.

public void executeQuery() throws Exception{

        //1.数据库四要素
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");

        //2.用Properties,key返回value值
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String driver = properties.getProperty("jdbc.drivers");
        String url = properties.getProperty("jdbc.url");
        String user = properties.getProperty("jdbc.user");
        String password Properties.getProperty = ( "jdbc.password" ); 

        // 3. for generating object of a class of mechanisms used Class.forName reflected 
        Class.forName (Driver); 

        // 4. connection with DriverManager Driver 
        Connection Connection = DriverManager. the getConnection (URL, User, password);
 //         System.out.println (connection); 

        // 5. the method in connection with the Statement class, preparing a query 
        Statement Statement = Connection.createStatement (); 

        // 6. the query with executeQuery SQL 
        String SQL = "t_user from the SELECT *" ; 
        ResultSet resultSet = Statement.executeQuery (SQL); 

        // 6. because there ResultSet Boolean execution while a cursor in order to query the database so the use of next () of
        // 7. List in the traversal results in 
        List <the User> Users = new new the ArrayList <> ();
         the while (ResultSet.next ()) { 

            int ID = ResultSet.getInt (. 1 ); 
            String login_name = resultSet.getString ( 2 ); 
            String password1 = resultSet.getString (. 3 ); 
            String USER_NAME = resultSet.getString (. 4 ); 
            
            // 8. The Create a User object, in order to add to the List 
            User user1 = new new User (ID, login_name, password1, USER_NAME ); 
            users.add (user1); 
            // 9. The printing result 
            System.out.println (user1); 
        } 


    }

 

Guess you like

Origin www.cnblogs.com/violetff/p/12533856.html