(7)Spring框架——Spring JdbcTemplate

news/2024/7/4 9:23:18

目录

 

一、目录结构

二、定义一个XML,名为:applicationContext.xml。位置放在了src下面。

二、编写测试类,名为:JdbcTemplateTest

三、其他情况


一、目录结构

二、定义一个XML,名为:applicationContext.xml。位置放在了src下面。

<?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.xsd">
<!--    配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--        数据库驱动-->
<!--        property都是通过setter赋值,后面的是要设置的参数-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!--        连接数据库的url  特别提醒:jdbc:mysql//服务器地址/数据库名-->
<!--       value="jdbc:mysql://localhost/db1" 这个db1要改成你的数据库库名。-->
        <property name="url" value="jdbc:mysql://localhost/db1"></property>
<!--        连接数据库的用户名-->
        <property name="username" value="root"></property>
<!--        连接数据库的密码-->
        <property name="password" value="123456"></property>
    </bean>
<!--    配置JDBC模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--            默认必须使用数据源。-->
        <!--        ref引用上一个bean的对象。-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

二、编写测试类,名为:JdbcTemplateTest

package com.stx.chapter04.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {
    /*public static void main(String[] args) {
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jt =(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
        jt.execute("CREATE table message(id int PRIMARY KEY,name CHAR(20),sex CHAR(4),dress CHAR(80))");
        System.out.println("信息表message创建成功!");
    }*/
//    这是导入的单元测试。Junit4开源框架。
    @Test
    public void mainTest(){/*
        特别提醒,如果是放在src下面的其他包,需要将其整个包名暴露出来。
        例如 String XmlPath = "com\stx\chapter04\jdbc\applicationContext.xml";
        在传递给ClassPathXmlApplicationContext(XmlPath);
*/
        ApplicationContext applicationContext = new
                ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jt =(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
        jt.execute("CREATE table message(id int PRIMARY KEY,name CHAR(20),sex CHAR(4),dress CHAR(80))");
        System.out.println("信息表message创建成功!");
    }
}

三、其他情况

(一)第一个报错。是我自己的数据库名称不对。

第二个报错:该表已经存在了。

(二)运行结果


http://www.niftyadmin.cn/n/4557284.html

相关文章

EwoMail 开源邮件服务器安装教程

安装环境 centos6/7系统&#xff0c;服务器需要干净环境&#xff0c;最好是全新安装的系统。 最低配置要求CPU&#xff1a;1核内存&#xff1a;1G硬盘&#xff1a;40G 检查swap 如果没启动swap&#xff0c;这会导致EwoMail的防病毒组件不能启动&#xff0c;所以在安装前先检查s…

(7.1)Spring框架——Spring JdbcTemplate

目录 一、概述 &#xff08;一&#xff09;、是配置数据库的Bean。 &#xff08;二&#xff09;、配置JDBC模板。 &#xff08;三&#xff09;、配置注入类。 二、步骤 &#xff08;一&#xff09;编写实体类。 &#xff08;二&#xff09;编写接口。 &#xff08;三&a…

求解一道Java面试题。

答案是E 当demo null; 不会马上就回收 因为java垃圾回收机制是根据自身内存情况而定

Materialize快速入门教程

https://materializecss.com/ https://github.com/Dogfalo/materialize http://www.materializecss.cn/ 1&#xff0c;下载 http://materializecss.cn/bin/materialize-v0.97.8.zip <!-- Compiled and minified CSS --> <link rel"stylesheet" href"ht…

(8)Spring框架——TransactionManager事务管理(基于XML方法的声明事务)

目录 一、XML声名事务概述 &#xff08;一&#xff09;PlatformTransactionManager &#xff08;二&#xff09;TransactionDefinition &#xff08;三&#xff09;TransactionStatus 二、举例 &#xff08;一&#xff09;、例子关键点描述 1、要在XML配置文件里面添加命…

BZOJ 2342 [Shoi2011]双倍回文(manacher+堆+set)

题意 N<500000 题解 维护一个set可以用堆来解决。 1 #include<iostream>2 #include<cstring>3 #include<cstdio>4 #include<cmath>5 #include<algorithm>6 #include<set>7 #include<queue>8 using namespace std;9 const int N50…

c.k死了没.

||| over了 很神奇 别去猜测时间能够说明一切问题&#xff0e;

(9)Spring框架——MyBatis的学习

目录 一、概述 &#xff08;一&#xff09;项目准备工作 &#xff08;二&#xff09;项目结构描述 &#xff08;三&#xff09;具体作用概述 二、举例 &#xff08;一&#xff09;步骤 &#xff08;二&#xff09;实例 一、概述 &#xff08;一&#xff09;项目准备工作…