相关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;

jdbcTemplate的配置

注意:这里用到了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");
    }

}
待续...

 

相关文章:

  • 2021-05-20
  • 2022-12-23
  • 2021-12-07
  • 2021-11-01
  • 2021-10-29
猜你喜欢
  • 2021-11-29
  • 2021-08-12
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2021-08-22
相关资源
相似解决方案