Spring连接数据库

1.DAO模式:

http://blog.csdn.net/initphp/article/details/8200016


①我建的是javaproject不是web的

②用的是DataSource但是没有用到连接池

需要在有数据库操作的方法的到中定义datasource并在bean.xml中配置DataSource


③用的连接file文件在其中写连接配置便于修改

<bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
        <property name="locations" value="jdbc.properties"/>      
    </bean>  

以上代码是引入外部文件

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName">
   <value>${jdbc.driverClassName}</value>
   </property>
   <property name="url">
   <value>${jdbc.url}</value>
   </property>
   <property name="username">
   <value>${jdbc.username}</value>
   </property>
   <property name="password">
   <value>${jdbc.password}</value>
   </property>
   </bean>

以上代码为取值


正文


package Dao;


public class User {


private String name;
public User() {

}
public User(String name, String password) {
super();
this.name = name;
this.password = password;
}
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}



package Dao;


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;


import javax.sql.DataSource;


public class UserDao implements UserDaoImpl {


private DataSource dataSource;

public DataSource getDataSource() {
return dataSource;
}


public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}


public void insertUser(User user) {
String name=user.getName();
String password=user.getPassword();
Connection conn=null;
Statement stmt=null;


try {
conn=dataSource.getConnection();
stmt=conn.createStatement();


stmt.execute("insert into user(name,password)"+"values('"+name+"','"+password+"')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


}



package Dao;


public interface UserDaoImpl {


public void insertUser(User user);
}



package test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import Dao.User;
import Dao.UserDao;
import Dao.save;


public class Main {


public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
/* save sa=(save) ac.getBean("save");
sa.doso();*/
 
 
User user=new User();
user.setName("yes!");
user.setPassword("33333");
UserDao userDao=(UserDao) ac.getBean("userdao");
userDao.insertUser(user);
System.out.println("数据添加成功!");
}
}



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean name="save" class="Dao.save"></bean>


<bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
        <property name="locations" value="jdbc.properties"/>      
    </bean>  
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName">
   <value>${jdbc.driverClassName}</value>
   </property>
   <property name="url">
   <value>${jdbc.url}</value>
   </property>
   <property name="username">
   <value>${jdbc.username}</value>
   </property>
   <property name="password">
   <value>${jdbc.password}</value>
   </property>
   </bean>
   
   <bean id="userdao" class="Dao.UserDao">
   <property name="dataSource">
   <ref local="dataSource"/>
   </property>
   </bean>
  
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值