Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. Error performing query: javax.servlet.ServletException: org.xml.sax.SAXParseException: Content is not allowed in prolog.
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.sql.*; // JDBC packages
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. import javax.xml.parsers.*;
  9. import org.xml.sax.*;
  10. import org.xml.sax.helpers.*;
  11.  
  12. public class step5 extends HttpServlet {
  13.  
  14. public static final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
  15. public static final String URL = "jdbc:odbc:rreOracle";
  16. public static final String username = "cm485a10";
  17. public static final String password = "y4e8f7s5";
  18.  
  19. SAXParserFactory factory;
  20.  
  21. public void init() throws ServletException {
  22.  
  23. factory = SAXParserFactory.newInstance();
  24. }
  25.  
  26. public void doGet (HttpServletRequest request,
  27. HttpServletResponse response)
  28. throws ServletException, IOException
  29. {
  30. PrintWriter out = response.getWriter();
  31. Connection con = null;
  32.  
  33. try {
  34.  
  35. Class.forName(DRIVER);
  36.  
  37. con = DriverManager.getConnection(URL,username,password);
  38.  
  39. try {
  40. Statement stmt = con.createStatement();
  41. ResultSet rs = stmt.executeQuery("SELECT sale_id, home_id, agent_id, customer_id FROM sale");
  42.  
  43. String xml = "";
  44. xml = xml + "<sales_description>";
  45. xml = xml + "<sale>";
  46.  
  47. boolean courseDataDone = false;
  48. while (rs.next()) {
  49. String sale_id = rs.getString(1);
  50. String home_id = rs.getString(2);
  51. String agent_id = rs.getString(3);
  52. String customer_id = rs.getString(4);
  53.  
  54. if (!courseDataDone) {
  55. xml = xml + "<sale_id>" + sale_id + "</sale_id>" +
  56. "<home_id>" + home_id + "</home_id>" +
  57. "<agent_id>" + agent_id + "</agent_id>" +
  58. "<customer_id>" + customer_id + "</customer_id>" +
  59. "" +
  60. "";
  61. courseDataDone = true;
  62. }
  63.  
  64. }
  65.  
  66. xml = xml + "</sale>" +
  67. "</sales_description>";
  68.  
  69. try {
  70. SAXParser parser = factory.newSAXParser();
  71. InputSource input = new InputSource(new StringReader(xml));
  72. parser.parse(input, new DefaultHandler());
  73. } catch (ParserConfigurationException e) {
  74. throw new ServletException(e);
  75. } catch (SAXException e) {
  76. throw new ServletException(e);
  77. }
  78.  
  79. response.setContentType("text/xml;charset=UTF-8");
  80. out.write(xml);
  81.  
  82. } catch(Exception ex) {
  83. out.println("Error performing query: " + ex);
  84. con.close();
  85. return;
  86. }
  87.  
  88. } catch(Exception ex) {
  89. out.println("Error performing DB connection: " + ex);
  90. return;
  91. }
  92.  
  93. }
  94. }
  95.  
  96. <?xml version="1.0" encoding="UTF-8"?>
  97.  
  98. response.setContentType("text/xml;charset=UTF-8");
  99. PrintWriter writer = response.getWriter();
  100. writer.append("<?xml version="1.0" encoding="UTF-8"?>");
  101. writer.append("<sales_description>");
  102.  
  103. try (
  104. Connection connection = dataSource.getConnection();
  105. Statement statement = connection.createStatement();
  106. ResultSet resultSet = statement.executeQuery("SELECT sale_id, home_id, agent_id, customer_id FROM sale");
  107. ) {
  108. if (resultSet.next()) {
  109. writer.append("<sale>");
  110. writer.append("<sale_id>").append(resultSet.getString("sale_id")).append("</sale_id>");
  111. writer.append("<home_id>").append(resultSet.getString("home_id")).append("</home_id>");
  112. writer.append("<agent_id>").append(resultSet.getString("agent_id")).append("</agent_id>");
  113. writer.append("</sale>");
  114. }
  115. } catch (SQLException e) {
  116. throw new ServletException(e);
  117. }
  118.  
  119. writer.append("</sales_description>");
  120.  
  121. package badservlet.model;
  122.  
  123. public class Sale
  124. {
  125. private String saleId;
  126. private String homeId;
  127. private String agentId;
  128. private String customerId;
  129.  
  130. public Sale(String saleId, String homeId, String agentId, String customerId)
  131. {
  132. if ((saleId == null) || (saleId.trim().length() == 0)
  133. throw new IllegalArgumentException("sales id cannot be blank or null");
  134. if ((homeId == null) || (homeId.trim().length() == 0)
  135. throw new IllegalArgumentException("home id cannot be blank or null");
  136. if ((agentId == null) || (agentId.trim().length() == 0)
  137. throw new IllegalArgumentException("agent id cannot be blank or null");
  138. if ((customerId == null) || (customerId.trim().length() == 0)
  139. throw new IllegalArgumentException("customer id cannot be blank or null");
  140.  
  141. this.saleId = saleId;
  142. this.homeId = homeId;
  143. this.agentId = agentId;
  144. this.customerId = customerId;
  145. }
  146.  
  147. public String getSaleId()
  148. {
  149. return saleId;
  150. }
  151.  
  152. public String getHomeId()
  153. {
  154. return homeId;
  155. }
  156.  
  157. public String getAgentId()
  158. {
  159. return agentId;
  160. }
  161.  
  162. public String getCustomerId()
  163. {
  164. return customerId;
  165. }
  166.  
  167. @Override
  168. public String toString()
  169. {
  170. return "Sale{" +
  171. "saleId='" + saleId + ''' +
  172. ", homeId='" + homeId + ''' +
  173. ", agentId='" + agentId + ''' +
  174. ", customerId='" + customerId + ''' +
  175. '}';
  176. }
  177. }
  178.  
  179. package badservlet.persistence;
  180.  
  181. import badservlet.model.Sale;
  182.  
  183. import java.sql.SQLException;
  184. import java.util.List;
  185.  
  186. public interface SaleDao
  187. {
  188. List<Sale> find() throws SQLException;
  189. }
  190.  
  191. package badservlet.persistence;
  192.  
  193. import badservlet.model.Sale;
  194.  
  195. import javax.sql.DataSource;
  196. import java.sql.Connection;
  197. import java.sql.ResultSet;
  198. import java.sql.SQLException;
  199. import java.sql.Statement;
  200. import java.util.ArrayList;
  201. import java.util.List;
  202.  
  203. public class SaleDaoImpl implements SaleDao
  204. {
  205. private static final String SELECT_ALL_SQL = "SELECT sale_id, home_id, agent_id, customer_id FROM sale";
  206.  
  207. private Connection connection;
  208.  
  209. public SaleDaoImpl(Connection connection)
  210. {
  211. this.connection = connection;
  212. }
  213.  
  214. public SaleDaoImpl(DataSource dataSource) throws SQLException
  215. {
  216. this(dataSource.getConnection());
  217. }
  218.  
  219. public List<Sale> find() throws SQLException
  220. {
  221. List<Sale> allSales = new ArrayList<Sale>();
  222.  
  223. Statement st = null;
  224. ResultSet rs = null;
  225.  
  226. try
  227. {
  228. st = this.connection.createStatement();
  229. rs = st.executeQuery(SELECT_ALL_SQL);
  230. while (rs.next())
  231. {
  232. String saleId = rs.getString("sale_id");
  233. String homeId = rs.getString("home_id");
  234. String agentId = rs.getString("agent_id");
  235. String customerId = rs.getString("customer_id");
  236. Sale sale = new Sale(saleId, homeId, agentId, customerId);
  237. allSales.add(sale);
  238. }
  239.  
  240. }
  241. catch (SQLException e)
  242. {
  243. e.printStackTrace();
  244. }
  245. finally
  246. {
  247. try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
  248. try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); }
  249. }
  250.  
  251. return allSales;
  252. }
  253. }
  254.  
  255. package badservlet.xml;
  256.  
  257. import badservlet.model.Sale;
  258. import org.jdom.Document;
  259. import org.jdom.Element;
  260. import org.jdom.transform.JDOMResult;
  261.  
  262. import javax.xml.bind.JAXBException;
  263. import javax.xml.transform.Result;
  264. import java.util.List;
  265.  
  266. public class SaleUnmarshaller
  267. {
  268. public void unmarshal(Object object, Result xml) throws JAXBException
  269. {
  270. List<Sale> allSales = (List<Sale>) object;
  271. Document document = new Document(new Element("sales"));
  272. for (Sale sale : allSales)
  273. {
  274. Element child = new Element("sale");
  275. child.setAttribute("id", sale.getSaleId());
  276. child.addContent(new Element("home", sale.getHomeId()));
  277. child.addContent(new Element("agent", sale.getAgentId()));
  278. child.addContent(new Element("customer", sale.getCustomerId()));
  279. document.addContent(child);
  280. }
  281.  
  282. JDOMResult result = new JDOMResult();
  283. result.setDocument(document);
  284. xml = result;
  285. }
  286. }
  287.  
  288. FirstSaleIDFirstHomeFirstAgentFirstCustomerSecondSaleIDSecondHomeSecondAgentSecondCustomer...
  289.  
  290. |sale_id | home_id | agent_id | customer_id |
  291. | 1 | 10 | 100 | 1000 |
  292. | 2 | 20 | 200 | 2000 |
  293. | 3 | 30 | 300 | 3000 |
  294.  
  295. 1010100100020003000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement