当前位置:网站首页>[three tier architecture and JDBC summary]

[three tier architecture and JDBC summary]

2022-07-05 08:21:00 Sing the wind tonight

I'm learning JDBC In the process of , Note that the tool class must correspond to the query field , Even if the query method is general , But when facing different tables, you should also pay attention to whether the declared attributes of the tool class correspond to the attributes used in the query statement , Otherwise, the object information required by the query statement cannot be queried , There will be no correct result .

Another key point is that there may be differences in the character set of the table , In order to query, add, delete and modify data normally , To be with now properties Add a statement description about the character set in the configuration file , As shown below :

url=jdbc:mysql://localhost:3306/homework3?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true

About multiple variable parameters :

In addition to the corresponding tool classes , In order to improve the generality of query methods, variable parameters are introduced 【args……】 To refer to SQL The parameters required by the statement .

When there are variable parameters ,args Equivalent to an array , The underlying system will first determine its length and then create an array of the same length , Then get the parameters and traverse the array

/**
     *  Inquire about 
     * @param sql
     * @param args
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static List<SelectTools> select1(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement pre = null;
        ResultSet resultSet=null;
        try {
            conn = JDBCUtils.getConn1();
            pre = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++){
                pre.setObject(i +1,args[i]);
            }// When there are variable parameters ,args Equivalent to an array ,
            //  The underlying system will first determine its length and then create an array of the same length , Then get the parameters and traverse the array .

            resultSet =pre.executeQuery();
//             while (resultSet.next()){
//                 System.out.println(resultSet.getObject(1) + "  " + resultSet.getObject(2)); ;
//             }
            // Query multiple rows of data using iterators while(resultSet.next())
            ResultSetMetaData rsmd=resultSet.getMetaData();
//            System.out.println(rsmd);
            int columnCount=rsmd.getColumnCount();
            List<SelectTools> list = new ArrayList<>();
            while (resultSet.next()){

                SelectTools se=new SelectTools();
                for (int i = 0; i <columnCount; i++) {
                    Object columnValue= resultSet.getObject(i+1);
                    String columnName=rsmd.getColumnLabel(i+1);
                    Field file= SelectTools.class.getDeclaredField(columnName);
                    file.setAccessible(true);
                    file.set(se,columnValue);
                }
                list.add(se);

            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource1(conn,pre,resultSet);

        }
        return null;


    }

JDBC Six steps of :

It is roughly divided into six steps , Namely :

① The load driver

② Link to the database

③ Create a through connection SQL Command transmitter Statement( Better avoidance SQL Inject Now use prepareStatement)

④ Use SQL The command sender sends... To the database SQL command .

⑤ Processing results

⑥ close resource

The relationship between the three layers of the structure is as follows :

 

原网站

版权声明
本文为[Sing the wind tonight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050819068098.html