目录

一、最近做了什么?

二、介绍、问题和解决

1.什么是JdbcTemplate

2.JdbcTemplate的准备

3.JdbcTemplate的使用——execute和update方法

4.execute和update方法的不同(参考)

5.最后怎么解决execute不能传参的问题呢?

一、最近做了什么?

五月初以来,主要是进行管理员端功能的完善和用户端bug的修复。

在之前的工作中,新建数据库、新建数据表功能实现时,所创建的表并没有实体,只是把表的模式信息存到了相对应的系统数据表中,并没有真正在MySQL中创建真正的数据库和 在相对应数据库创建真实的表。

所以要完善系统后端方法。

系统框架之前直接使用mybatis来进行有关数据库处理,但是数据源已经配置好了,新建的数据库没有办法动态配置到项目中,所以项目使用了JDBC来进行数据库操作。

在本人负责的部分,使用了JdbcTemplate。下面简单介绍一下JdbcTemplate,然后说一下我在使用时遇到的一些问题和解决办法

二、介绍、问题和解决

1.什么是JdbcTemplate

Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作

2.JdbcTemplate的准备

导入jar包

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

完整的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.JdbcTemplate的使用——execute和update方法

JdbcTemplate执行SQL语句有3类方法:

  1. execute:可以执行所有SQL语句,一般用于执行DDL语句。
  2. update:用于执行INSERTUPDATEDELETE等DML语句。
  3. query:用于如SELECT等DQL数据查询语句。

在我自己的使用中,主要用了execute和update方法,结果发现在使用execute方法删除数据库表的时候出错了,原因是execute没有办法传参,导致没有办法删除前端穿过来的表名所指的表。

4.execute和update方法的不同(参考

  • update可以带参数,而execute不可以。例如:
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背后是借助于java.sql.PreparedStatement完成,而execute是基于java.sql.Statement。
  • update返回int, 即受影响的行数。execute返回void

5.最后怎么解决execute不能传参的问题呢?

直接使用JDBC语句,不使用JdbcTemplate了

代码如下:

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;
    }

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐