Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Blob;
- import java.util.Properties;
- import java.util.zip.GZIPInputStream;
- /**
- * Java Program to Access the B2Bi database, and download maps as individual files.
- * @author Nicholas Turdo
- */
- public class MapDump {
- 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";
- public static final String DIRECTORYFORMAPS = "MAP_DIR"; //Need to make directory before running program
- public static Connection ConnectToDB(){
- //URL of Oracle database server
- String url = "jdbc:oracle:thin:@timsnas.com:1521:orcl";
- //properties for creating connection to Oracle database
- Properties props = new Properties();
- props.setProperty("user", "SI_USER");
- props.setProperty("password", "password");
- //creating connection to Oracle database using JDBC
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url,props);
- } catch (SQLException e) {
- System.out.println("Application Failed making the Connection: ");
- e.printStackTrace();
- }
- return conn;
- }
- public static ResultSet CheckDB(Connection conn){
- PreparedStatement preStatement = null;
- ResultSet result = null;
- try {
- //creating PreparedStatement object to execute query
- preStatement = conn.prepareStatement(SQLQUERY);
- result = preStatement.executeQuery();
- } catch (SQLException e) {
- System.out.println("Failed Querying The Database: ");
- e.printStackTrace();
- }
- return result;
- }
- public static void saveMaps(ResultSet result){
- try{
- while(result.next()){
- System.out.println("Map Name: " + result.getString("MAP_NAME"));
- Blob blob = result.getBlob("Data_Object");
- byte[] compressedMap = blob.getBytes(1,(int) blob.length()+1);
- GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressedMap));
- byte[] buffer = new byte[8192];
- gis.read(buffer, 0, 5);
- gis.close();
- String s = new String(buffer, 0, 5);
- String outputFileName = "";
- if (s.startsWith("<?xml"))
- outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME")+".mxl";
- //write outStr to file
- else if (s.startsWith("GENTR"))
- outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME")+".map";
- else
- outputFileName = DIRECTORYFORMAPS + "/" + result.getString("MAP_NAME");
- GZIPInputStream writeToFile = new GZIPInputStream(new ByteArrayInputStream(compressedMap));
- FileOutputStream fos = new FileOutputStream(outputFileName);
- int length;
- while ((length = writeToFile.read(buffer, 0, 8192)) != -1)
- fos.write(buffer, 0, length);
- fos.close();
- writeToFile.close();
- }
- }catch(Exception e){
- System.out.println("ERROR");
- e.printStackTrace();
- }
- }
- /**
- * @param args Not Used
- * @throws SQLException
- */
- public static void main(String[] args) throws SQLException{
- Connection con = ConnectToDB();
- ResultSet result = null;
- if(con != null)
- result = CheckDB(con);
- if(result != null)
- saveMaps(result);
- System.out.println("done");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement