Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package uk.ac.ucl.cege.cegeg077.uceskkw;
  2.  
  3. import java.sql.*;
  4. import java.text.DecimalFormat;
  5. import java.util.ArrayList;
  6. import java.io.*;
  7. import oracle.jdbc.pool.OracleDataSource;
  8.  
  9. public class oracleSQLTEST {
  10. private Connection conn;
  11.  
  12. // nb the initialisation method does not return a value, so not public void
  13. // oracleSQL
  14. public oracleSQLTEST(String theareaname) {
  15. // the initialisation method
  16. conn = null;
  17. System.out.println("-------- Oracle JDBC Connection Initializing... ------");
  18. try {
  19. conn = createCon();
  20. } catch (ClassNotFoundException e) {
  21. System.out.println("Oracle JDBC Driver Missing.");
  22. e.printStackTrace();
  23. return;
  24. } catch (SQLException e) {
  25. System.out.println("Connection Failed! Check output console");
  26. e.printStackTrace();
  27. return;
  28. }
  29. if (conn != null) {
  30. System.out.println("Database Connection Successful");
  31. } else {
  32. System.out.println("Failed to make connection.");
  33. }
  34. }
  35.  
  36. private Statement createStatement() {
  37. // Create a statement
  38. Statement stmt = null;
  39. try {
  40. stmt = conn.createStatement();
  41. } catch (SQLException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. return stmt;
  46. }
  47.  
  48. public Connection createCon() throws ClassNotFoundException, SQLException {
  49. String user = "user";
  50. String password = "password";
  51. String database = "database";
  52.  
  53. // Open an OracleDataSource and get a connection
  54. OracleDataSource ods = new OracleDataSource();
  55. ods.setURL("jdbc:oracle:thin:@localhost:1521:" + database);
  56. ods.setUser(user);
  57. ods.setPassword(password);
  58. Connection conn = ods.getConnection();
  59. return conn;
  60.  
  61. }
  62.  
  63. // an example routine
  64. public void createKMLFile(String kmltablename) {
  65. try {
  66. conn = createCon();
  67. String query = "select geometry from " + kmltablename;
  68. System.out.println(query);
  69. Statement stmt1 = createStatement();
  70. ResultSet rs = stmt1.executeQuery(query);
  71. System.out.println("Result Set = " + rs);
  72.  
  73. while (rs.next()) {
  74. System.out.println(rs.getString(1));
  75. String building_id = rs.getString(1);
  76. if (building_id == null) {// if you fetched null value then
  77. // initialize output with blank
  78. // string
  79. building_id = "1";
  80. }
  81.  
  82. query = "select * from table(select b.geometry.sdo_ordinates from buildings b ";
  83. query += " where id = '" + building_id + "')";
  84. Statement stmt2 = createStatement();
  85.  
  86. ResultSet theCoords = stmt2.executeQuery(query);
  87. System.out.println(query);
  88.  
  89. // find the first z value
  90. int coordCount = 0;
  91. while (theCoords.next()) {
  92. // System.out.println("l00000000000000000000000000000"
  93. // + building_id);
  94. coordCount = coordCount + 1;
  95. double xvalue = theCoords.getDouble(1);
  96. theCoords.next();
  97. double yvalue = theCoords.getDouble(1);
  98. theCoords.next();
  99. double zvalue = theCoords.getDouble(1);
  100.  
  101. System.out.println(xvalue);
  102. System.out.println(yvalue);
  103. System.out.println(zvalue);
  104.  
  105. }
  106. building_id = building_id + 1; // questionable bit of code here...
  107. theCoords.close();
  108. stmt2.close();
  109. System.out.println(building_id);
  110. }
  111.  
  112. rs.close();
  113. stmt1.close();
  114.  
  115. conn.close();
  116.  
  117. } catch (ClassNotFoundException e) {
  118. System.out.println("Class Not Found Error");
  119. e.printStackTrace();
  120. } catch (SQLException e) {
  121. System.out.println("SQL Exception Error");
  122. e.printStackTrace();
  123. } catch (Exception e) {// Catch exception if any
  124. System.err.println("Error: " + e.getMessage());
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement