0% found this document useful (0 votes)
30 views2 pages

EmployeeDAO Java

The document contains code for an EmployeeDAO class that uses JDBC to connect to a database and retrieve the employee count. It defines a method getEmpsCount() that executes a prepared statement to run a SQL query counting employees, and returns the result. A test class is also included that uses Spring Boot to run the application, loads the EmployeeDAO bean, and calls getEmpsCount() to print the employee count. Configuration properties are provided to connect to an Oracle database.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

EmployeeDAO Java

The document contains code for an EmployeeDAO class that uses JDBC to connect to a database and retrieve the employee count. It defines a method getEmpsCount() that executes a prepared statement to run a SQL query counting employees, and returns the result. A test class is also included that uses Spring Boot to run the application, loads the EmployeeDAO bean, and calls getEmpsCount() to print the employee count. Configuration properties are provided to connect to an Oracle database.

Uploaded by

rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//EmployeeDAO.

java
package com.nt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import org.springframework.stereotype.Component;

@Component("empDAO")
public class EmployeeDAO {

private static final String GET_EMPS_COUNT ="SELECT COUNT (*) FROM EMP";

private DataSource ds;

public int getEmpsCount() throws Exception{


Connection con= ds.getConnection();

PreparedStatement ps =con.prepareStatement(GET_EMPS_COUNT);
ResultSet rs= ps.executeQuery();
rs.next();
int count =rs.getInt(1);
ps.close();
con.close();
return count;

}
}

//=========================================================================
//com.nt.test

package com.nt.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import com.nt.dao.EmployeeDAO;

@SpringBootApplication
public class BootProj01JdbcApplication {

public static void main(String[] args) {

ApplicationContext ctx=
SpringApplication.run(BootProj01JdbcApplication.class, args);
EmployeeDAO dao=ctx.getBean("empDAO",EmployeeDAO.class);

try {
System.out.println("emps count ::"+dao.getEmpsCount());
}
catch(Exception e) {
e.printStackTrace();
}

((ConfigurableApplicationContext)ctx).close();
}

//
===================================================================================
===========

#jdbc example

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=system
spring.datasource.password=admin

You might also like