wood burning stoves 2.0*
The moose likes JDBC and the fly likes JDBC/servlet connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC/servlet connection" Watch "JDBC/servlet connection" New topic
Author

JDBC/servlet connection

rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
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
Robert Brunner
Ranch Hand

Joined: Jul 18, 2001
Posts: 49
First, you should do this with JSP not Servlets. Second, you look to be on the right track. I suggest you look at the example JSP pages that come with tomcat:
http://jakarta.apache.org/tomcat/index.html
if you want more detailed information.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: JDBC/servlet connection
 
Similar Threads
Reading uploaded file in jsp
can anybody help me
servlet-jdbc - internal servlet error (error:500)
Strange Problem with servlet output
Displaying using constructor