Can someone help me with code? It doesn't compile.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ArrayList;
import com.mysql.jdbc.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
/**
* Space. Something for rockets to fly in.
* Modified code from the Capella IT Lab (Dec. 2010) based on
* @author Michael Kolling
* @version 1.1
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 3;
private String player;
private ArrayList<String> topTen;
private Connection conn;
private PreparedStatement stmt;
private static String SERVER_ADDR = "vle8.capella.edu";
private static String USER_NAME = "ab1234567"; // Change to your DB user name
private static String PASSWORD = "ab1234567ab"; // Change to your DB password
private static String DATABASE_URL = "jdbc:mysql://" + SERVER_ADDR + "/" + USER_NAME;
public Space()
{
super(600, 400, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars(300);
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
Explosion.initializeImages();
ProtonWave.initializeImages();
topTen = new ArrayList<String>();
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
* Create a given number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 120 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
background.fillOval(x, y, 2, 2);
}
}
public void changeScore(int pts) {
scoreCounter.add(pts);
}
/**
* This method is called when the game is over to display the final score, save to the DB, and display top 10 scores.
*/
public void gameOver()
{
try {
connectToDB();
saveScore();
getTopTen();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
finally {
addObject(new ScoreBoard(scoreCounter.getValue(), topTen), getWidth()/2, getHeight()/2);
}
}
private void connectToDB() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(DATABASE_URL, USER_NAME, PASSWORD);
}
private void saveScore() throws Exception
{
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String InsertSQL = "INSERT INTO scores(player, score, gamedate) VALUES(?, ?, ?)";
stmt = (PreparedStatement) conn.prepareStatement(InsertSQL);
stmt.setString(1, player);
stmt.setInt(2, scoreCounter.getValue());
String dateTime = format.format(Calendar.getInstance().getTime());
stmt.setString(3, dateTime);
stmt.executeUpdate();
}
private void getTopTen() throws Exception
{
String Top10SQL = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
ResultSet rs = stmt.executeQuery(Top10SQL);
topTen = new ArrayList<String>();
while(rs.next()) {
topTen.add(rs.getInt("score") + " - " + rs.getString("player") + " - " + rs.getString("gamedate"));
}
conn.close();
}
public void setPlayerName(String playerName) {
player = playerName;
}
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
/**
* Crete a given number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 120 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
background.fillOval(x, y, 2, 2);
}
}
/**
* Create a score board with dummy result for testing.
*/
public void ScoreBoard()
{
}
/**
* Create a score board for the final result.
*/
public int ScoreBoard(int score)
{
/**
* Create a score board with dummy result for testing.
*/
{
this(100);
}
/**
* Create a score board for the final result.
*/
public ScoreBoard(int score)
{
makeImage("Game Over", "Final Score: ", score);
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
addObject(new ScoreBoard(999), getWidth()/2, getHeight()/2);
}
}
Thanks