我想给JDBC连接mysql。我不希望我的班级里面有数据值的人()

时间:2010-08-21 10:37:24

标签: java mysql jdbc

 import java.util.*;
 import org.directwebremoting.util.Logger;
 public class People
{
public People()
{
    people = new HashSet();
    random = new Random();
    log.debug("Generating a new set of random people");
    for(int i = 0; i < 5; i++)
        people.add(getRandomPerson());

}

public Set getAllPeople()
{
    return people;
}

public void setPerson(Person person)
{
    log.debug("Adding person: " + person);
    if(person.getId() == -1)
        person.setId(getNextId());
    people.remove(person);
    people.add(person);
}

public void deletePerson(Person person)
{
    log.debug("Removing person: " + person);
    people.remove(person);
    debug();
}

private Person getRandomPerson()
{
    Person person = new Person();
    person.setId(getNextId());
    String firstname = FIRSTNAMES[random.nextInt(FIRSTNAMES.length)];
    String surname = SURNAMES[random.nextInt(SURNAMES.length)];
    person.setName(firstname + " " + surname);
    String housenum = (random.nextInt(99) + 1) + " ";
    String road1 = ROADS1[random.nextInt(ROADS1.length)];
    String road2 = ROADS2[random.nextInt(ROADS2.length)];
    String town = TOWNS[random.nextInt(TOWNS.length)];
    String address = housenum + road1 + " " + road2 + ", " + town;
    person.setAddress(address);
    float salary = Math.round(10F + 90F * random.nextFloat()) * 1000;
    person.setSalary(salary);
    return person;
}

protected void debug()
{
    Person person;
    for(Iterator it = people.iterator(); it.hasNext(); log.debug(person.toString()))
        person = (Person)it.next();

}

private static synchronized int getNextId()
{
    return nextId++;
}

static Class _mthclass$(String x0)
{
try
{
return Class.forName(x0);
}
catch(ClassNotFoundException x1)
{
throw new NoClassDefFoundError(x1.getMessage());
}
}

private Set people;
private static int nextId = 1;
private Random random;
private static final String FIRSTNAMES[] = {
    "Fred", "Jim", "Shiela", "Jack", "Betty", "Jacob", "Martha", "Kelly", "Luke", "Matt", 
    "Gemma", "Joe", "Ben", "Jessie", "Leanne", "Becky"
};
private static final String SURNAMES[] = {
    "Sutcliffe", "MacDonald", "Duckworth", "Smith", "Wisner", "Iversen", "Nield", "Turton", "Trelfer", "Wilson", 
    "Johnson", "Cowan", "Daniels"
};
private static final String ROADS1[] = {
    "Green", "Red", "Yellow", "Brown", "Blue", "Black", "White"
};
private static final String ROADS2[] = {
    "Close", "Drive", "Street", "Avenue", "Crescent", "Road", "Place"
};
private static final String TOWNS[] = {
    "Birmingham", "Kettering", "Paris", "San Francisco", "New York", "San Mateo",   "Barcelona"
};
private static final Logger log;

static 
{
    log = Logger.getLogger(People.class);
}
}

我不希望这些数据值FIRSTNAMES,TOWNS,ROADS2,ROADS1,SURNAMES在程序内部而不是我想将它连接到mysql表。怎么做才有人帮助我。

1 个答案:

答案 0 :(得分:3)

您可以查看this入门教程。

关于连接Java应用程序和MySQL,还有很多在线教程。

另一方面,如果你想向前迈出一步,你可能想看看像Hibernate这样的ORM工具。它基本上允许您的Java应用程序连接到数据库(就像JDBC)。这种框架的优势在于它允许您更改正在使用的数据库,而无需对应用程序进行重大更改。更不用说它会自动将存储在数据库中的数据映射到对象,反之亦然。