Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>WebApp</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. </head>
  7. <body style="background-color:blue;">
  8. <center>
  9. <font color="white">
  10. <h1> Welcome to the Project 4 Remote Database Management System</h1>
  11. <hr>
  12. You are connected to the Project4 database. <br>Please enter any valid SQL query or update statement.<br>
  13. If no query/update command is given the Execute button will display all supplier information in the database. <br>All execution results will appear below.
  14. <br>
  15. <br>
  16. <form action="NewServlet" method="post">
  17. <textarea rows="10" cols="60"name="command"></textarea>
  18. <br>
  19. <button type="submit">Execute Query</button>
  20. <button type="submit">Clear Command</button>
  21. </form>
  22. <hr>
  23. <h1>Database Results</h1>
  24. </font>
  25. </body>
  26. </html>
  27.  
  28. import java.io.IOException;
  29. import java.io.PrintWriter;
  30. import java.sql.Connection;
  31. import java.sql.DriverManager;
  32. import java.sql.ResultSet;
  33. import java.sql.SQLException;
  34. import java.sql.Statement;
  35. import java.util.ArrayList;
  36. import java.util.Vector;
  37. import java.util.logging.Level;
  38. import java.util.logging.Logger;
  39. import javax.servlet.ServletException;
  40. import javax.servlet.http.HttpServlet;
  41. import javax.servlet.http.HttpServletRequest;
  42. import javax.servlet.http.HttpServletResponse;
  43. import javax.swing.JOptionPane;
  44.  
  45. /**
  46. *
  47. * @author KJ4CC
  48. */
  49. public class NewServlet extends HttpServlet {
  50.  
  51. /**
  52. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  53. * methods.
  54. *
  55. * @param request servlet request
  56. * @param response servlet response
  57. * @throws ServletException if a servlet-specific error occurs
  58. * @throws IOException if an I/O error occurs
  59. */
  60. Connection connection;
  61. Vector<String> columnNames = new Vector<String>();
  62. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  63. throws ServletException, IOException {
  64. response.setContentType("text/html;charset=UTF-8");
  65. try (PrintWriter out = response.getWriter()) {
  66. /* TODO output your page here. You may use following sample code. */
  67. String command = request.getParameter("command");
  68. out.println("<!DOCTYPE html>");
  69. out.println("<html>");
  70. sqlConnection(command);
  71. //prints out column names into the browser.
  72. out.println(columnNames);
  73.  
  74. }
  75. }
  76. public void sqlConnection(String command){
  77. String driver = "com.mysql.jdbc.Driver";
  78. String url = "jdbc:mysql://localhost:3306/project3";
  79. String user = "root";
  80. String pass = "Brandy?1994";
  81. ResultSet rs;
  82. try {
  83. Class.forName(driver);
  84. } catch (ClassNotFoundException ex) {
  85. Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
  86. }
  87. try {
  88. connection = DriverManager.getConnection(url,user,pass);
  89. } catch (SQLException ex) {
  90. Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
  91. }
  92. Statement stmt;
  93. try {
  94. stmt = connection.createStatement();
  95. rs = stmt.executeQuery(command);
  96. int colNum = rs.getMetaData().getColumnCount();
  97.  
  98. for (int i = 0; i < colNum; i++) {
  99.  
  100. columnNames.add(rs.getMetaData().getColumnLabel(i+1));
  101.  
  102.  
  103. }
  104. } catch (SQLException ex) {
  105. Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
  106. }
  107. }
  108. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  109. /**
  110. * Handles the HTTP <code>GET</code> method.
  111. *
  112. * @param request servlet request
  113. * @param response servlet response
  114. * @throws ServletException if a servlet-specific error occurs
  115. * @throws IOException if an I/O error occurs
  116. */
  117. @Override
  118. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  119. throws ServletException, IOException {
  120. processRequest(request, response);
  121. }
  122.  
  123. /**
  124. * Handles the HTTP <code>POST</code> method.
  125. *
  126. * @param request servlet request
  127. * @param response servlet response
  128. * @throws ServletException if a servlet-specific error occurs
  129. * @throws IOException if an I/O error occurs
  130. */
  131. @Override
  132. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  133. throws ServletException, IOException {
  134. processRequest(request, response);
  135. }
  136.  
  137. /**
  138. * Returns a short description of the servlet.
  139. *
  140. * @return a String containing servlet description
  141. */
  142. @Override
  143. public String getServletInfo() {
  144. return "Short description";
  145. }// </editor-fold>
  146.  
  147. }
  148.  
  149. <html>
  150. <head>
  151. <title>WebApp</title>
  152. <meta charset="UTF-8">
  153. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  154. </head>
  155. <body style="background-color:blue;">
  156. <center>
  157. <font color="white">
  158. <h1> Welcome to the Project 4 Remote Database Management System</h1>
  159. <hr>
  160. You are connected to the Project4 database. <br>Please enter any valid SQL query or update statement.<br>
  161. If no query/update command is given the Execute button will display all supplier information in the database. <br>All execution results will appear below.
  162. <br>
  163. <br>
  164. <form action="NewServlet" method="post">
  165. <textarea rows="10" cols="60"name="command"></textarea>
  166. <br>
  167. <button type="submit">Execute Query</button>
  168. <button type="submit">Clear Command</button>
  169. </form>
  170. <hr>
  171. <h1>Database Results</h1>
  172.  
  173. <%
  174. DO TABLE STUFF HERE TO OUTPUT SQL RESULTS
  175. %>
  176.  
  177. </font>
  178. </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement