Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>WebApp</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body style="background-color:blue;">
- <center>
- <font color="white">
- <h1> Welcome to the Project 4 Remote Database Management System</h1>
- <hr>
- You are connected to the Project4 database. <br>Please enter any valid SQL query or update statement.<br>
- 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.
- <br>
- <br>
- <form action="NewServlet" method="post">
- <textarea rows="10" cols="60"name="command"></textarea>
- <br>
- <button type="submit">Execute Query</button>
- <button type="submit">Clear Command</button>
- </form>
- <hr>
- <h1>Database Results</h1>
- </font>
- </body>
- </html>
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Vector;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.swing.JOptionPane;
- /**
- *
- * @author KJ4CC
- */
- public class NewServlet extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- Connection connection;
- Vector<String> columnNames = new Vector<String>();
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- String command = request.getParameter("command");
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- sqlConnection(command);
- //prints out column names into the browser.
- out.println(columnNames);
- }
- }
- public void sqlConnection(String command){
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/project3";
- String user = "root";
- String pass = "Brandy?1994";
- ResultSet rs;
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- connection = DriverManager.getConnection(url,user,pass);
- } catch (SQLException ex) {
- Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- Statement stmt;
- try {
- stmt = connection.createStatement();
- rs = stmt.executeQuery(command);
- int colNum = rs.getMetaData().getColumnCount();
- for (int i = 0; i < colNum; i++) {
- columnNames.add(rs.getMetaData().getColumnLabel(i+1));
- }
- } catch (SQLException ex) {
- Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- <html>
- <head>
- <title>WebApp</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body style="background-color:blue;">
- <center>
- <font color="white">
- <h1> Welcome to the Project 4 Remote Database Management System</h1>
- <hr>
- You are connected to the Project4 database. <br>Please enter any valid SQL query or update statement.<br>
- 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.
- <br>
- <br>
- <form action="NewServlet" method="post">
- <textarea rows="10" cols="60"name="command"></textarea>
- <br>
- <button type="submit">Execute Query</button>
- <button type="submit">Clear Command</button>
- </form>
- <hr>
- <h1>Database Results</h1>
- <%
- DO TABLE STUFF HERE TO OUTPUT SQL RESULTS
- %>
- </font>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement