相关jar 包
package sfk.bbs.test.springjsbctempletTest;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
注意:这里用到了spring-tx.jar.
Spring配置文件
<!-- jdbcTemplate -->
<bean />
</bean>
一个简单的测试类
package sfk.bbs.test.springjsbctempletTest;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* 又有一种世上无难事的感觉
* 1.添加jar包
* 2.配置xml文件
* 3.代码
* @author rocky
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TempletTest
{
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void handle() throws SQLException
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/sfkbbs?useUnicode=true&characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("2011211961");
Connection conn = dataSource.getConnection();
PreparedStatement pstm = conn.prepareStatement("select * from tb_StudentOfTestSpringMVC");
ResultSet rs = pstm.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt("id")+"==="+rs.getString("name"));
}
rs.close();
pstm.close();
conn.close();
}
@Test
public void test()
{
fail("Not yet implemented");
}
}
待续...