Introductory JDBC Example
/* You'll need to make sure that ~karl/public/270/mysql.jar is in
* your CLASSPATH for this to work. You can do this by issuing
* the following commands from a shell:
*
* If you are running csh or tcsh as your shell:
*
* setenv KARL ~karl
* setenv CLASSPATH $KARL/public/270/mysql.jar:.
*
* If you are running bash as your shell:
*
* export CLASSPATH=/Net/solen/home/w/o/karl/public/270/mysql.jar:.
*
* To have these commands run each time you use your shell, you can
* put them in your .cshrc file (if you are using csh or tcsh) or in
* your .bashrc (if you are using bash)
*/
import java.sql.*;
public class Decades {
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
// new com.mysql.jdbc.Driver();
Connection c = DriverManager.getConnection
("jdbc:mysql://mcs-mysql.gac.edu/dummy_movies",
"dummy", "(password goes here)");
Statement stmt = c.createStatement();
for(int decadeStart = 1920; decadeStart < 2000; decadeStart += 10){
System.out.println("==== Movies of the " + decadeStart + "s ====");
ResultSet rs = stmt.executeQuery
("select title, year_made from movies "
+ "where year_made >= " + decadeStart
+ " and year_made < " + (decadeStart + 10));
while(rs.next()){
System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")");
}
}
} catch(Exception e){
System.err.println(e);
}
}
}