Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: Does this Connection object have no schema?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Does this Connection object have no schema?

    This is the output of the program given below:
    Java Code:
    Connection made!
    Schema Name:null
    
    Successfully connected to null
    Releasing all open resources ...
    Inside establishConnection(), conn is initialized as null. Then the first statement inside the try block is supposed to establish a connection with the database, and the third statement is then printing the name of the current schema of conn.

    According to the API, getSchema() returns the current schema name or null if there is none.

    This means that there is no schema (I think the schema name is same as the database name) associated with conn? Can anyone suggest if I am correct in my anticipation, and also suggest why is that there is no schema or null associated with conn?

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class ConnectDB {
    
    	private Connection establishConnection() throws SQLException {
    		Connection conn = null;
    		try {
    			conn = DriverManager.getConnection(
    					"jdbc:mysql://localhost:3306/test_final1", "root", "password");
    			System.out.println("Connection made!\n");
    System.out.println("Schema Name:"+conn.getSchema());
    		} catch (SQLException sqle) {
    			System.err.println("SQL Exception thrown while making connection");
    			printSQLException(sqle);
    		}
    		return conn;
    	}
    
    	public static void main(String[] args) {
    		ConnectDB cdb= new ConnectDB();
    		Connection myconn=null;;
    		try{
    			myconn=cdb.establishConnection();
    			if(myconn!=null) System.out.println("Successfully connected to " + myconn.getSchema());
    		}catch (SQLException e) {
    		      ConnectDB.printSQLException(e);
    	    } catch (Exception e) {
    	      e.printStackTrace(System.err);
    	    } finally {
    	      ConnectDB.closeConnection(myconn);
    	    }
    
    	}
    	
    	
    	public static void printSQLException(SQLException ex) {
    	    for (Throwable e : ex) {
    	      if (e instanceof SQLException) {
    	        if (ignoreSQLException(((SQLException)e).getSQLState()) == false) {
    	          e.printStackTrace(System.err);
    	          System.err.println("SQLState: " + ((SQLException)e).getSQLState());
    	          System.err.println("Error Code: " + ((SQLException)e).getErrorCode());
    	          System.err.println("Message: " + e.getMessage());
    	          Throwable t = ex.getCause();
    	          while (t != null) {
    	            System.out.println("Cause: " + t);
    	            t = t.getCause();
    	          }
    	        }
    	      }
    	    }
    	  }
    	
    	
    	
    	 public static boolean ignoreSQLException(String sqlState) {
    		    if (sqlState == null) {
    		      System.out.println("The SQL state is not defined!");
    		      return false;
    		    }
    		    // X0Y32: Jar file already exists in schema
    		    if (sqlState.equalsIgnoreCase("X0Y32"))
    		      return true;
    		    // 42Y55: Table already exists in schema
    		    if (sqlState.equalsIgnoreCase("42Y55"))
    		      return true;
    		    return false;
    		  }
    
    	 
    	 public static void closeConnection(Connection connArg) {
    		    System.out.println("Releasing all open resources ...");
    		    try {
    		      if (connArg != null) {
    		        connArg.close();
    		        connArg = null;
    		      }
    		    } catch (SQLException sqle) {
    		      printSQLException(sqle);
    		    }
    		  }
    	 
    }
    Last edited by Zarah; 04-25-2014 at 11:14 PM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Does this Connection object have no schema?

    You didn't load the driver if I read your code correctly.

    So you should load the Driver class with Class.forName.

    Konrad

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default Re: Does this Connection object have no schema?

    You didn't load the driver if I read your code correctly.

    So you should load the Driver class with Class.forName.

    Konrad
    I did not load the driver because I am using MySQL Connector/J 5.1.30.

    And on this page, it is written,

    "In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. .... Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)"
    I have the path of the Connector/J jar file added in the CLASSPATH system variable (The path does contain spaces, if that matters)

    Moreover, I still tried to load the driver using the following statement. Did not help.

    Java Code:
    try {
    	            // The newInstance() call is a work around for some
    	            // broken Java implementations
    
    	            Class.forName("com.mysql.jdbc.Driver").newInstance();
    	        } catch (Exception ex) {
    	            // handle the error
    	        	ex.printStackTrace(System.err);
    	        }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default Re: Does this Connection object have no schema?

    Use getCatalog.
    Not all drivers provide both catalog and schema data...which can be annoying.
    Zarah likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 02-09-2011, 03:22 PM
  2. Replies: 0
    Last Post: 08-02-2010, 04:41 PM
  3. Close connection object
    By ersachinjain in forum JDBC
    Replies: 1
    Last Post: 05-30-2010, 12:30 AM
  4. Replies: 0
    Last Post: 12-01-2009, 10:01 PM
  5. Getting warnings reported by Connection object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:57 AM

Posting Permissions

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