2021-09-08 Spring JdbcTemplate的常用方法之execute

execute(String sql):该方法能够完成执行SQL语句的功能。

第一步:在MySql中创建spring数据库

第二步:创建一个chapter04的Web项目,并导入Spring框架所需的5个基础包以及

               MySQL数据库的驱动JAR包: mysql-connector-java-5.1.8-bin.jar

               Spring JDBC的JAR包:spring-jdbc-4.3.6.RELEASE.jar

               Spring事务处理的JAR包:spring-tx-4.3.6.RELEASE.jar

 第三步:在src目录下,创建配置文件applicationContext.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
       <!-- 1、配置数据源 -->
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       
			<!-- 数据库驱动 -->
			<!-- drverClassName:所使用的驱动名称,对应驱动JAR包中的Driver类 -->
       		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
       		
        	<!-- 连接数据库的url -->
        	<!-- url:数据源所在地址 -->
        	<property name="url" value="jdbc:mysql://localhost/spring"></property>
        	
        	<!-- 连接数据库的用户名 -->
        	<!-- username:访问数据库的用户名 -->
        	<property name="username" value="root"></property>
        	
        	<!-- 连接数据库的密码 -->
        	<!-- password:访问数据库的密码 -->
        	<property name="password" value="123456"></property>
        	
        </bean>
        
        <!-- 2、配置JDBC模板 -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        	<!-- 默认必须使用数据源 -->
        	<property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 3、配置注入类 -->
<!--         <bean id="xxx" class="xxx">
        	<property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean> -->
</beans>
		

第四步:在src目录下,创建一个com.itheima.jdbc包,并创建测试类JdbcTemplateText

package com.itheima.jdbc;



import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
	/**
	 * 使用execute()方法创建表
	*/

	
	public static void main(String[] args) {
		//加载配置文件
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取JdbcTemplat实例
		JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
		//使用该实例的execute(String sql)方法执行创建数据表的SQL语句
		jdbcTemplate.execute("create table user01("
				+ "id int primary key auto_increment,"
				+ "username varchar(10),"
				+ "balance double)");
		System.out.println("创建成功");
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值