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://localhost/movies";
String user="root";
String pw="daytek";
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;
}
}
}
Sorry for not posting anything very direct. This is a COMPILED code that i can't get to work. I have a active database on my computer right now as well.
when i run the code the black box pops up and gives me this...
com.mysql.jdbc.Driver
Press any key to Continue....
I think my driver is setup wrong and if so I would like to ask help with that part of it. Or I am entering the wrond URL or something
Thank You
roba