1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| package Exercise5;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.sql.*;
import java.lang.*;
public class Inventory1 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
String user = "aglandon";
String pass = "uplykqth";
String dburl="JDBC:mysql://localhost/aglandon";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Mon Inventory</title></head>");
out.println("<body>");
out.println(" <FORM METHOD=\"GET\" ACTION=\"http://136.206.25.35/aglandon/servlet/Exercise5.Inventory\">");
out.println(" <h3>Add/update a product</h3>");
out.println(" ID : <INPUT TYPE=\"text\" NAME=\"ID\" SIZE=\"20\"> <p>");
out.println(" Name : <INPUT TYPE=\"text\" NAME=\"Name\" SIZE=\"20\"> <p>");
out.println(" Description : <INPUT TYPE=\"text\" Description=\"ID\" SIZE=\"20\"> <p>");
out.println(" Price : <INPUT TYPE=\"text\" NAME=\"Price\" SIZE=\"20\"> <p>");
out.println(" Quantity: <INPUT TYPE=\"text\" NAME=\"Quantity\" SIZE=\"20\"> <p>");
try{
String pilote ="com.mysql.jdbc.Driver";
Class.forName(pilote);
}catch(ClassNotFoundException cnfe){
out.println("Driver introuvable : ");
cnfe.printStackTrace();}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
String createQuery="CREATE TABLE IF NOT EXISTS inventory (ProductID INTEGER UNSIGNED PRIMARY KEY, ProductName VARCHAR(30), ProductDescription VARCHAR(40),ProductPrice INTEGER UNSIGNED,Quantity INTERGER UNSIGNED)";
connection = DriverManager.getConnection(dburl,user,pass);
statement = connection.createStatement();
statement.executeUpdate(createQuery);
}
catch(SQLException sqle){}
finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {}
statement = null;
}
if (connection!= null) {
try {
connection.close();
} catch (SQLException e) {}
connection= null;
}
}
try{
String Id="";
Id=request.getParameter("ID");
int id=Integer.parseInt(Id);
String name="";
name=request.getParameter("Name");
String description="";
description=request.getParameter("Description");
String Pryce="";
Pryce=request.getParameter("Price");
int price=Integer.parseInt(Pryce);
String Quantiti="";
Quantiti=request.getParameter("Quantity");
int quantity=Integer.parseInt(Quantiti);
connection = DriverManager.getConnection(dburl,user,pass);
statement = connection.createStatement();
String addElementQuery="INSERT INTO inventory (ProductID, ProductName, ProductDescription, ProductPrice, Quantity) VALUES (" +id+ "," + name + "," + description + ", " + price + ", " + quantity+ ");";
statement.executeUpdate(addElementQuery);
}catch(SQLException sqle){}
finally{
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {}
statement = null;
}
if (connection!= null) {
try {
connection.close();
} catch (SQLException e) {}
connection= null;
}
}
try{
out.println("1");
connection = DriverManager.getConnection(dburl,user,pass);
statement = connection.createStatement();
String selectQuery="SELECT * FROM inventory";
statement.executeQuery(selectQuery);
resultSet = statement.getResultSet();
out.println("<table border=\"1\" cellspacing=\"1\">");
out.println("<TR><TD>Product ID</TD> <TD>Product Name</TD> <TD>Product Description</TD> <TD> Product Price (&eur;) </TD> <TD> Quantity </TD></TR>");
while(resultSet.next()){
out.println("<TR><TD>" + resultSet.getInt("ProductID") + "</TD> <TD>"+ resultSet.getString("ProductName") +"</TD> <TD>"+resultSet.getString("ProductDescription")+"</TD> <TD>"+resultSet.getInt("ProductPrice")+" (&eur;) </TD> <TD>"+ resultSet.getInt("Quantity")+" </TD></TR>");
}
}catch(SQLException sqle){}
finally{if (resultSet != null)
{
try {
resultSet.close();
} catch (SQLException e) {}
resultSet= null;
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {}
statement = null;
}
if (connection!= null) {
try {
connection.close();
} catch (SQLException e) {}
connection= null;
}
}
out.println(" <INPUT TYPE=\"submit\" VALUE=\"Submit\"> <INPUT TYPE=\"reset\" VALUE=\"reset\"> <p>");
out.println("</FORM>");
out.print("</table>");
out.println("</body>");
out.println("</html>");
}
} |
Partager