Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.manjosh.main;
- import org.manjosh.dao.DaoClass;
- import org.manjosh.model.Circle;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class JdbcMain {
- public static void main(String[] args){
- ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
- DaoClass dao = ctx.getBean("daoClass",DaoClass.class);
- Circle circle = dao.getCirle(1);
- System.out.println(circle.getName());
- }
- }
- package org.manjosh.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.manjosh.model.Circle;
- import org.springframework.stereotype.Component;
- @Component
- public class DaoClass {
- public Circle getCirle(int circleId){
- String driver = "oracle.jdbc.driver.OracleDriver";
- Connection conn = null;
- try{
- //step1 load the driver class
- Class.forName(driver).newInstance();
- //step2 create the connection object
- conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
- //step3 create the statement object
- PreparedStatement stmt=conn.prepareStatement("SELECT * FROM circle where ID =?");
- stmt.setInt(1, circleId);
- Circle circle = null;
- //step4 execute query
- ResultSet rs=stmt.executeQuery();
- while(rs.next()){
- circle = new Circle(rs.getInt(circleId),rs.getString("name"));
- }
- rs.close();
- stmt.close();
- return circle;
- }
- catch(Exception e){
- throw new RuntimeException(e);
- }
- finally {
- try{
- conn.close();
- }
- catch(SQLException e){}
- }
- }
- }
- <?xml version = "1.0" encoding = "UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <context:annotation-config/>
- <context:component-scan base-package = "org.manjosh.main"/>
- </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement