Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2003
    Posts
    15

    Unanswered: Testing connection via JSP

    I currently am using the following method:

    public void dbConnect() throws Exception
    {
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection(strURL, strUID, strPWD);
    }
    catch (SQLException e)
    {
    System.out.println("SQLException - [dbConnect]: " + e.getMessage());
    while ((e = e.getNextException()) != null)
    System.out.println(e.getMessage());
    }
    catch (ClassNotFoundException e)
    {
    System.out.println("ClassNotFoundException: " + e.getMessage());
    }
    }


    My problem is that I need to test a connection from my JSP prior to actually executing queries against the connection.

    My initial solution was to have the method return a string with the error (if in fact it errors) and i would simply verify the message returned and decide if i would like to run queries against. But the problem is that in an invalid connection string, i get the exception as expected but the return string never gets returned.

    Is there a method in the DriverManager that allows me to simply test the connection without it blowing up upon an invalid connection string?

    Thanks

  2. #2
    Join Date
    Feb 2002
    Posts
    3

    Re: Testing connection via JSP

    I didn't understand exactly what you need, but if u want to connect to oracle from a jsp, all you have to do is something like this:

    As the "strURL" variable you're using is not defined on your post, I can't tell you if is right or wrong, however, all you need to define in that string is (example):

    JDBC driver name: oracle:thin
    server name: ntsrv1
    port: 1521
    database name: eval
    database user: myuser
    database password: mypwd

    LIKE THIS:

    Class.forName("oracle.jdbc.OracleDriver");
    connection = DriverManager.getConnection("jdbc:oracle:thin:@nts rv1:1521:eval","myuser","mypwd");
    statement = connection.createStatement();
    rs = statement.executeQuery("SELECT * from myTable");

    while (rs.next()) {
    out.println(rs.getString(1)+ " " );
    out.println(rs.getString(2) + "<br>" );
    }
    rs.close();
    statement.close();

    Edit this script with your server values, and it should work. If not, verify that the oracle driver file (ojdbc14.jar i guess) is located in %JAVA_HOME%\jre\lib\ext, or another JVM findable path.

    I also recommend u to use a connection pool, instead of openning and closing a connection every time.

    Hope this helps.

  3. #3
    Join Date
    Aug 2003
    Posts
    15
    Thanks!
    But i do currently use your method, my question was more as to a way to test a connection with out causing an exception error if the connection fails. since an exception does not allow me to carry that infomation back to the jsp page for me to capture it.

    hope that makes sense.

    - L

  4. #4
    Join Date
    Jan 2004
    Posts
    3

    Re: Testing connection via JSP

    You can use the JdbcCkeckup.java script to test your connection and you can then capture the error that it will throw if there is an error.

  5. #5
    Join Date
    Aug 2003
    Posts
    15
    forgive me for not knowing but where can i get a hold of jdbcCkechup.java script?

  6. #6
    Join Date
    Jan 2004
    Posts
    3

    Re: Testing connection via JSP

    We are all learning. It is not a sin if one don't know. Here

    D:\oracle\ora92\jdbc\demo and unzip the zip file to any driectory you will find the file JdbcCheckup.java abd other files as well, compile it and run. Your Oracle home directory maybe different from mine (ie the D.

    ypu can use this as well. Just copy and paste and compile. Remeber to change the user name, password in the connetion string.

    /*
    * This sample can be used to check the JDBC installation.
    * Just run it and provide the connect information. It will select
    * "Hello World" from the database.
    */

    // You need to import the java.sql package to use JDBC
    import java.sql.*;

    // We import java.io to be able to read from the command line

    import java.sql.*;
    import java.math.*;
    import java.io.*;

    class JdbcCheckup
    {
    public static void main (String args [])
    throws SQLException, IOException
    {
    // Load the Oracle JDBC driver
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    // Prompt the user for connect information
    System.out.println ("Please enter information to test connection to the database");
    String user;
    String password;
    String database;

    user = readEntry ("user: ");
    int slash_index = user.indexOf ('/');
    if (slash_index != -1)
    {
    password = user.substring (slash_index + 1);
    user = user.substring (0, slash_index);
    }
    else
    password = readEntry ("password: ");
    database = readEntry ("database (a TNSNAME entry): ");

    System.out.print ("Connecting to orcl database, please wait...");
    System.out.flush ();

    // Connect to the database
    // You can put a database name after the @ sign in the connection URL.

    System.out.println ("Connecting...");
    Connection conn = DriverManager.getConnection(
    "jdbcracleci:@",
    "chukws", "ikechi");
    System.out.println ("You are connected.");

    // Create a statement
    Statement stmt = conn.createStatement ();

    // Do the SQL "Hello World" thing
    ResultSet rset = stmt.executeQuery ("select 'Hello World' from dual");

    while (rset.next ())
    System.out.println (rset.getString (1));

    System.out.println ("Congratulations, your JDBC installation is correct.");

    // close the resultSet
    rset.close();

    // Close the statement
    stmt.close();

    // Close the connection
    conn.close();
    }

    // Utility function to read a line from standard input
    static String readEntry (String prompt)
    {
    try
    {
    StringBuffer buffer = new StringBuffer ();
    System.out.print (prompt);
    System.out.flush ();
    int c = System.in.read ();
    while (c != '\n' && c != -1)
    {
    buffer.append ((char)c);
    c = System.in.read ();
    }
    return buffer.toString ().trim ();
    }
    catch (IOException e)
    {
    return "";
    }
    }
    }

Posting Permissions

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