Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Blob;
  8. import java.util.Properties;
  9. import java.util.zip.GZIPInputStream;
  10.  
  11. /**
  12. * Java Program to Access the B2Bi database, and download maps as individual files.
  13. * @author Nicholas Turdo
  14. */
  15.  
  16. public class MapDump {
  17.  
  18. public static final String SQLQUERY = "select m.*, dt.DATA_OBJECT from map_versions mv, map m, DATA_TABLE dt where (upper(mv.map_name) like upper('KLG%') or upper(mv.map_name) like upper('KLA%')) and m.map_name = mv.map_name and m.map_version = mv.default_version and m.UNCOMPILED_MAP = dt.data_id";
  19. public static final String DIRECTORYFORMAPS = "MAP_DIR"; //Need to make directory before running program
  20.  
  21. public static Connection ConnectToDB(){
  22. //URL of Oracle database server
  23. String url = "jdbc:oracle:thin:@timsnas.com:1521:orcl";
  24.  
  25. //properties for creating connection to Oracle database
  26. Properties props = new Properties();
  27. props.setProperty("user", "SI_USER");
  28. props.setProperty("password", "password");
  29.  
  30. //creating connection to Oracle database using JDBC
  31. Connection conn = null;
  32.  
  33. try {
  34. conn = DriverManager.getConnection(url,props);
  35. } catch (SQLException e) {
  36. System.out.println("Application Failed making the Connection: ");
  37. e.printStackTrace();
  38. }
  39.  
  40. return conn;
  41. }
  42.  
  43. public static ResultSet CheckDB(Connection conn){
  44. PreparedStatement preStatement = null;
  45. ResultSet result = null;
  46. try {
  47. //creating PreparedStatement object to execute query
  48. preStatement = conn.prepareStatement(SQLQUERY);
  49. result = preStatement.executeQuery();
  50. } catch (SQLException e) {
  51. System.out.println("Failed Querying The Database: ");
  52. e.printStackTrace();
  53. }
  54. return result;
  55. }
  56.  
  57. public static void saveMaps(ResultSet result){
  58. try{
  59. while(result.next()){
  60. System.out.println("Map Name: " + result.getString("MAP_NAME"));
  61. Blob blob = result.getBlob("Data_Object");
  62. byte[] compressedMap = blob.getBytes(1,(int) blob.length()+1);
  63.  
  64. GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedMap));
  65. byte[] buffer = new byte[8192];
  66. gis.read(buffer, 0, 5);
  67. gis.close();
  68. String s = new String(buffer, 0, 5);
  69. String outputFileName = "";
  70.  
  71. if (s.startsWith("<?xml"))
  72. outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME")+".mxl";
  73. //write outStr to file
  74. else if (s.startsWith("GENTR"))
  75. outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME")+".map";
  76. else
  77. outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME");
  78.  
  79. GZIPInputStream writeToFile = new GZIPInputStream(new ByteArrayInputStream(compressedMap));
  80. FileOutputStream fos = new FileOutputStream(outputFileName);
  81. int length;
  82.  
  83. while ((length = writeToFile.read(buffer, 0, 8192)) != -1)
  84. fos.write(buffer, 0, length);
  85. fos.close();
  86. writeToFile.close();
  87. }
  88. }catch(Exception e){
  89. System.out.println("ERROR");
  90. e.printStackTrace();
  91. }
  92. }
  93.  
  94. /**
  95. * @param args Not Used
  96. * @throws SQLException
  97. */
  98. public static void main(String[] args) throws SQLException{
  99. Connection con = ConnectToDB();
  100. ResultSet result = null;
  101. if(con != null)
  102. result = CheckDB(con);
  103. if(result != null)
  104. saveMaps(result);
  105. System.out.println("done");
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement