package com.net;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
public class DBConnection {
private ApplicationContext applicationContext;
private JdbcTemplate jdbcTemplate1;
public JdbcTemplate getJdbcTemplate1() {
if(applicationContext==null){
applicationContext=new ClassPathXmlApplicationContext("unitContext.xml");
}
jdbcTemplate1=(JdbcTemplate)applicationContext.getBean("jdbcTemplate");
return jdbcTemplate1;
}
public void setJdbcTemplate1(JdbcTemplate jdbcTemplate1) {
this.jdbcTemplate1 = jdbcTemplate1;
}
public List queryForList(String sql){
return getJdbcTemplate1().queryForList(sql);
}
/**
* @param sql String sql="select * from student where id=?";
* @param params Object[] params = new Object[] {"11"};
* @param types int[] types = new int[] {Types.VARCHAR};
* @return
*/
public List queryForList(String sql,Object[] params,int[] types){
return getJdbcTemplate1().queryForList(sql, params, types);
}
public void query(String sql){
getJdbcTemplate1().query(sql, new RowCallbackHandler() {
public void processRow(ResultSet rst) throws SQLException {
System.out.println(rst.getString(1));//从第一列开始
}
});
}
public int insert(String sql){
return getJdbcTemplate1().update(sql);
}
public int insert(String sql,Object[] params,int[] types){
return getJdbcTemplate1().update(sql,params,types);
}
public static void main(String[] args) {
String sql="insert into student values(?,'3232')";
Object[] params = new Object[] {"44"};
int[] types = new int[] {Types.VARCHAR};
new DBConnection().query("select * from student");
new DBConnection().insert(sql,params,types);
// System.out.println(list);
}
}
<?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-2.5.xsd">
<!--
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
-->
<bean id="SQLDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;databasename=test" />
<property name="username" value="wei" />
<property name="password" value="1987828" />
<property name="maxActive" value="50"></property>
<property name="maxIdle" value="10"></property>
<property name="maxWait" value="1000"></property>
</bean>
<bean id="TransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="SQLDataSource" />
</bean>
<bean id="spJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="SQLDataSource" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="SQLDataSource" />
</bean>
<bean id="DataUtilit" class="com.net.DBConnection">
<property name="jdbcTemplate1">
<ref bean="jdbcTemplate" />
</property>
</bean>
</beans>
|