Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <%@page import="java.sql.*"%>
  2. <%
  3. Connection con;
  4. // Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  5. Class.forName("com.mysql.jdbc.Driver").newInstance();
  6. out.println("Connecting...\n <br>");
  7. try {
  8. // Open Database connection
  9. con = DriverManager.getConnection("jdbc:mysql://db1.cs.uakron.edu:3306/ISP_scs102","scs102","passscs102");
  10. out.println("Connection established.\n <br>");
  11.  
  12. //Variables
  13. String id = request.getParameter("id");
  14. String typ = request.getParameter("type");
  15. String miles = request.getParameter("miles");
  16. String year = request.getParameter("year");
  17. String state = request.getParameter("state");
  18. String statement = request.getParameter("statement");
  19. String action = request.getParameter("action").toString();
  20.  
  21. //Set Variable to Values if none are provided
  22. if ("".equals(id))
  23. id = "0";
  24. if ("".equals(typ))
  25. typ = "N/A";
  26. if ("".equals(miles))
  27. miles = "0.0";
  28. if ("".equals(year))
  29. year = "0";
  30. if ("".equals(state))
  31. state = "N/A";
  32.  
  33. // Query Database (all queries use the same connection)
  34. // Set the query to sqll based on choice
  35. String sqll = "";
  36. if ("display".equals(action))
  37. {
  38. sqll = "SELECT * FROM Corvettes";
  39. }
  40. else if ("insert".equals(action))
  41. {
  42. sqll = "insert into Corvettes values(" + id + ", " + typ + ", " + miles + ", " + year + ", " + state + ")";
  43. }
  44. else if ("update".equals(action))
  45. {
  46. sqll = "update Corvettes set Body_style = " + typ + ", Miles = " + miles + ", Year = " + year + ", State = " + state + " where Vette_id = " + id + "";
  47. }
  48. else if ("delete".equals(action))
  49. {
  50. sqll = "delete from Corvettes where Vette_id = " + id;
  51. }
  52. else if ("user".equals(action))
  53. {
  54. sqll = "" + statement + "";
  55. }
  56.  
  57. Statement stmt = con.createStatement();
  58. ResultSet rs = stmt.executeQuery(sqll);
  59.  
  60. ResultSetMetaData rsmd = rs.getMetaData();
  61. int columncount = rsmd.getColumnCount();
  62. out.println("<table border=1>");
  63. out.println("<tr>");
  64. for (int x = 0; x < columncount; x++)
  65. {
  66. out.println("<th>" + rsmd.getColumnLabel(x + 1) + "</th>");
  67. }
  68. out.println("</tr>)");
  69. while (rs.next())
  70. {
  71. out.println("<tr>");
  72. for (int i = 1; i <= columncount; i++)
  73. {
  74. out.print("<td>" + rs.getString(i) + " " + "</td>");
  75. if (i == columncount)
  76. out.println("<br>");
  77. }
  78. out.println("</tr>");
  79. }
  80. out.println("</table>");
  81.  
  82.  
  83. // Finished using the database instances
  84. rs.close();
  85. stmt.close();
  86. con.close();
  87. }
  88.  
  89. catch (Exception e) {
  90. out.println(e.toString()); // Error message to display
  91. }
  92.  
  93. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement