File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .List ;
2+
3+ import javax .sql .DataSource ;
4+
5+ import org .springframework .jdbc .core .JdbcTemplate ;
6+
7+ /**
8+ * Simple Java class which uses Spring's JdbcTemplate class to implement
9+ * business logic.
10+ *
11+ */
12+ public class EmpJDBCTemplate {
13+ private DataSource dataSource ;
14+ private JdbcTemplate jdbcTemplate ;
15+
16+ public void setDataSource (DataSource dataSource ) {
17+ this .dataSource = dataSource ;
18+ this .jdbcTemplate = new JdbcTemplate (dataSource );
19+
20+ }
21+
22+ public void displayEmpList () {
23+ final String sql = "SELECT ename FROM emp" ;
24+ List <EmployeeDAO > employees = jdbcTemplate .query (sql , new EmployeeMapper ());
25+ for (EmployeeDAO employee : employees ) {
26+ System .out .println (employee .getName ());
27+ }
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+
2+ /**
3+ * Simple DAO class implementation for EMP table.
4+ *
5+ */
6+ public class EmployeeDAO {
7+ private String name ;
8+
9+ public String getName () {
10+ return name ;
11+ }
12+
13+ public void setName (String name ) {
14+ this .name = name ;
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ import java .sql .ResultSet ;
2+ import java .sql .SQLException ;
3+
4+ import org .springframework .jdbc .core .RowMapper ;
5+
6+ /**
7+ * Simple Row mapper implementation class for EMP table.
8+ *
9+ */
10+ public class EmployeeMapper implements RowMapper <EmployeeDAO > {
11+
12+ @ Override
13+ public EmployeeDAO mapRow (ResultSet rs , int rowNo ) throws SQLException {
14+ EmployeeDAO emp = new EmployeeDAO ();
15+ emp .setName (rs .getString ("ename" ));
16+ return emp ;
17+ }
18+
19+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <beans xmlns =" http://www.springframework.org/schema/beans"
3+ xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
5+
6+ <!-- Initialization for data source -->
7+ <bean id =" dataSource" class =" oracle.ucp.jdbc.PoolDataSourceImpl" >
8+ <property name =" connectionFactoryClassName"
9+ value=" oracle.jdbc.pool.OracleDataSource" />
10+ <property name =" URL" value =" jdbc:oracle:thin:@jdbctest_medium?TNS_ADMIN=D:/temp/wallet_JDBCTEST" />
11+ <property name =" user" value =" jdbcuser" />
12+ <property name =" password" value =" WE10come12##" />
13+ <property name =" maxPoolSize" value =" 10" />
14+ <property name =" initialPoolSize" value =" 5" />
15+ </bean >
16+
17+ <!-- Definition for EmpJDBCTemplate bean -->
18+ <bean id =" EmpJDBCTemplate" class =" EmpJDBCTemplate" >
19+ <property name =" dataSource" ref =" dataSource" />
20+ </bean >
21+
22+ </beans >
Original file line number Diff line number Diff line change 1+
2+ import org .springframework .boot .CommandLineRunner ;
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .context .ApplicationContext ;
5+ import org .springframework .context .annotation .Bean ;
6+ import org .springframework .context .annotation .ImportResource ;
7+
8+ /**
9+ * SpringBoot application main class. It uses JdbcTemplate class which
10+ * internally uses UCP for connection check-outs and check-ins.
11+ *
12+ */
13+ // Specify the Spring XML config file name
14+ @ ImportResource ({ "classpath*:HelloAppConfig.xml" })
15+ public class HelloApplication {
16+
17+ public static void main (String [] args ) {
18+ SpringApplication .run (HelloApplication .class , args );
19+ }
20+
21+ @ Bean
22+ public CommandLineRunner commandLineRunner (ApplicationContext ctx ) {
23+ return args -> {
24+ final EmpJDBCTemplate empJDBCTemplate = (EmpJDBCTemplate ) ctx .getBean ("EmpJDBCTemplate" );
25+ System .out .println ("Listing employee records : " );
26+ empJDBCTemplate .displayEmpList ();
27+ };
28+ }
29+
30+ }
You can’t perform that action at this time.
0 commit comments