我是maven工程
在数据库,先建一个表,初始化3条数据。
CREATE TABLE `t_user` (
`_id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) DEFAULT NULL,
`userpwd` VARCHAR(32) DEFAULT NULL,
PRIMARY KEY (`_id`)
) ENGINE=INNODB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
SELECT * FROM t_user
INSERT INTO `t_user` VALUES ('1', 'jake', '123456');
INSERT INTO `t_user` VALUES ('2', 'rose', '123456789');
INSERT INTO `t_user` VALUES ('3', 'tom', '999');
application.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="bdvmonitor-dubbo" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.x.112:2182" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="MetricService" interface="com.broadtech.middleware.metric.MetricService" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://192.168.5.xxx:3306/ml_test"/>
<property name="user" value="root"/>
<property name="password" value="xxxxx"/>
</bean>
<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
创建连接的类
package com.broadtech.bdvmonitor.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import com.broadtech.bdvmonitor.main.Main;
public class SpringDataSource {
private static Logger logger = Logger.getLogger(Main.class);
//这里是spring ioc, 取出数据源,初始化
static DataSource dataSource = (DataSource) Main.context.getBean("dataSource");
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public static Connection getConnection() {
Connection conn=null;
try {
conn= dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
logger.error("spring数据源连接有异常",e);
}
return conn ;
}
}
spring配置在main方法中,初始化
package com.broadtech.bdvmonitor.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class TestSpringJdbc {
@Test
public void demo(){
String xmlpath = "springconfig/application.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);
JdbcTemplate jdbcTemplate = (JdbcTemplate) context.getBean("jdbctemplate");
jdbcTemplate.update("insert into t_user(username,userpwd) values(?,?)", "tom2","362427");
}
PreparedStatement st= null;
Connection conn=null;
@Test
public void demo1(){
String xmlpath = "springconfig/application.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xmlpath);
DataSource dataSource = (DataSource) context.getBean("dataSource");
try {
conn = dataSource.getConnection();
String sql="insert into t_user(username,userpwd) values(?,?)";
st=conn.prepareStatement(sql);
st.setString(1, "孟梁");
st.setString(2, "spring测试成功2");
st.addBatch();
st.executeBatch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果: