I want to create a database that stores information about customer choices.Like,my html page contains a choice of text books.Corresponding to the selection the user made, i want to insert the isbn number of the book in the database.
Iam not sure if it is the correct way of doing it.
import java.io.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class cookie1 extends HttpServlet
{
private Connection connection;
private JTable table;
String url="jdbc
dbc:book";
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
String isbn[]={"1234","34234","45362","78960"};
String name[]={"java","c++","c","oracle"};
String data=request.getParameter("data");
Cookie c=new Cookie("java","isbn");
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
response.addCookie(c);
pw.println("<html><head><title>");
pw.println("cod cookies");
pw.println("</title></head><body>");
pw.println("
welcome to cod cookies");
pw.println("
");
pw.println("dexter is a good dog");
pw.println("</body></html>");
pw.close();
}//end of dopost
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter pw;
Cookie cookies[];
cookies=request.getCookies();
response.setContentType("text/html");
pw=response.getWriter();
pw.println("<html><head><title>");
pw.println("cod cookies");
pw.println("</title></head><body>");
pw.println("</body></html>");
pw.close();
}
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url);
}
catch(ClassNotFoundException cnf)
{
System.out.println("no class");
}
catch(SQLException sqle)
{
System.out.println("no db");
}
try
{
Statement s=connection.createStatement();
String q="insert into choices(bookname, isbnnumber)values('1000','norrisio')";
connection.nativeSQL(q);
int result=s.executeUpdate(q);
if(result==1)
System.out.println("successfull");
else
System.out.println("unsuccessfull");
s.close();
}
catch(SQLException sqle)
{
System.out.println("failed access");
}
}
}
}//end of class