开发者

I want to give JDBC connection to mysql. I dont wish have the data values inside my class people()

开发者 https://www.devze.com 2023-01-12 07:26 出处:网络
import java.util.*; import org.directwebremoting.util.Logger; public class People { public People() { people = new HashSet();
 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 getAllPe开发者_如何学Goople()
{
    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);
}
}

I dont want these data values FIRSTNAMES,TOWNS,ROADS2,ROADS1,SURNAMES inside the program rather i want to connect it to mysql table. How to do that anyone help me.


You can take a look at this introductory tutorial.

There are tons of tutorials online as well regarding connecting Java applications and MySQL.

On the other hand, if you want to go that extra step forward, you might want to take a look at ORM Tools like Hibernate. It basically allows your Java application to connect to a database (just like JDBC). The advantage of such framework tough is that it allows you to change the Database you are using without having to make drastic changes to your application. Not to mention that it automatically maps data stored inside the database to objects and the other way round.

0

精彩评论

暂无评论...
验证码 换一张
取 消