Project training record (5) - execute and update in JdbcTemplate

Table of contents

1. What have you done recently?

2. Introduction, problems and solutions

1. What is JdbcTemplate

2. Preparation of JdbcTemplate

3. Use of JdbcTemplate - execute and update methods

4. The difference between execute and update methods (reference)

5. Finally, how to solve the problem that execute cannot pass parameters?

1. What have you done recently?

Since the beginning of May, it has mainly been improving the functions of the administrator side and fixing the bugs of the user side.

In the previous work, when the functions of creating a new database and creating a new data table were implemented, the created table did not have an entity, but only stored the schema information of the table in the corresponding system data table, and did not actually create a real database in MySQL. And create a real table in the corresponding database.

So it is necessary to improve the system back-end method.

The system framework used mybatis directly for database processing before, but the data source has been configured, and the newly created database cannot be dynamically configured into the project, so the project uses JDBC for database operations.

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

2. Introduction, problems and solutions

1. What is JdbcTemplate

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

2. Preparation of JdbcTemplate

import jar package

spring-beans-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
spring-jdbc-4.1.2.RELEASE.jar
spring-tx-4.1.2.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar

complete xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 
    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql:///user_db" /><!--对应SQLyog里的数据库-->
        <property name="username" value="root" />            <!-- 用户名 -->
        <property name="password" value="4.233928" />        <!-- 密码 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>
 
    <!-- JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource属性-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
 
    <!-- 组件扫描 -->
    <context:component-scan base-package="JDBC"></context:component-scan>
 
</beans>

3. Use of JdbcTemplate - execute and update methods

JdbcTemplate has three types of methods for executing SQL statements:

  1. execute: Can execute all SQL statements, generally used to execute DDL statements.
  2. update: Used to execute DML statements such as INSERT, UPDATE, and so on.DELETE
  3. query: Used for such SELECTDQL data query statements.

In my own use, I mainly used the execute and update methods. It turned out that I made an error when using the execute method to delete the database table. The reason was that execute had no way to pass parameters , so there was no way to delete the table name that the front end passed through. surface.

4. The difference between execute and update methods ( reference )

  • 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"); 

  • Update is done with the help of java.sql.PreparedStatement, and execute is based on java.sql.Statement.
  • update returns an int, the number of rows affected. execute returns void

5. Finally, how to solve the problem that execute cannot pass parameters?

Use JDBC statement directly instead of JdbcTemplate

code show as below:

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

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

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

        connection.close();

        return 1;
    }

Guess you like

Origin blog.csdn.net/pinkray_c/article/details/125110473