How to make a successful connection to MySQL Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NewJavaProgrammer
    New Member
    • Nov 2008
    • 1

    How to make a successful connection to MySQL Database

    Hi

    Can someone please take me through how to make a connection to a MySQL database using Java in Netbeans.

    I have downloaded the driver from MySQL, unzipped it to my location, and added the driver to the editor. After adding the driver, I can see it in the editor and my database along with the tables.

    However, when I use Java code to make a connection, I get the following exception message:

    Exception:com.m ysql.jdbc.Drive r

    My code is below, it is very simple and basic. However, I would welcome guidance on how to make a proper class where I can call it from the main rather than being place in the main.
    [code=java]
    import java.sql.* // -- this is placed right at the top!


    Connection con = null;

    try
    {
    Class.forName(" com.mysql.jdbc. Driver").newIns tance();
    con = DriverManager.g etConnection("j dbc:mysql://localhost:3306/bank","root","t riumph");

    if(!con.isClose d())
    System.out.prin tln("Successful ly Connected");

    }catch(Exceptio n e){
    System.err.prin tln("Exception: " + e.getMessage()) ;
    }
    }
    [/code]
    Can anyone help me?
    Last edited by Nepomuk; Nov 12 '08, 11:41 PM. Reason: Please use [CODE] tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi NewJavaProgramm er! Welcome to bytes.com!
    Originally posted by NewJavaProgramm er
    ...However, when I use Java code to make a connection, I get the following exception message:

    Exception:com.m ysql.jdbc.Drive r
    I'm sure, that isn't the complete error you get, is it? There should be more information.

    Also, please use the [CODE]...[/CODE] tags when posting code - I've added them for you this time.

    Oh, and if you haven't done so already, please read our Posting Guidelines.

    Greetings,
    Nepomuk (Moderator)

    Comment

    Working...