Results 1 to 9 of 9
  1. #1
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default I need help with MySql linking with my Java application

    I have downloaded and installed Connector/J and added the .jar file to my classpath. However, I try to open a MySql database that I have for experimental purposes. When I run my code which compiles just fine all I get is the string literal from the Class.forName statement in the terminal window. What I am attempting to do is access a line from MySql database MyProfile. Here is code I try:

    import javax.swing.*;
    import java.sql.*;

    public class MyProfile
    {
    public static void main(String[] args)
    {
    new MyProfile();



    }

    public MyProfile()
    {
    try
    {
    Statement s = getConnection().createStatement();
    String select = "Select name, email, paid from profile order by name";
    ResultSet rows = s.executeQuery(select);

    while (rows.next())
    {
    String name = rows.getString(1);
    String email = rows.getString(2);
    double paid = rows.getDouble(3);

    System.out.println(name + email + paid);
    }
    }
    catch (SQLException e)
    {
    System.out.println("Exception caught");

    }

    }

    private static Connection getConnection()
    {
    Connection con = null;
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    String url;
    String user;
    String pw;

    url = "jdbc.mysql://localhost/MyProfile";
    user = "administrator";
    pw = "joel";
    con = DriverManager.getConnection(url, user, pw);
    System.out.println("Opened successfully");

    }
    catch (ClassNotFoundException e)
    {
    System.out.println(e.getMessage());

    System.exit(0);
    }
    catch (SQLException e)
    {
    System.out.println(e.getMessage());

    System.exit(0);

    }
    return con;
    }


    }

    Any help would be greatly appreciated! I really need to know this evolution of Java programming.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    First, in all your catch blocks do e.printStackTrace(), otherwise you'll miss lots of useful error messages.
    Second, show us your output here, word for word.
    Oh, and use code tags for posting code.

  3. #3
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    thank you for your help! I will get on that today. I already tried putting printStack statements in there, and I do get errors ClassNotFoundException. ALso, the output on a Terminal Window is com.mysql.jdbc.Driver.

    Thanks for your help, and BTW I will use tags. Thanks for your help in here, this is a great community for Java.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    ClassNotFoundException
    com.mysql.jdbc.Driver

    You haven't included the Connector/J jar file on the classpath...

  5. #5
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I'm pretty sure I have. I restarted and everything, and the classpath has indeed been changed to the .jar file where the Connector/J driver is located. Here is screenshot:

    Capture.PNG

    Thanks for your replies! Any help on this matter is greatly appreciated!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    No.
    The runtime classpath.
    That error always means "this is not on your classpath".
    How do you run this?
    javac?

  7. #7
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    i compile and run in BlueJ 3.0.4. How would I go about manipulating the runtime classpath, in Java settings?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Presumably there's a setting for dependencies somewhere.
    I have no idea how BlueJ works...it's not like normal IDEs .
    using javac you'd do something like:
    javac -cp <path to jar file>;. myclass
    The bit in bold defines the classpath to use.

    In Eclipse and Netbeans and IntelliJ you give the project the list of jars you are using.

  9. #9
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I see....I will also migrate the code over to Eclipse in a little while and compile it there and see if it works. BlueJ has been the de facto IDE for me since college, so it is more educational and not as feature rich as Eclipse or any others. I understand though that deployable applications should be debugged and comiled on something else. Thanks for your help.

Similar Threads

  1. Replies: 9
    Last Post: 01-17-2012, 08:31 AM
  2. Trouble linking up to MySql database
    By Joel in forum New To Java
    Replies: 2
    Last Post: 05-27-2011, 10:17 PM
  3. Replies: 3
    Last Post: 10-28-2010, 02:42 PM
  4. MySQL application to applet?
    By Natrix in forum Networking
    Replies: 14
    Last Post: 03-27-2010, 10:31 PM
  5. Creating Java EE application with MySQL db
    By [RTS]BN+VS* in forum JDBC
    Replies: 0
    Last Post: 03-25-2009, 01:46 PM

Posting Permissions

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