database not exist, how to tell

Discussion in 'Java' started by Liz, Jul 15, 2004.

  1. Liz

    Liz Guest

    I have a java program that will create a MySQL
    database, create a table, and then write data into the table.
    My problem is how to tell if the databasename that
    is given to me already exists or not, because if it
    exists then I can just delete the table and create
    a new one. Otherwise I have to create it too. Right
    now I have this code that works, but is there a way
    to tell existence without using the exception? "filename"
    is passed to me as a parameter.
    ---
    try {
    DriverManager.registerDriver(new Driver());
    Connection conn =
    DriverManager.getConnection("jdbc:mysql://localhost/" + filename);
    } catch (SQLException e1) {
    // probably database doesn't exist (what we want)
    if ((e1.getMessage().indexOf("Unknown database") == -1)) {
    // some other error occurred
    System.out.println("SQLException: " + e1.getMessage());
    System.out.println("SQLState: " + e1.getSQLState());
    System.out.println("VendorError: " + e1.getErrorCode());
    e1.printStackTrace();
    return null;
    }
    System.out.println("Got what we wanted -- file does not exist");
    // do something
    return filename;
    }
    // file exists -- we have to get rid of it
    // do something
    // return filename;
     
    Liz, Jul 15, 2004
    #1
    1. Advertising

  2. Liz

    Liz Guest

    "Liz" <> wrote in message
    news:w%mJc.93871$Oq2.17949@attbi_s52...
    > I have a java program that will create a MySQL
    > database, create a table, and then write data into the table.
    > My problem is how to tell if the databasename that
    > is given to me already exists or not, because if it
    > exists then I can just delete the table and create
    > a new one. Otherwise I have to create it too. Right
    > now I have this code that works, but is there a way
    > to tell existence without using the exception? "filename"
    > is passed to me as a parameter.
    > ---
    > try {
    > DriverManager.registerDriver(new Driver());
    > Connection conn =
    > DriverManager.getConnection("jdbc:mysql://localhost/" + filename);
    > } catch (SQLException e1) {
    > // probably database doesn't exist (what we want)
    > if ((e1.getMessage().indexOf("Unknown database") == -1)) {
    > // some other error occurred
    > System.out.println("SQLException: " + e1.getMessage());
    > System.out.println("SQLState: " + e1.getSQLState());
    > System.out.println("VendorError: " + e1.getErrorCode());
    > e1.printStackTrace();
    > return null;
    > }
    > System.out.println("Got what we wanted -- file does not exist");
    > // do something
    > return filename;
    > }
    > // file exists -- we have to get rid of it
    > // do something
    > // return filename;
    >

    After having posed the above problem, I tried to move on to having
    the application create a database, but I could not figure out how.
    The tutorial sort of implies that it can't be done, but doesn't explicitly
    say so. They start the tutorial assuming the database already exists.
    So is it possible to create a database in java?
     
    Liz, Jul 15, 2004
    #2
    1. Advertising

  3. Liz wrote:

    [snip]
    > After having posed the above problem, I tried to move on to having
    > the application create a database, but I could not figure out how.
    > The tutorial sort of implies that it can't be done, but doesn't explicitly
    > say so. They start the tutorial assuming the database already exists.
    > So is it possible to create a database in java?


    Connect to mysql rather than a specific database. Its not generally
    a good idea to allow DBA stuff and user stuff to be done by the same
    user so you may want to separate this - if you just want temp
    tables just use your current db. Also you will need to use the full
    table name with this connection (or a 'use testdb'). Alternatively I
    guess you could have various batch files that do this / mysqladmin
    commands and either run then before/after your prog, use jni or the
    runtime exec methods.

    String driverName = "com.mysql.jdbc.Driver";
    Class.forName(driverName);
    ....
    String serverName = "localhost";
    String mydatabase = "";
    String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
    ....
    connection = DriverManager.getConnection(url, username, password);
    ....
    int result = stmt.executeUpdate("create database testdb");
    ....

    --
    Andrew
     
    Andrew Harker, Jul 15, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    1,025
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,101
    Phlip
    Jul 25, 2004
  3. yogesh
    Replies:
    1
    Views:
    369
    Victor Bazarov
    Mar 14, 2007
  4. Mike A
    Replies:
    17
    Views:
    263
    Dr John Stockton
    Nov 19, 2003
  5. Markus Mohr
    Replies:
    7
    Views:
    251
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page