您的位置:首页 > 编程语言 > Java开发

使用Spring搭建数据库连接驱动,测试是否成功

2017-11-30 20:16 519 查看
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.用到的jar
sqljdbc4
3.基础类信息
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();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐