Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import javax.servlet.Servlet;
  2. import javax.servlet.ServletConfig;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.*;
  10.  
  11. public class Main extends HttpServlet {
  12.  
  13.     private Connection connection;
  14.     private Statement statement;
  15.     private ResultSet resultSet;
  16.     private ResultSetMetaData metaData;
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.     }
  21.  
  22.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  23.             IOException {
  24.  
  25.         // Set the content type
  26.         response.setContentType("text/html");
  27.         // Set the PrintWriter
  28.         PrintWriter out = response.getWriter();
  29.  
  30.         // Get the query from the HTML document
  31.         String query = request.getParameter("sqlStatement").toLowerCase();
  32.         // Split the query into an array
  33.         String[] querySplit = query.split(" ", 2);
  34.         // Get the first word to use later
  35.         String firstWord = querySplit[0];
  36.  
  37.         // Create the connection to the database
  38.         init();
  39.  
  40.         // Check if the first word of the query is "select"
  41.         // Execute a query if so
  42.         if (firstWord.equals("select")) {
  43.             try {
  44.                 setQuery(query);
  45.                 destroy();
  46.             } catch (SQLException sql) {
  47.                 sql.printStackTrace();
  48.             }
  49.             // Otherwise, perform an update
  50.         } else {
  51.             try {
  52.                 setUpdate(query);
  53.                 destroy();
  54.             } catch (SQLException sql) {
  55.                 sql.printStackTrace();
  56.             }
  57.         }
  58.  
  59.     }
  60.  
  61.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
  62.             IOException {
  63.  
  64.         response.setContentType("text/html");
  65.         PrintWriter out = response.getWriter();
  66.  
  67.     }
  68.  
  69.     // Method to create a connection to the SQL database
  70.     public void init() {
  71.         try {
  72.             Class.forName("com.mysql.jdbc.Driver");
  73.             connection = DriverManager.getConnection("jdbc:mysql://localhost:3310/project4", "root",
  74.                     "root");
  75.             statement = connection.createStatement();
  76.         } catch (Exception e) {
  77.             e.printStackTrace();
  78.             e.getMessage();
  79.         }
  80.  
  81.     }
  82.  
  83.     // DESTROY ALL THE THINGS (or at least the connection things)!!!
  84.     public void destroy() {
  85.         try {
  86.             statement.close();
  87.             connection.close();
  88.         } catch (SQLException sql) {
  89.             sql.printStackTrace();
  90.         }
  91.     }
  92.  
  93.     public void setQuery(String query) throws SQLException {
  94.         try {
  95.             // Get the result set from the data
  96.             resultSet = statement.executeQuery(query);
  97.         } catch (SQLException sql) {
  98.             sql.printStackTrace();
  99.         }
  100.     }
  101.  
  102.     public void setUpdate(String query) throws SQLException {
  103.         int res;
  104.         try {
  105.             res = statement.executeUpdate(query);
  106.         } catch (SQLException sql) {
  107.             sql.printStackTrace();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement