Advertisement
alinapixii

SW-LAB3

Oct 11th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package sw;
  7.  
  8.  
  9. import java.sql.*;
  10. import java.io.StringWriter;
  11. import java.io.Writer;
  12. import java.util.ArrayList;
  13. import java.util.Iterator;
  14.  
  15.  
  16. import javax.xml.parsers.DocumentBuilder;
  17. import javax.xml.parsers.DocumentBuilderFactory;
  18. import javax.xml.transform.OutputKeys;
  19. import javax.xml.transform.Transformer;
  20. import javax.xml.transform.TransformerFactory;
  21. import javax.xml.transform.dom.DOMSource;
  22. import javax.xml.transform.stream.StreamResult;
  23.  
  24. import org.w3c.dom.Comment;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Element;
  27.  
  28. /**
  29.  *
  30.  * @author student
  31.  */
  32. public class SW{
  33.  
  34.     /**
  35.      * @param args the command line arguments
  36.      */
  37.    
  38.  
  39.  
  40. //​public static void main(String[] args) throws Exception {
  41. //​​
  42. //​​
  43. //
  44. //}
  45.    public static void main(String[] args) throws ClassNotFoundException, SQLException, Exception {
  46.        
  47.         String url="jdbc:derby://localhost:1527/tripss";
  48.          final String JDBC_DRIVER = "com.derby.jdbc.Driver";  
  49.          final String DB_URL = "jdbc:derby://localhost/tripss";
  50.         Connection con=DriverManager.getConnection(url, "a", "a");
  51.         Statement instr=con.createStatement();
  52.         Statement instr2=con.createStatement();
  53.         String sql="SELECT * FROM a.trips t";
  54.         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  55.       DocumentBuilder builder = dbf.newDocumentBuilder();
  56.         Document doc = builder.newDocument();
  57.          Element element = doc.createElement("trips");
  58.          doc.appendChild(element);
  59.  
  60.  
  61.          //Comment comment = doc.createComment("This is a comment");
  62.          //doc.insertBefore(comment, element);
  63.  
  64.  
  65.  
  66.          //element.appendChild(itemElement);
  67.  
  68.          //itemElement.setAttribute("myattr", "attrvalue");
  69.  
  70.          //itemElement.insertBefore(doc.createTextNode("text"), itemElement.getLastChild());
  71.  
  72.  
  73.         ArrayList<Trip> trips = new ArrayList<Trip>();
  74.         //String sql2="SELECT t.Title FROM app.trip t, app.address a where p.address= a.id and a.city= 'Bucharest'";
  75.         ResultSet rs=instr.executeQuery(sql);
  76.         //ResultSet rs2=instr2.executeQuery(sql2);
  77.             while(rs.next()){
  78.                 Trip t = new Trip(Integer.parseInt(rs.getString(1)),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5), rs.getString(6));  
  79.                 trips.add(t);
  80.             }
  81.             Iterator<Trip> it= trips.iterator();
  82.            
  83.             for (Trip t:trips) {
  84.          
  85.  
  86.  
  87.         Element itemElement = doc.createElement("trip");
  88.  
  89.         element.appendChild(itemElement);
  90.        
  91.         Element agencyElement = doc.createElement("agency");
  92.  
  93.         itemElement.appendChild(agencyElement);
  94.        
  95.         Element titleElement = doc.createElement("title");
  96.  
  97.         itemElement.appendChild(titleElement);
  98.            
  99.         Element descriptionElement = doc.createElement("description");
  100.  
  101.         itemElement.appendChild(descriptionElement);
  102.  
  103.  
  104.         itemElement.setAttribute("id", ""+t.getId());
  105.         itemElement.setAttribute("startDate", ""+t.getStartt());
  106.         itemElement.setAttribute("endDate", ""+t.getEndt());
  107.  
  108.  
  109.         itemElement.insertBefore(doc.createTextNode(""), itemElement.getLastChild());
  110.         agencyElement.insertBefore(doc.createTextNode(t.getAgency()), agencyElement.getLastChild());
  111.         titleElement.insertBefore(doc.createTextNode(t.getTitle()), titleElement.getLastChild());
  112.         descriptionElement.insertBefore(doc.createTextNode(t.getDescription()), descriptionElement.getLastChild());
  113.             }
  114. //            while(it.hasNext()) {
  115. //                itemElement.setAttribute("myattr", Integer.toString(it.next().getId()));
  116. //                itemElement.setAttribute("myattr", it.next().getStartt());
  117. //                itemElement.setAttribute("myattr", it.next().getEndt());
  118. //                itemElement.insertBefore(doc.createTextNode("Agency"), itemElement.getLastChild());
  119. //            }
  120. //            while(rs2.next()){
  121. //                System.out.println(rs2.getString("firstName"));
  122. //            }
  123.   prettyPrint(doc);
  124.         rs.close();
  125.         instr.close();
  126.         con.close();
  127.         //rs2.close();
  128.         instr2.close();
  129.         con.close();
  130.        
  131.    
  132.      
  133.    
  134.    }
  135.    
  136.   public static final void prettyPrint(Document xml) throws Exception {
  137.     Transformer tf = TransformerFactory.newInstance().newTransformer();
  138.     tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  139.     tf.setOutputProperty(OutputKeys.INDENT, "yes");
  140.     Writer out = new StringWriter();
  141.     tf.transform(new DOMSource(xml), new StreamResult(out));
  142.     System.out.println(out.toString());
  143.     }
  144. }
  145.  
  146.  
  147.  
  148.  
  149. package sw;
  150.  
  151. /**
  152.  *
  153.  * @author student
  154.  */
  155. public class Trip {
  156.     private int id;
  157.     private String endt;
  158.     private String startt;
  159.     private String Agency;
  160.  
  161.     public int getId() {
  162.         return id;
  163.     }
  164.  
  165.     public String getEndt() {
  166.         return endt;
  167.     }
  168.  
  169.     public String getStartt() {
  170.         return startt;
  171.     }
  172.  
  173.     public String getAgency() {
  174.         return Agency;
  175.     }
  176.  
  177.     public String getTitle() {
  178.         return title;
  179.     }
  180.  
  181.     public String getDescription() {
  182.         return Description;
  183.     }
  184.     private String title;
  185.     private String Description;
  186.  
  187.     public Trip(int id, String endt, String startt, String Agency, String title, String Description) {
  188.         this.id = id;
  189.         this.endt = endt;
  190.         this.startt = startt;
  191.         this.Agency = Agency;
  192.         this.title = title;
  193.         this.Description = Description;
  194.     }
  195.    
  196.          
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement