Project training record - execute and update in JdbcTemplate


1, What have you done recently?

2, Introduction, problems and Solutions

1. what is a JdbcTemplate

2. Preparation of jdbctemplate

3. Use of jdbctemplate - execute and update methods

4. Differences between execute and update methods (Reference)

5. finally, how to solve the problem that execute cannot transfer parameters?

1, What have you done recently?

Since the beginning of May, it has mainly improved the functions of the administrator side and fixed the bug s on the user side.

In the previous work, when the functions of creating a new database and a new data table were implemented, the created tables had no entities, but stored the schema information of the tables in the corresponding system data tables, and did not really create a real database in MySQL or a real table in the corresponding database.

Therefore, it is necessary to improve the back-end method of the system.

Before, the system framework directly used mybatis for database processing, but the data source has been configured, and the new database cannot be dynamically configured into the project, so the project uses JDBC for database operation.

In the part I am responsible for, I use the JdbcTemplate. Let's briefly introduce the JdbcTemplate, and then talk about some problems and solutions I encountered when using it

2, Introduction, problems and Solutions

1. what is a JdbcTemplate

The Spring framework encapsulates JDBC and uses the JdbcTemplate to facilitate database operations

2. Preparation of jdbctemplate

Import jar package


Full xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <!-- Database connection pool -->
    <bean id="dataSource" class=""
        <property name="url" value="jdbc:mysql:///user_ DB "/ > <! -- corresponding to the database in SQLyog -- >
        <property name="username" value="root" />            <!-- user name -->
        <property name="password" value="4.233928" />        <!-- password -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <!-- JdbcTemplate object -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--injection dataSource attribute-->
        <property name="dataSource" ref="dataSource"></property>
    <!-- Component scan -->
    <context:component-scan base-package="JDBC"></context:component-scan>

3. Use of jdbctemplate - execute and update methods

There are three types of methods for executing SQL statements in JdbcTemplate:

  1. Execute: all SQL statements can be executed. It is generally used to execute DDL statements.
  2. Update: used to execute DML statements such as INSERT, update, DELETE, etc.
  3. Query: used for DQL data query statements such as SELECT.

In my own use, I mainly use the execute and update methods. As a result, I found that an error occurred when I used the execute method to delete the database table. The reason is that the execute method has no way to transfer parameters, which makes it impossible to delete the table referred to by the table name passed through the front end.

4. Differences between execute and update methods( reference resources)

  • update can take parameters, but execute cannot. For example:
jdbcTemplate.update("update TableA set name = 'Andy' where id=?", new Object[] {new Integer(3)}); 

jdbcTemplate.update("insert into sys_person(person_id, person_name, gender) values(?,?,?)",new Object[]{"eeee","eeee","eeee"});

jdbcTemplate.execute("update TableA set name = 'Andy' where id=3"); 

  • Behind update, java sql. Preparedstatement is completed, and execute is based on java sql. Statement.
  • update returns int, that is, the number of rows affected. execute returns void

5. finally, how to solve the problem that execute cannot transfer parameters?

Directly use JDBC statements instead of JdbcTemplate

The codes are as follows:

public int specificDelete(int id,int DBId)throws Exception{
        DB db=dbMapper.findDBById(DBId);
        String dbName=db.getDBEN();
        Chart chart=chartMapper.findChartById(id,DBId);
        String chartName= chart.getEN();

        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://"+dbName;
        String user = "root";
        String password = "xxxxx";

        Connection connection=null;
            connection = DriverManager.getConnection(url, user, password);
            String sql="drop table "+chartName;
            PreparedStatement statement = connection.prepareStatement(sql);
            int resultSet = statement.executeUpdate();
        }catch (ClassNotFoundException e){
        }catch (SQLException e){


        return 1;

Tags: Database Spring Boot Vue

Posted by deft on Sat, 04 Jun 2022 03:54:19 +0530