Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. package my.jdbc.app;
  2.  
  3. import java.sql.SQLException;
  4.  
  5. import org.xml.sax.Attributes;
  6. import org.xml.sax.SAXException;
  7. import org.xml.sax.helpers.DefaultHandler;
  8.  
  9. public class MyEventHandler extends DefaultHandler {
  10.    
  11.     final MyConnectionHandler connection;
  12.     int orderNo;
  13.     String name;
  14.     boolean isItem = false;
  15.    
  16.     public MyEventHandler() throws SQLException {
  17.         // Setup a database connection
  18.         connection = new MyConnectionHandler();
  19.     }
  20.    
  21.     @Override
  22.     public void startDocument() throws SAXException {
  23.         try {
  24.             // Clear all previous entries out of the database table to avoid primary key conflicts
  25.             connection.clear();
  26.         } catch (SQLException e) {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30.    
  31.     @Override
  32.     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  33.         // Check if current element is an <item>
  34.         if(qName == "item") {
  35.             // If so, switch to item mode by setting the boolean isItem to true
  36.             isItem = true;
  37.             // Getting the "orderNo" attribute and parsing it to Integer
  38.             orderNo = Integer.parseInt(attributes.getValue("orderNo"));
  39.            
  40.         }
  41.     }
  42.    
  43.     @Override
  44.     public void endElement(String uri, String localName, String qName) throws SAXException {
  45.         // If the program is in "item mode" - and therefore the necessary elements have been gathered - shoot an insert statement
  46.         if(isItem) {
  47.             try {
  48.                 //
  49.                 connection.insertItem(orderNo, name);
  50.             } catch (NumberFormatException e) {
  51.                 // TODO Auto-generated catch block
  52.                 e.printStackTrace();
  53.             } catch (SQLException e) {
  54.                 // TODO Auto-generated catch block
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.        
  59.         // Reset the isItem boolean
  60.         isItem = false;
  61.     }
  62.    
  63.     @Override
  64.     public void characters(char[] ch, int start, int length) throws SAXException {
  65.         // If element is an item, save the item's textual content
  66.         if(isItem) {
  67.             name = new String(ch, start, length);
  68.         }
  69.     }
  70.    
  71.     @Override
  72.     public void endDocument() throws SAXException {
  73.        
  74.         try {
  75.             // Close the database connection to avoid socket leaks
  76.             connection.close();
  77.         } catch (SQLException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement