JDBC problems

Discussions

Web tier: servlets, JSP, Web frameworks: JDBC problems

  1. JDBC problems (6 messages)

    I am new to Java. I have been trying to run an exercise from a book but can't get it to run. All it does is do a simple insert to an Access DB. The servlet is found and running because a user defined error message prints to the browser screen when an attempt is made to "touch" the DB with the INSERT statement. I am using the following connect code:

    private String URL = "jdbc:odbc:GuestBook";
    try
    {
      Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
      connection = DriverManager.getConnection( URL, "", "" );
    }
    No error comes up at this point.

    I have a User DSN named "GuestBook" defined from the ODBC control panel.

    The code runs fine until it reaches the try{} block with the INSERT statement:

    try
    {
      statement = connection.createStatement();
      statement.execute(stringtoinsert);
      statement.close();
    }

    I have pasted the stringtoinsert into the Access query window and executed it so I know the syntax is correct.

    I have to believe there is a driver problem here. Any ideas? Thanks.

    Threaded Messages (6)

  2. JDBC problems[ Go to top ]

    I would also add that I have done another example from the same book that creates a Java application using JDBC on an Access database and it works fine so I must back peddle here and say that JDBC drivers may not be the problem. This may be a problem with the Web Server software layer not acting properly.
  3. JDBC problems[ Go to top ]

    I wud like to know how r u trying to insert - can u post the string u r trying to insert Maybe the problem is with the string - double quote etc.
  4. JDBC problems[ Go to top ]

    //Here is the .java file

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    import java.sql.*;

    public class GuestBookServlet extends HttpServlet {
       private Statement statement = null;
       private Connection connection = null;
       private String URL = "jdbc:odbc:GuestBook";

       public void init( ServletConfig config )
          throws ServletException
       {
          super.init( config );

          try {
             Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
             connection = DriverManager.getConnection( URL, "", "" );
          }
          catch ( Exception e ) {
             e.printStackTrace();
             connection = null;
          }
       }

       public void doPost( HttpServletRequest req, HttpServletResponse res )
          throws ServletException, IOException
       {
          String email, firstName, lastName, company;
          String snailmailList, cppList, javaList, vbList;
          String iwwwList, qString;

          email = req.getParameter( "Email" );
          firstName = req.getParameter( "FirstName" );
          lastName = req.getParameter( "LastName" );
          company = req.getParameter( "Company" );
          snailmailList = req.getParameter( "mail" );
          cppList = req.getParameter( "c_cpp" );
          javaList = req.getParameter( "java" );
          vbList = req.getParameter( "vb" );
          iwwwList = req.getParameter( "iwww" );

          PrintWriter output = res.getWriter();
          res.setContentType( "text/html" );

          if ( email.equals( "" ) || firstName.equals( "" ) || lastName.equals( "" ) ) {
             output.println( "<H3> Please click the back button and fill in all fields.</H3>" );
             output.close();
             return;
          }

    qString = "INSERT INTO GuestBook values (" +
    "'" + email + "','" + firstName + "','" + lastName +
             "','" + company + "',' ',' ',' ',' ',' ','" +
             ( snailmailList != null ? "yes" : "no" ) + "','" +
             ( cppList != null ? "yes" : "no" ) + "','" +
             ( javaList != null ? "yes" : "no" ) + "','" +
             ( vbList != null ? "yes" : "no" ) + "','" +
             ( iwwwList != null ? "yes" : "no" ) + "');";
    //output.println(qString);
          boolean success = insertIntoDB(qString, res);

          if ( success )
             output.print( "<H2>Thank you for registering.</H2>" );
          else
    output.println(System.err);
             output.println( "<H2>An error occurred. Try again later.</H2>" );

          output.close();
       }

       private boolean insertIntoDB( String stringtoinsert, HttpServletResponse resp )
    throws ServletException, IOException
       {
    PrintWriter output = resp.getWriter();
           resp.setContentType( "text/html" );
    output.println(stringtoinsert + "\n");

          try {
             output.println("\ncreateStatement attempted");
             statement = connection.createStatement(); // FAILS HERE
             output.println("\ncreateStatement successful");

             output.println("\nexecute attempted");
             statement.execute( stringtoinsert );
             statement.close();
          }
          catch ( Exception e ) {
             System.err.println( "ERROR: Problems with adding new entry" );
             e.printStackTrace();
             return false;
          }

    output.close();
          return true;
       }

       public void destroy()
       {
          try {
             connection.close();
          }
          catch( Exception e ) {
             System.err.println( "Problem closing the database" );
          }
       }
    }

  5. JDBC problems[ Go to top ]

    //Here is the .html file

    <HTML>
    <HEAD>
       <TITLE>Deitel Guest Book Form</TITLE>
    </HEAD>

    <BODY>
       <H1>Guest Book</H1>
       <FORM
          ACTION=http://localhost:8080/servlet/GuestBookServlet
          METHOD=POST><PRE>
          * Email address: <INPUT TYPE=text NAME=Email>
          * First Name: <INPUT TYPE=text NAME=FirstName>
          * Last name: <INPUT TYPE=text NAME=LastName>
          Company: <INPUT TYPE=text NAME=Company>

                           * fields are required
          </PRE>
          
          <P>Select mailing lists from which you want
          to receive information<BR>
          <INPUT TYPE=CHECKBOX NAME=mail VALUE=mail>
             Snail Mail<BR>
          <INPUT TYPE=CHECKBOX NAME=c_cpp VALUE=c_cpp>
             <I>C++ How to Program & C How to Program</I><BR>
          <INPUT TYPE=CHECKBOX NAME=java VALUE=java>
             <I>Java How to Program</I><BR>
          <INPUT TYPE=CHECKBOX NAME=vb VALUE=vb>
             <I>Visual Basic How to Program</I><BR>
          <INPUT TYPE=CHECKBOX NAME=iwww VALUE=iwww>
             <I>Internet and World Wide Web How to Program</I><BR>
          </P>
          <INPUT TYPE=SUBMIT Value="Submit">
       </FORM>
    </BODY>
    </HTML>

    //Thanks for responding
  6. JDBC problems[ Go to top ]

    ok Howard i have seen your code below and regarding connection interfaces,statement interfaces,everything is correct and it might not give you any problem but my suggesstion please check once insertstatement which you are passing the getparameter variables which you are nullified with strings and singlequotes...
     and check the closing connection also..
  7. JDBC problems[ Go to top ]

    I tried to step through the servlet from the IDE and this message printed to a window >>

    Server threw an exception while running

    java.lang.reflect.InvocationTargetException: com.sun.web.server.HttpServerException: Can not start endpoint on null:8080 due to exception java.net.BindException: Address in use: bind

    at com.sun.web.server.EndpointManager.getEndpoint(EndpointManager.java:183)

    at com.sun.web.server.EndpointManager.startServer(EndpointManager.java:99)

    at com.sun.web.server.HttpServer.start(HttpServer.java:344)null



    at java.lang.reflect.Method.invoke(Native Method)

    at com.borland.jbuilder.runtime.JspTestbed.b(Unknown Source)

    at com.borland.jbuilder.runtime.JspTestbed.main(Unknown Source)

    <

    I don't understand what you ask me to do in your response. Are you telling me to replace the single quotes with double quotes? I printed the INSERT string to the browser window and copied it. I then pasted it into a query window in Access and ran it. The string worked with the single quotes. Thanks to trying to help me.