Results 1 to 6 of 6
  1. #1
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default How do I check if a database exists ...Any help?

    I wish to write a code which checks if a database exists,if it doesn't it create that database otherwise it proceeds to query or update the database.Any help?

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    Small confusion in ur question.Is it database or table ?
    Ramya:cool:

  3. #3
    nmvictor is offline Member
    Join Date
    Oct 2009
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Small confusion in ur question.Is it database or table ?
    Actually its both, database and table.I'll apreciate

  4. #4
    Amit Kulkarni is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default

    Please, any of you get the answer for this please reply me.....
    Thanks...

  5. #5
    Karsten is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Thumbs up

    Java Code:
    try {
        this.conn = DriverManager.getConnection(...);
        databaseFound = true;
    } catch (SQLException e) {
        databaseFound = false;
        ...
    }
    If db doesn't exist, you get this exception.
    If you get this exception, the reason may be different.
    Any better ideas?
    Regards

  6. #6
    Karsten is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Forget my previous reply. Here comes the correct solution.
    You have to check for your database's SQLStates. (I checked it for Derby DB.)
    Java Code:
        public static boolean checkDBExistence() throws KBException {
    	long msStart = System.currentTimeMillis();
    	String methodName = "checkDBExistence()";
    	logger.debug(methodName + " Start");
    		
    [B]    	boolean flag = false;
    [/B]	try {
        		String url = PROTOCOL + DBNAME;
        		//String url = PROTOCOL + DBNAME + "x"; // for test
    [B]		DriverManager.getConnection(url);
    		flag = true;
            } catch (SQLException e) {
            	if (e.getSQLState().equals("XJ004")){ // DB not found
            		flag = false;[/B]
            	} else
            		throw new KBException("DatabaseMeta2", 
            				methodName, 1, 
            				e.getMessage(), null);
    	}
    
            long msEnd = System.currentTimeMillis();
    	logger.debug(methodName + " Time elapsed: " 
    			+ Toolbox.timeElapsed(msStart, msEnd));
    	[B]return flag;[/B]
        }
    Regards

Similar Threads

  1. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 02:43 AM
  2. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM
  3. How to check whether file is exists or not
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  4. Check if a web page exists or not
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:24 PM
  5. Replies: 1
    Last Post: 01-10-2008, 01:18 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
  •