spring-使用配置文件完成JdbcTemplate操作數據庫

一、創建spring項目
?? ?項目名稱:spring101302
二、在項目上添加jar包
?? ?1.在項目中創建lib目錄
?? ??? ?/lib
?? ?2.在lib目錄下添加spring支持
?? ??? ?commons-logging.jar
?? ??? ?junit-4.10.jar
?? ??? ?log4j.jar
?? ??? ?mysql-connector-java-5.1.18-bin.jar
?? ??? ?spring-beans-3.2.0.RELEASE.jar
?? ??? ?spring-context-3.2.0.RELEASE.jar
?? ??? ?spring-core-3.2.0.RELEASE.jar
?? ??? ?spring-expression-3.2.0.RELEASE.jar
?? ??? ?spring-jdbc-3.2.0.RELEASE.jar
?? ??? ?spring-tx-3.2.0.RELEASE.jar
三、在項目中添加配置文件
?? ?1.在項目中創建conf目錄
?? ?2.在conf目錄下添加spring核心配置文件
?? ??? ?配置文件名稱: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"
?? ??? ??????? xmlns:context="http://www.springframework.org/schema/context"
?? ??? ??????? 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">
?? ??? ?
?? ??? ?<!-- 1.配置數據庫連接池 -->
?? ??? ?<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
?? ??? ??? ?<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
?? ??? ??? ?<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
?? ??? ??? ?<property name="username" value="root"></property>
?? ??? ??? ?<property name="password" value="root"></property>
?? ??? ?</bean>
?? ??? ?
?? ??? ?<!-- 2.配置JdbcTemplate -->
?? ??? ?<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
?? ??? ??? ?<!-- 給屬性注入值 -->
?? ??? ??? ?<property name="dataSource" ref="dataSource"></property>
?? ??? ?</bean>
</beans>
四、測試
?? ?1.在項目上創建test目錄
?? ??? ?/test
?? ?2.在test目錄下創建測試包
?? ??? ?包名:cn.jbit.spring101301.test
?? ?3.在測試包下創建測試類
?? ??? ?測試類名:JdbcTemplateDemo.java
?? ??? ?測試類的內容:
?? ??? ?public class JdbcTemplateDemo {
?? ??? ??? ?/**
?? ??? ??? ? * 使用spring jdbctemplate添加數據
?? ??? ??? ? */
?? ??? ??? ?@Test
?? ??? ??? ?public void testJdbcTemplate(){
?? ??? ??? ??? ?ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
?? ??? ??? ??? ?JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbctemplate");
?? ??? ??? ??? ?String sql = "INSERT INTO temp(tid,tname) VALUES(1,'zhangsan')";
?? ??? ??? ??? ?jdbcTemplate.execute(sql);
?? ??? ??? ?}

?? ??? ?}

本文轉自 ?素顏豬 ?51CTO博客,原文鏈接:http://blog.51cto.com/suyanzhu/1563218


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279359.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279359.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279359.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

瓦片經緯度及行列號轉換_Slippy map tilenames(瓦片和經緯度換算)

Slippy map tilenames(瓦片和經緯度換算)This article describes the file naming conventions for theSlippy Map application.Tiles are 256 256 pixel PNG filesEachzoom level is a directory, each column is a subdirectory, andeach tile in that column is a fileFilen…

在Windows 7或Vista(或Windows 8.x,Sorta)上禁用Aero

The Windows Aero Glass interface for Windows 7 or Vista requires a decent video card, you won’t be able to use it on an old clunker computer. For those worried about performance, sometimes squeezing every last drop requires disabling Aero. Windows 7或Vist…

一個簡單的JDBC通用工具

支持多種數據庫&#xff0c;統一方式產生連接&#xff0c;最優化、最簡單方式釋放資源。歡迎拍磚&#xff01;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.sql.*; import java.util.List; import java.util.Properties…

sfm點云代碼_VisualSFM使用方法與心得

關于VisualSfM的更多內容組合多個模型(What if VisualSFM produces multiple models?)&#xff1a;按照上述步驟進行稀疏重建后&#xff0c;理論上可以得到很好的模型。如果結果產生了多個模型&#xff0c;要想把多個模型合成成一個&#xff0c;點擊菜單中的“SfM->More Fu…

macos mojave_使Ubuntu看起來像macOS Mojave的黑暗模式

macos mojaveIf you’re a Linux user who likes the look of the dark mode coming in macOS Mojave, you’re in luck: there’s a GTK theme just for you. 如果您是Linux用戶&#xff0c;并且喜歡macOS Mojave中的黑暗模式外觀&#xff0c;那么您很幸運&#xff1a;這里有一…

html的列表標簽

列表一般應用在布局中的新聞標題列表和文章標題列表以及網頁菜單等。 例如這個就是一個列表&#xff1a; 列表標簽有幾種&#xff0c;分別是有序列表&#xff0c;無序列表&#xff0c;定義列表。 有序列表<!DOCTYPE html> <html lang"en"> <head>&…

撬鎖錘怎么用_安全錘是啥?消防蜀黍教你怎么選?如何快速破拆逃生?

逃生錘又叫安全錘&#xff0c;生活中很多地方都可以看到&#xff0c;公交車、地鐵窗邊都少不了它們的身影它的款式也是五花八門&#xff0c;那么問題來了當遇到突發狀況被困車內時&#xff0c;哪種破窗工具最有效&#xff1f;又該如何快速逃生自救&#xff1f;近日&#xff0c;…

WSUS技術概覽

WSUS新功能展示: 支持更多微軟產品更新-->Windows Office MS SQL Server Exchange ......基于產品及分類篩選下載更新的能力更多語言支持定位更新目標計算機或計算機組的能力-->分發前,測試更新; 保護運行特定應用程序的計算機; 靈活使用Deadline; ...... 見下…

Java基礎學習總結(16)——Java制作證書的工具keytool用法總結

2019獨角獸企業重金招聘Python工程師標準>>> 一、keytool的概念 keytool 是個密鑰和證書管理工具。它使用戶能夠管理自己的公鑰/私鑰對及相關證書&#xff0c;用于&#xff08;通過數字簽名&#xff09;自我認證&#xff08;用戶向別的用戶/服務認證自己&#xff09…

什么是文件擴展名?

A file extension, or filename extension, is a suffix at the end of a computer file. It comes after the period, and is usually two-four characters long. If you’ve ever opened a document or viewed a picture, you’ve probably noticed these letters at the end…

變量與常量

什么是變量/常量&#xff1f; 變量是計算機內存中的一塊區域&#xff0c;變量可以存儲規定范圍內的值&#xff0c;而且值可以改變。基于變量的數據類型&#xff0c;解釋器會分配指定內存&#xff0c;并決定什么數據可以被存儲在內存中。常量是一塊只讀的內存區域&#xff0c;常…

python藍牙編程_藍牙編程經典程序!

文檔從網絡中收集&#xff0c;已重新整理排版.word版本可編輯.歡迎下載支持.1word版本可編輯.歡迎下載支持.L2CAP socketsExample 4-4. l2cap-server.c#include #include #include #include #include int main(int argc, char **argv){struct sockaddr_l2 loc_addr { 0 }, rem…

[項目總結]在ios中使用soundtouch庫實現變聲

這篇文章是項目總結了。 做了段時間的項目&#xff0c;過程中也遇到了很多麻煩&#xff0c;但是好在終于都解決了&#xff0c;這里是這里是項目之后憑著記憶總結出來&#xff0c;大家有遇到同樣的問題&#xff0c;希望能參考了&#xff0c;但是我記憶可能不太好了&#xff0c;要…

Myeclipse優化配置

2019獨角獸企業重金招聘Python工程師標準>>> 作為企業級開發最流行的工具&#xff0c;用Myeclipse開發java web程序無疑是最合適的&#xff0c;java web前端采用jsp來顯示&#xff0c;myeclipse默認打開jsp的視圖有卡頓的現象&#xff0c;那么如何更改jsp默認的打開…

Java多線程之靜態代理

1 package org.study2.javabase.ThreadsDemo.staticproxy;2 3 /**4 * Date:2018-09-18 靜態代理 設計模式5 * 1、真實角色6 * 2、代理角色&#xff1a;持有真實角色的引用7 * 3、二者實現相同的接口8 * 舉例說明&#xff1a;Couple類和Company類都實現了Marry&#xff0c;…

Google在Android P中隱藏了真棒的按應用自動旋轉功能

Historically, when you turn your phone on its side, the screen rotates. To keep this from happening, you can lock the orientation. But with Android P, Google included a way to have to the best of both worlds. 從歷史上看&#xff0c;當您將手機側放時&#xff…

python 位置參數、默認參數、可變參數位置關系_python的位置參數、默認參數、關鍵字參數、可變參數區別...

一、位置參數調用函數時根據函數定義的參數位置來傳遞參數#!/usr/bin/env python#codingutf-8defprint_hello(name, sex):sex_dict {1: u先生, 2: u女士}print hello %s %s, welcome to python world! %(name, sex_dict.get(sex, u先生))#兩個參數的順序必須一一對應&#xff0…

2015年終總結

2019獨角獸企業重金招聘Python工程師標準>>> 2015年終總結 用勇氣改變可以改變的事情&#xff0c;用胸懷接受不可以改變的事情&#xff0c;然后用智慧分辨兩者的不同&#xff01; 短信平臺sms 影像系統fastfile 統一信任中心uts(單點登錄&#xff09; 簡歷增加 總結…

筆記本本地連接顯示電纜拔出_沒有安全電纜槽的筆記本電腦如何固定?

筆記本本地連接顯示電纜拔出Historically laptops included a slot in the side for attaching security cables–as seen in the photo here–but increasingly more slender laptops like ultrabooks are omitting the lock-slot from their case design. How do you properl…

JMeter中添加dubbo相關插件異常問題解決

從網上下載了一個dubbo的插件&#xff0c;然后放到JMeter的/lib/ext目錄下&#xff1a; 然后啟動直接異常 發現啟動不了&#xff0c;然后下載了一個全新的JMeter3.2將dubbo插件放到同樣的目錄&#xff0c;啟動&#xff0c;沒有問題&#xff1a; 那應該不是JMeter本身的問題 通過…