Bug #12960 DriverManager.getConnection can throw a java.net.UnknownHostException
Submitted: 3 Sep 2005 2:59 Modified: 3 Sep 2005 12:25
Reporter: Claude Lamy Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.17 OS:Windows (Windows)
Assigned to: CPU Architecture:Any

[3 Sep 2005 2:59] Claude Lamy
Description:
I am trying to implement a mySQL data base interface where the host will be entered by the user.  Since the host can be valid or not, I would like to catch the exception in the later case to report it to the user.  The following code does not compile with netbeans 4.1 (exception java.net.UnknownHostException is never thrown in body of corresponding try statement).  If I change the exception type to Exception, the exception thrown by getConnection is not caught and the application crashes.  All of this is probably because getConnection is not identified as it can throw a UnknownHostException.  getConnection throws in deed a UnknownHostException in the case of an unknown host.

How to repeat:
/*
 * DataModule.java
 *
 * Created on August 28, 2005, 10:36 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package webgenealogie.DataModule;

/* import needed for JDBC access */
import java.sql.*;
import java.net.*;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author Parents
 */
public class DataModule {
    
    private Connection con = null;
    private Statement stmt = null;
    private String strLocalHostName = null;
    private String strWWWHostName = null;
    private String strDataBaseName = null;
    private String strUserName = null;
    private String strPassword = null;
    private String strConnectedServer = null;
    
    /** Creates a new instance of DataModule */
    public DataModule(String WWWHostName, String LocalHostName, String DataBaseName, String UserName, String Password)
    {
        strWWWHostName = WWWHostName;
        strLocalHostName = LocalHostName;
        strDataBaseName = DataBaseName;
        strUserName = UserName;
        strPassword = Password;
    }
    
    protected void finalize() throws Throwable
    {
        Disconnect();
    }
    
    public void Disconnect()
    {
       try
       {
           if ( con != null)
           {
               con.close();
               con = null;
           }
       }
       catch (SQLException e)
       {
           System.out.println("Error disconnecting...");
       }
    }

    public void ConnectToServer() throws Exception
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String strURL = "jdbc:mysql://" + strLocalHostName + ":3306/" + strDataBaseName;
            con = DriverManager.getConnection(strURL, strUserName, strPassword);
            strConnectedServer = strLocalHostName;
        }
        catch (UnknownHostException e)
        {
            System.err.println("Exception: " + e.getMessage() + "  Trying second hostname.");
            try
            {
                String strURL = "jdbc:mysql://" + strWWWHostName + ":3306/" + strDataBaseName;
                con = DriverManager.getConnection(strURL, strUserName, strPassword);
                strConnectedServer = strWWWHostName;
            }
           catch (UnknownHostException ee)
           {
               System.err.println("Exception: " + ee.getMessage());
           }
        }
        if (con != null)
            System.out.println("Successfully connected to " + strConnectedServer);
            
    }
    
    public void ExecuteSQL(String strQuery) throws Exception
    {
	try {
	    Statement s = con.createStatement ( );
	    ResultSet rs = s.executeQuery( strQuery );
            while ( rs.next ( ) )
            {
		String id = ( rs.getObject ("id").toString() );
		String text = ( rs.getObject ("field_name").toString() );
		System.out.println ( "found record : " + id + " " + text );
	    }
			
	    rs.close ( );

	}
        catch ( SQLException e )
        {
	    System.out.println ( "Error executing sql statement" );
	    throw ( e );
	}
    }

}    

Suggested fix:
Mark the DriverManager.getConnection that can throw a UnknownHostException.
[3 Sep 2005 12:25] Mark Matthews
DriverManager.getConnection() is a standard interface defined in the JDBC API, and you are not allowed to extend the signature.

You should catch the SQLException that is returned, and determine UnknownHost from that exception, based on the SQLState of the SQLException.
[3 Sep 2005 12:27] Mark Matthews
I should be more specific...java.sql.DriverManager is a _class_ that's implemented by Sun and built into the JVM's class library, so the method signature can not be changed, even if we wanted to.