Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Problems connecting to mysql database on network

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems connecting to mysql database on network

    Hi there,

    I'm currently teaching myself Java from a for dummies book. However I have currently got stuck on the section to connect to a mysql database, in the book it assumes that the database is on a local machine however in my case it is on my server. Both machines are running Linux and my workstation has mysql-jdbc installed on it.

    Here is a copy of the code with the user name and password changed
    import java.sql.*;
    import java.text.NumberFormat;
     
    public class ListMovies
    {
      public static void main(String[] args)
      {
        NumberFormat cf = NumberFormat.getCurrencyInstance();
     
        ResultSet movies = getMovies();
        try
        {
          while (movies.next())
          {
    	Movie m = getMovie(movies);
    	String msg = Integer.toString(m.year);
    	msg += ": " +m.title;
    	msg += " (" + cf.format(m.price) + ")";
    	System.out.println(msg);
          }
        }
        catch (SQLException e)
        {
          System.out.println(e.getMessage());
        }
      }
     
      private static ResultSet getMovies()
      {
        Connection con = getConnection();
        try
        {
          Statement s = con.createStatement();
          String select = "Select title, year, price " + "from movie order by year";
          ResultSet rows;
          rows = s.executeQuery(select);
          return rows;
        }
        catch (SQLException e)
        {
          System.out.println(e.getMessage());
        }
        return null;
      }
     
      private static Connection getConnection()
      {
        Connection con = null;
     
        try 
        {
          Class.forName("com.mysql.jdbc.Driver");
          String url = "jdbc:mysql://myserver:3306/movies";
          String user = "user";
          String pw = "password";
          con = DriverManager.getConnection(url, user, pw);
        }
     
        catch (ClassNotFoundException e)
        {
          System.out.println(e.getMessage());
          System.exit(0);
        }
        catch (SQLException e)
        {
          System.out.println(e.getMessage());
          System.exit(0);
        }
        return con;
      }
     
      private static Movie getMovie(ResultSet movies)
      {
        try
        {
          String title = movies.getString("title");
          int year = movies.getInt ("year");
          double price = movies.getDouble("price");
          return new Movie(title, year, price);
        }
        catch (SQLException e)
        {
          System.out.println(e.getMessage());
        }
     
        return null;
      }
     
      private static class Movie
      {
        public String title;
        public int year;
        public double price;
        public Movie(String title, int year, double price)
        {
          this.title = title;
          this.year = year;
          this.price = price;
        }
      }
    }

    however when it is run the output i get is the following
    com.mysql.jdbc.Driver

    I realise that i'm probably missing something really simple but after several hours of looking i can't work it out


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems connecting to mysql database on network

    Print out the full exception stack trace...it has a lot more information than and will give you (and us) a much better sense as to the problem.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems connecting to mysql database on network

    how do I go about doing that? sorry to sound thick!

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problems connecting to mysql database on network

    The simplest way would by replacing the statement:
    System.out.println(e.getMessage());
    in all of your catch blocks with:
    e.printStackTrace();

    The Stack Trace will give you EVERYTHING you need to know about the exception.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems connecting to mysql database on network

    right I think i get it

    I have changed the code to show the following
     catch (ClassNotFoundException e)
        {
          System.out.println(e.getMessage());
          e.printStackTrace();
          System.exit(0);

    and the output that I am receiving is the following

    com.mysql.jdbc.Driver
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
            at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:186)
            at ListMovies.getConnection(ListMovies.java:52)
            at ListMovies.getMovies(ListMovies.java:30)
            at ListMovies.main(ListMovies.java:10)

    Now to hit google to find out what the problem is, assuming this is what the problem is!

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problems connecting to mysql database on network

    Did you download the jdbc driver and add it to your project's classpath?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems connecting to mysql database on network

    Thanks for everyones help so far

    It seems that it is an issue with the jdbc driver not being in the classpath. Now to work out how to get around this problem, there seems to be some debate as to whether or not it's wise to add it into the classpath as it's a global variable.

    the relevent file appears to be /usr/share/java/mysql-jdbc/mysql-connector-java-5.1.18-bin.jar

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems connecting to mysql database on network

    Quote Originally Posted by aussiemcgr View Post
    Did you download the jdbc driver and add it to your project's classpath?
    I'm using a text editor and bash rather than a full blown IDE to do this as my book doesn't cover any editors and they look a bit scary!

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems connecting to mysql database on network

    there seems to be some debate as to whether or not it's wise to add it into the classpath as it's a global variable
    I've never heard a debate (not denyong one does not exist, but please quote sources)...if you wish to deploy your application to other computers and platforms, its best practice to add the library to the class path.

    PATH and CLASSPATH (The Java™ Tutorials > Essential Classes > The Platform Environment)
    Last edited by copeg; May 8th, 2012 at 05:27 PM.

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems connecting to mysql database on network

    I have got it working (well i have got it working to the point that the server rejects the request) by setting $CLASSPATH with the following

    CLASSPATH=.:../:/usr/share/java/mysql-jdbc/*
    export CLASSPATH

    does anyone know how to get $CLASSPATH to be set on boot? because every time I reboot I have to reset $CLASSPATH

    thank you for all your help so far

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problems connecting to mysql database on network

    See the link above - the preferred way of setting the classpath is not through an environmental variable, but by using the -cp command line option to javac and java

Similar Threads

  1. JCombobox connecting to mysql
    By myexternall11 in forum JDBC & Databases
    Replies: 2
    Last Post: February 16th, 2012, 11:04 PM
  2. Replies: 1
    Last Post: December 15th, 2011, 08:29 AM
  3. Error connecting to database
    By bczm8703 in forum JDBC & Databases
    Replies: 9
    Last Post: December 8th, 2011, 01:06 AM
  4. problems with connection to mysql database
    By thepower in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2010, 02:59 AM
  5. Connecting to a database
    By fwashington in forum JDBC & Databases
    Replies: 5
    Last Post: March 15th, 2010, 01:37 PM