gpt4 book ai didi

java - 我想给 mysql 提供 JDBC 连接。我不希望在我的类(class) people() 中拥有数据值

转载 作者:行者123 更新时间:2023-11-29 06:21:11 24 4
gpt4 key购买 nike

 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 表。怎么办谁帮帮我。

最佳答案

您可以看看this入门教程。

网上还有大量关于连接 Java 应用程序和 MySQL 的教程。

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

关于java - 我想给 mysql 提供 JDBC 连接。我不希望在我的类(class) people() 中拥有数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3537287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com