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