sring框架的jdbc应用
作者:互联网
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="wuxu112411"/>
</bean> <!-- Definition for studentJDBCTemplate bean -->
<bean id="DaoJdbc"
class="com.cn.DaoJdbc">
<property name="dataSource" ref="dataSource" />
</bean> </beans> javaDAO层获取以及使用 private DataSource ds;
private JdbcTemplate jdbc;
public DataSource getDs() {
return ds;
} @Override
public void setDataSource(DataSource ds) {
this.ds = ds;
this.jdbc = new JdbcTemplate(ds);
} @Override
public void create(String name, String password) {
String sql = "insert into t_user (username, password) values (?, ?)";
jdbc.update( sql, name, password);
System.out.println("Created Record Name = " + name + " Age = " + password);
}
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="wuxu112411"/>
</bean> <!-- Definition for studentJDBCTemplate bean -->
<bean id="DaoJdbc"
class="com.cn.DaoJdbc">
<property name="dataSource" ref="dataSource" />
</bean> </beans> javaDAO层获取以及使用 private DataSource ds;
private JdbcTemplate jdbc;
public DataSource getDs() {
return ds;
} @Override
public void setDataSource(DataSource ds) {
this.ds = ds;
this.jdbc = new JdbcTemplate(ds);
} @Override
public void create(String name, String password) {
String sql = "insert into t_user (username, password) values (?, ?)";
jdbc.update( sql, name, password);
System.out.println("Created Record Name = " + name + " Age = " + password);
}
标签:sring,www,jdbc,框架,DataSource,org,password,ds 来源: https://www.cnblogs.com/wuxu/p/10363845.html