Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package org.manjosh.main;
  2.  
  3. import org.manjosh.dao.DaoClass;
  4. import org.manjosh.model.Circle;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. public class JdbcMain {
  9.  
  10. public static void main(String[] args){
  11.  
  12. ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
  13.  
  14. DaoClass dao = ctx.getBean("daoClass",DaoClass.class);
  15.  
  16. Circle circle = dao.getCirle(1);
  17. System.out.println(circle.getName());
  18. }
  19.  
  20. }
  21.  
  22. package org.manjosh.dao;
  23.  
  24. import java.sql.Connection;
  25. import java.sql.DriverManager;
  26. import java.sql.PreparedStatement;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;
  29.  
  30. import org.manjosh.model.Circle;
  31. import org.springframework.stereotype.Component;
  32. @Component
  33. public class DaoClass {
  34.  
  35. public Circle getCirle(int circleId){
  36.  
  37. String driver = "oracle.jdbc.driver.OracleDriver";
  38. Connection conn = null;
  39.  
  40. try{
  41. //step1 load the driver class
  42. Class.forName(driver).newInstance();
  43.  
  44. //step2 create the connection object
  45. conn =DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
  46.  
  47. //step3 create the statement object
  48. PreparedStatement stmt=conn.prepareStatement("SELECT * FROM circle where ID =?");
  49. stmt.setInt(1, circleId);
  50.  
  51. Circle circle = null;
  52.  
  53. //step4 execute query
  54. ResultSet rs=stmt.executeQuery();
  55.  
  56. while(rs.next()){
  57. circle = new Circle(rs.getInt(circleId),rs.getString("name"));
  58. }
  59. rs.close();
  60. stmt.close();
  61. return circle;
  62. }
  63. catch(Exception e){
  64. throw new RuntimeException(e);
  65. }
  66. finally {
  67. try{
  68. conn.close();
  69. }
  70. catch(SQLException e){}
  71. }
  72. }
  73.  
  74. }
  75.  
  76. <?xml version = "1.0" encoding = "UTF-8"?>
  77.  
  78. <beans xmlns="http://www.springframework.org/schema/beans"
  79. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  80. xmlns:context="http://www.springframework.org/schema/context"
  81. xsi:schemaLocation="http://www.springframework.org/schema/beans
  82. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  83. http://www.springframework.org/schema/context
  84. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  85.  
  86. <context:annotation-config/>
  87. <context:component-scan base-package = "org.manjosh.main"/>
  88.  
  89.  
  90.  
  91.  
  92. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement