WebPrintVideoAudioMusicClient LoginMember Login
  • Latest Posts
  • Monthly Posts


  • Contact Us:
    315-469-8414


    Contact Form


    4701 Wrightsville Ave #2203
    Wilmington, NC 28403

    Site Links:
    Clients Area
    Members Area
    H.O.W.D.Y. Help!
    H.O.W.D.Y. Tools
    H.O.W.D.Y. Games
    Spammer Info
    Site Map

    Our Network:
    H.O.W.D.Y. Media
    H.O.W.D.Y. Web
    H.O.W.D.Y. Print
    H.O.W.D.Y. Video
    H.O.W.D.Y. Audio
    H.O.W.D.Y. Music
    H.O.W.D.Y. Host
    H.O.W.D.Y. Mail
    H.O.W.D.Y. Photo
    H.O.W.D.Y. Space
    SEO No-No
    Veggie Kisses
    fadishist

    The content of this site is
    ©2000-2024 H.O.W.D.Y Media
    Wordpress Themes
    from $99
    Custom made to match you existing website, 24 Hours of FREE option


    OSCommerce Work
    from $99
    Custom Themes and Development, no task too large or small


    Business Cards
    from $75
    Shipped within 48 hours, Full Color Printing


    Slides or Photos
    4x5" at $5
    Possibly the Lowest Price Online for Scitex Quality!

    Connecting MySQL Databases with Java Applications
    Java — Written by Steve Baldwin on March 7, 2010

    If you are having a hard time finding a *simple* as to why you are not getting anywhere, this should cut through it all in a few minutes. I am assuming that you are a programmer and have moderate knowledge, but even following these steps should have your MySQL accessible from your Java application in no time.

    For this, I’m using NetBeans (6.1) , but the same principles can be used in Eclipse. Since some of you may have already set up your database properly, I’ll save that to last.

    Mysql.java

    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    
    public class Mysql {                                   // Class Declaration
    private Connection conn = null;
    private Statement st = null;
    private ResultSet rs = null;
    
    public Mysql(){                                     // Constructor Class
    try{
    System.out.println(”Attempting to add driver…”);
    Class.forName(”com.mysql.jdbc.Driver”).newInstance();  // Load the driver
    System.out.println(”Driver added successfully.”);
    }catch(Exception ex){
    System.out.println(”Exception: ” + ex.getMessage());
    }
    try{
    System.out.println(”Attempting to connect…”);
    conn = DriverManager.getConnection(
    “jdbc:mysql://YOUR_IP_ADDRESS:3306/YOUR_DATABASE_NAME”,
    “YOUR_MYSQL_USER”,”YOUR_MYSQL_USER_PASSWORD”);
    System.out.println(”Connection was made successfully.”);
    }catch(SQLException ex){ // Note - SQLException!
    System.out.println(”SQLException: ” + ex.getMessage());
    System.out.println(”SQLState: ” + ex.getSQLState());
    System.out.println(”VendorError: ” + ex.getErrorCode());
    }
    }
    

    That should do the trick on the code side, but now you have one very important step! You must make the driver available to your application. Simply copy the driver to your project folder and under the ‘Projects’ tab in NetBeans, add it to your libraries.

    To do this, find the location of the MySQL Connector/J driver. By clicking on the ‘Services’ tab, open Databases, open Drivers, and right click on MySQL (Connector/J driver), and finally select ‘Customize’. This will tell you the path. Mine was located at:

    C:\Program Files\NetBeans 6.1\ide9\modules\ext\mysql-connector-java-5.1.5-bin.jar

    Now that you have the tough part taken care of, you should be able to run the application above and get positive feedback.

    If you are getting driver connection errors, then you will have go through the steps above to add the driver jar to your project library.

    If you are getting MySQL connection errors, then you most likely have an issue with your MySQL setup:

    1. Make sure that the database name, user, and password are all correct. One easy step is to create a small PHP script to test the connection:

    db.php

    mysql_connect('HOST','DB_USER','DB_USER_PASS') or die('Connection failed!');

    2. If that script failed, then you haven’t set up your database access correctly. Recreate or create a new user and try the new access credentials with both scripts.

    3. If you still can’t get this, are you trying to access a database that isn’t ‘localhost’? Remember, most hosts will force you to add an exception to allow database access from an outside location. Make sure that the IP address of the application (on your computer or the server you are deploying to) is added to the allowable hosts.

    If none of this helps, you can stop trying to find the solution with your MySQL server and your application code as it probably is all good. Your problem probably lies elsewhere.

    Comments (0)