Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@page import="java.sql.*"%>
- <%
- Connection con;
- // Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- out.println("Connecting...\n <br>");
- try {
- // Open Database connection
- con = DriverManager.getConnection("jdbc:mysql://db1.cs.uakron.edu:3306/ISP_scs102","scs102","passscs102");
- out.println("Connection established.\n <br>");
- //Variables
- String id = request.getParameter("id");
- String typ = request.getParameter("type");
- String miles = request.getParameter("miles");
- String year = request.getParameter("year");
- String state = request.getParameter("state");
- String statement = request.getParameter("statement");
- String action = request.getParameter("action").toString();
- //Set Variable to Values if none are provided
- if ("".equals(id))
- id = "0";
- if ("".equals(typ))
- typ = "N/A";
- if ("".equals(miles))
- miles = "0.0";
- if ("".equals(year))
- year = "0";
- if ("".equals(state))
- state = "N/A";
- // Query Database (all queries use the same connection)
- // Set the query to sqll based on choice
- String sqll = "";
- if ("display".equals(action))
- {
- sqll = "SELECT * FROM Corvettes";
- }
- else if ("insert".equals(action))
- {
- sqll = "insert into Corvettes values(" + id + ", " + typ + ", " + miles + ", " + year + ", " + state + ")";
- }
- else if ("update".equals(action))
- {
- sqll = "update Corvettes set Body_style = " + typ + ", Miles = " + miles + ", Year = " + year + ", State = " + state + " where Vette_id = " + id + "";
- }
- else if ("delete".equals(action))
- {
- sqll = "delete from Corvettes where Vette_id = " + id;
- }
- else if ("user".equals(action))
- {
- sqll = "" + statement + "";
- }
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(sqll);
- ResultSetMetaData rsmd = rs.getMetaData();
- int columncount = rsmd.getColumnCount();
- out.println("<table border=1>");
- out.println("<tr>");
- for (int x = 0; x < columncount; x++)
- {
- out.println("<th>" + rsmd.getColumnLabel(x + 1) + "</th>");
- }
- out.println("</tr>)");
- while (rs.next())
- {
- out.println("<tr>");
- for (int i = 1; i <= columncount; i++)
- {
- out.print("<td>" + rs.getString(i) + " " + "</td>");
- if (i == columncount)
- out.println("<br>");
- }
- out.println("</tr>");
- }
- out.println("</table>");
- // Finished using the database instances
- rs.close();
- stmt.close();
- con.close();
- }
- catch (Exception e) {
- out.println(e.toString()); // Error message to display
- }
- %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement