Results 1 to 4 of 4
  1. #1
    hylee101001 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default trouble with connecting database in a servlet

    I tried to connect DB in a servlet. So I made ConnecDB function like below, and this funciton will be called in doPost method of the servlet. However, this function does not connect to DB. Do you know why?

    I don't understand because when I run exact same function in another class's main, it works.



    public static void ConnectDB(){
    Connection c =null;

    try{
    String connectionString = "jdbc:hsqldb:hsql://localhost:9001";
    c = DriverManager.getConnection(connectionString); //connect!!
    }// end of try
    catch(SQLException e){
    System.out.println("\n--- SQLException caught ---\n");
    while (e != null)
    {
    System.out.println("Message: " + e.getMessage());
    System.out.println("SQLState: " + e.getSQLState());
    System.out.println("Vendor ErrorCode: " + e.getErrorCode());
    e = e.getNextException();
    System.out.println();

    }
    }// end of catch

    catch(Exception e){
    e.printStackTrace();

    }// end of catch
    finally{
    if (c !=null){
    try{
    c.close();
    }
    catch(Exception e2){
    //do nothing - can't do anything anyway.
    }
    }// end of if
    }// end of finally
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: trouble with connecting database in a servlet

    What errors/exceptions do you get?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,184
    Rep Power
    20

    Default Re: trouble with connecting database in a servlet

    Until we see the exception there's little point preempting the cause of the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    nicephotog is offline Member
    Join Date
    Mar 2012
    Posts
    41
    Rep Power
    0

Similar Threads

  1. Trouble connecting to my database,
    By KyleS in forum JDBC
    Replies: 1
    Last Post: 02-22-2012, 10:34 AM
  2. connecting to a database
    By mxb7642 in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 09:39 AM
  3. connecting to a database
    By kswiss in forum NetBeans
    Replies: 4
    Last Post: 06-25-2009, 07:22 PM
  4. Connecting JSP with database
    By ramachandran in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 05-13-2009, 07:15 AM
  5. Connecting to a database
    By peiceonly in forum New To Java
    Replies: 2
    Last Post: 04-06-2008, 03:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •