技术标签: java Spring企业级程序设计练习 mybatis mysql jdbc 数据库
使用Properties类型注入方式,注入MySQL数据库连接的基本信息,然后使用JDBC方式连接数据库,模拟执行业务代码后释放资源,最后在控制台输出打印结果。
要求如下:
引入MySQL驱动jar包。
在com.mhys.demo.pojo包下创建DataSource类,添加Properties类型属性。
package com.mhys.demo.pojo;
import java.util.Map;
import java.util.Properties;
public class DataSource {
private Map<String, String> map;
private Properties properties;
@Override
public String toString() {
return "DataSource [map=" + map + "]";
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
在applicationContext.xml配置文件中注册DataSource类到容器。
<bean id="dataSource" class="com.mhys.demo.pojo.DataSource">
<property name="Properties">
<props>
<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://locahost:3306/goods</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
在com.mhys.demo.service包下创建JdbcService类,添加DataSource类型属性,声明getConnection()方法和close()方法。
package com.zn.mhys.demo.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mhys.demo.pojo.DataSource;
public class JdbcService {
private DataSource dataSource;
public Connection getConnection(){
Connection conn = null;
String url = (String) dataSource.getProperties().get("url");
String username = (String) dataSource.getProperties().get("username");
String password = (String) dataSource.getProperties().get("password");
try {
Class.forName(dataSource.getProperties().getProperty("driverClassName"));
conn = (Connection)DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void close(Connection conn){
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("连接释放成功!");
}
}
}
在applicationContext.xml配置文件中注册JdbcService类到容器。
<bean id="jdbcService" class="com.zn.mhys.demo.service.JdbcService">
<property name="dataSource" ref="dataSource"></property>
</bean>
在com.mhys.demo.test包下创建Test测试类。
package com.zn.mhys.demo.test;
import java.sql.Connection;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.zn.mhys.demo.service.JdbcService;
public class Test {
public static void main(String[] args) {
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
XmlBeanFactory context = new XmlBeanFactory(resource);
// 2.2.5
JdbcService jdbcService = (JdbcService) context.getBean("jdbcService");
Connection connection = jdbcService.getConnection();
if (connection!=null) {
System.out.println("获取数据库连接成功!");
}
System.out.println("义务代码执行成功!");
jdbcService.close(connection);
}
}
文章浏览阅读2.2k次。原因很简单,就是客户端的aidl文件与远程service的所在包的包名不同只需在客户端工程中重建一个与远程service包名一致的包,然后把aidl文件复制进去即可。_binder invocation to an incorrect interface
文章浏览阅读1.1k次,点赞2次,收藏12次。单选若某种编码的最小距离为四,则其检错能力和纠错能力可能为___A.检错一位,纠错两位B.检错三位,纠错两位C.检错两位,纠错两位D.检错两位, 纠错一位正确答案:D设由四个模块组成的四体存储器结构,每个体的存储字长为16位,存取周期为250ns,假设数据总线宽度为16位,总线传输周期为50ns,试求顺序存储和交叉存储的带宽分别为___bpsA.6.4107 和 1.6107B.6.4108 和 1.6108C.1.6108 和 6.4107D.6.4107 和 1.6108_(1分) 采用八体并行低位交叉存储器,设每个体的存储容量为32k*16位,存取周期为
文章浏览阅读2.4k次。判断ie/7/8var isIE6 = navigator.userAgent.toLowerCase().indexOf("msie 6") > -1; //IE6//判断ie6/7/8var ua = navigator.userAgent.toLocaleLowerCase();var ie = 0;if(ua.indexOf('ie 6') > -1 || ua.ind_js助手函数
文章浏览阅读1.9k次。Xposed是一个针对Android Framework的hook框架, 通过/system/bin/app_process修改Zygote的行为, 从而实现hook应用。Xposed框架安装完整的Xposed框架包含三个部分 详见Xposed InstallerXposed FrameworkXposed Module插件开发通过自定义插件hook目标应..._xp插件开发教程
文章浏览阅读244次。<div id='app' style="display: none;" :style="{display: 'block'}">加上[ style="display: none;" :style="{display: 'block'}" ] 完美解决!解决思路:style只有vue渲染的时候才识别,在此之前style=“display: none;” 让页面不显示。vue渲染后,:style="{display: ‘block’}"有让页面显示出来。..._style="display:none;" :style="{display: 'block'}
文章浏览阅读2.7w次,点赞11次,收藏76次。来源:德先生(D-Technologies)FITEE“人工智能2.0”专题导读国务院近日印发《新一代人工智能发展规划》,提出了面向2030年我国新一代人工智能发展的指导思想、战略目标、重点任务和保障措施,部署构筑我国人工智能发展的先发优势,加快建设创新型国家和世界科技强国。2017年1-2月,中国工程院院刊信息与电子工程学部分刊《信息与电子工程前沿(英文)》出版了“Artificial Inte_混合增强智能
文章浏览阅读2.1k次。提供了一种异步观察目标元素与其祖先元素或顶级文档视窗(viewport)交叉状态的方法1、生成观察器 可以在同一个观察者对象中配置监听多个目标元素 var intersectionObserver = new IntersectionObserver(function(entries) { 触发的监听回调 entries:所有监听的元素 boundingClientRect 目标元素的矩形信息 intersectionRatio 相交区域和目标元素的比例值 interse_js监听元素进入可视区
文章浏览阅读185次。thinkphp5两种分页方法 use \org\until\Page;public function page(){//法一:thinkphp5自带分页$list = Db::name('article')->paginate(5,true);$page = $list->render();//法二:使用扩展类库分页$count = Db::name..._thinkphp5 分页
文章浏览阅读3k次,点赞2次,收藏10次。#include 〈graphics.h〉#include 〈stdio.h〉#include 〈fcntl.h〉#include 〈io.h〉#include 〈stdlib.h〉#include 〈conio.h〉#define ROW 1 //纵坐标放大倍数#define COL 2 //横坐标放大倍数void main(){int x,y;char *s=″汉字显示程..._c语言怎么输出的汉字放大
文章浏览阅读110次。镜像的定制实际上就是定制每一层所添加的配置、文件。我们可以把每一层修改、安装、构建操作的命令都写入一个脚本,这个脚本就是 Dockerfile。Dockerfile是一个文件,其内容包含了一条条的指令,每一条指令构建一层,因此每条指令的内容,就是描述该层应当如何构建。接下来我们以官方nginx镜像为例,使用Dockerfile来定制。创建目录:mkdir mynginxcd myngin..._docker如何定制镜像
CentOS 7安装node-canvas记录,包括更新gcc版本和安装相关依赖,以及启动命令。
文章浏览阅读1.3k次。SQL2005+ASP.NET 2.0 作为系统的底层数据交换,系统表现层则采用了Ext 3.3构建WEB框架。其他的不多说了先放上系统图片,请大家批评指正 _c# +postgresql图书信息管理系统