使用Spring搭建数据库连接驱动,测试是否成功
来源:互联网 发布:php 静态方法 编辑:程序博客网 时间:2024/06/07 14:54
1.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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 驱动类--> <bean name="sqlconndb" class="model.SqlConnDb"> <!-- 驱动名称--> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <!-- 数据库地址--> <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=x6"></property> <!-- 数据库用户--> <property name="username" value="sa"></property> <!-- 数据库密码--> <property name="password" value="root123"></property> </bean></beans>2.用到的jarsqljdbc43.基础类信息3.1package model;public class SqlConnDb {private String driverClassName;// 数据库连接驱动;private String url;// 数据库连接地址;private String username;// 数据库用户;private String password;// 数据库密码;public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}3.2package dao;import java.sql.DriverManager;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.sql.*;import model.SqlConnDb;public class Testdao {public void testconn() {ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });SqlConnDb connDb = (SqlConnDb) context.getBean("sqlconndb");try {Class.forName(connDb.getDriverClassName());Connection Conn = DriverManager.getConnection(connDb.getUrl(), connDb.getUsername(), connDb.getPassword());System.out.println("连接数据库成功!!");} catch (Exception e) {System.out.print("连接失败!!");}}}3.3package service.impl;import dao.Testdao;public class TestSqljdbc extends Testdao{public static void main(String[] args) {TestSqljdbc ts =new TestSqljdbc();ts.testconn();}}
阅读全文
0 0
- 使用Spring搭建数据库连接驱动,测试是否成功
- C#测试数据库连接是否成功
- C#测试数据库连接是否成功
- 测试集群是否搭建成功
- 测试Theano使用GPU并行计算,以验证环境搭建是否成功
- 测试php 环境是否搭建成功的测试脚本
- spring声明式事务配置是否成功测试方法
- 测试spring是否注入成功两种方法
- spring + ehcache 使用实例 我测试成功
- memcached测试是否成功
- led驱动测试成功
- Spring 测试数据库连接
- 使用VMware测试U盘启动盘是否制作成功
- 如何使用php文件测试mongodb是否安装成功
- 使用ping命令快速测试MTU设置是否成功
- 使用Spring进行测试驱动开发
- 测试开通博客是否成功
- 测试mysql 是否连接成功
- 项目中的sql语句涉及到取集合时(即in(...))的用法foreach
- E
- Java HashMap提高性能和原理
- document.ready和window.onload的区别
- java group by 和 join
- 使用Spring搭建数据库连接驱动,测试是否成功
- 冒泡排序
- Mean Vector and Covariance Matrix
- curl的使用
- 使用JMX监控Kafka
- 【学习笔记】设计模式-组合模式
- springMVC接收日期类型参数的自定义转换
- Python 大战 Java
- 7 主成成分分析(PCA)降维算法