gpt4 book ai didi

java - 将 ArrayList 添加到 SQL 数据库? (文件管理)

转载 作者:行者123 更新时间:2023-11-30 22:08:41 26 4
gpt4 key购买 nike

我正在为我的数据结构编程课做大学作业。我们正在为这项任务分组工作,所以我目前正在处理我的一个队友项目。我添加了本地 READ/WRITE 代码,这样我就可以将数组列表保存到本地 .dat 文件中。但是,对于额外的标记,我可以进行高级文件处理。

我决定采用 MySQL 方法。

该程序是一个篮球运动员 CRUD 系统,您可以在其中添加一名球员、他们所在的球队、地址、姓名和号码。

我有 5 个类(class)。一个教练类(目前什么都不做),一个球员类和一个团队类。这些类只有 set、get 和 toString 方法。

最后两个类是 BasketBallSystem 类,它具有所有 GUI、方法、菜单等。

package ie.wit.basketball;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class BasketballSystem {

public ArrayList<Team> getTeams() {
return teams;
}

private ArrayList<Team> teams = new ArrayList<>();

public BasketballSystem() throws Exception {
teams=BasketballFile.ArrayListFromFile();
if (teams.size() == 0)
{ Team t = new Team("Golden State Warriors", 10);
teams.add(t);

t.getPlayers().add(new Player("Donna Kinsella", 666));
t.getPlayers().add(new Player("Tommy White", 21));

t.setTheCoach(new Coach("Tom Jones"));

for (Player p : t.getPlayers()) {
System.out.println(p.getPlayerName());
}

t = new Team("Florida Falcons", 10);
t.getPlayers().add(new Player("Don Keating", 76));
t.getPlayers().add(new Player("Tom Green", 22));
teams.add(t);
}

}

public int menuMain() {

JLabel addPlayer = new JLabel("1. Add a Player");
JLabel listPlayers = new JLabel("2. List all Players");
JLabel updateDetails = new JLabel("3. Update Players Details");
JLabel deletePlayers = new JLabel("4. Delete a player");
JLabel exitSys = new JLabel("5. Exit");
JTextField optionField = new JTextField();

Object[] message = new Object[6];

message[0] = addPlayer;
message[1] = listPlayers;
message[2] = updateDetails;
message[3] = deletePlayers;
message[4] = exitSys;
message[5] = optionField;

JOptionPane.showMessageDialog(null, message);
try{
return Integer.parseInt(optionField.getText());
}
catch(NumberFormatException e)
{
return 5;
}
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// new BasketballSystem();

int choice;

BasketballSystem b = new BasketballSystem();

do {

choice = b.menuMain();

switch (choice) {
case 1:
b.addPlayer();
break;
case 2:
b.listPlayers();
break;
case 3:
b.updatePlayersDetails();
break;
case 4:
b.removePlayer();
break;
case 5:
JOptionPane.showMessageDialog(null, "Thank you for using this system.");
BasketballFile.ArrayListToFile(b.getTeams());
break;
default:
JOptionPane.showMessageDialog(null, "Invalid Entry, Please try again.");
}
} while (choice != 5);

}

private void updatePlayersDetails() {
// TODO Auto-generated method stub

String playerNumber = new String("Player Number");
JTextField number = new JTextField("");
String playerName = new String("Player Name");
JTextField name = new JTextField("");
String playerAddress = new String("Player Address");
JTextField address = new JTextField("");

String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);

Object message[] = new Object[6];

message[0] = playerNumber;
message[1] = number;
message[2] = playerName;
message[3] = name;
message[4] = playerAddress;
message[5] = address;

int response = JOptionPane.showConfirmDialog(null, message, "Player Details Update",
JOptionPane.OK_CANCEL_OPTION);

if (response == JOptionPane.OK_OPTION) {
try {
int pnum = Integer.parseInt(number.getText());
List<Player> players = teams.get(teamnamescombo.getSelectedIndex()).getPlayers();
Iterator<Player> it = players.iterator();

while (it.hasNext()) {
Player p = it.next();
if (p.getPlayerNumber() == pnum) {
p.setPlayerName(playerName);
p.setPlayerNumber(playerNumber);
p.setPlayerName(playerName);
}
// if (p.getPlayerAddress()==pAdd){
JOptionPane.showMessageDialog(null, "Player's address has been successfully updated");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: Player number invalid");
}
}
}

private void removePlayer() {
// TODO Auto-generated method stub

String playerNumber = new String("Player Number");
JTextField number = new JTextField("");

String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);

Object message[] = new Object[3];

message[0] = playerNumber;
message[1] = number;
message[2] = teamnamescombo;

int response = JOptionPane.showConfirmDialog(null, message, "Player Removal", JOptionPane.OK_CANCEL_OPTION);

if (response == JOptionPane.OK_OPTION) {

int pnum = Integer.parseInt(number.getText());
List<Player> players = teams.get(teamnamescombo.getSelectedIndex()).getPlayers();
Iterator<Player> it = players.iterator();

while (it.hasNext()) {
Player p = it.next();
if (p.getPlayerNumber() == pnum) {
it.remove();
JOptionPane.showMessageDialog(null, "Player has been successfully removed");
}
}
}

}

private void listPlayers() {
// TODO Auto-generated method stub
// String

for (Team t : teams) // for all teams
{

System.out.println("Team: " + t.getTeamName());
for (Player p : t.getPlayers()) // for all player
{
System.out.println(
"Player: " + p.getPlayerName() + " " + p.getPlayerNumber() + " " + p.getPlayerAddress());

}
}
}

private void addPlayer() {
// TODO Auto-generated method stub
// String teamName
String playerName = new String("Player name");
String playerNumber = new String("Player Number");
String playerAddress = new String("Player Address");
JTextField name = new JTextField("");
JTextField number = new JTextField("");
JTextField address = new JTextField("");

String[] teamnames = new String[teams.size()];
for (int i = 0; i < teams.size(); i++)
teamnames[i] = teams.get(i).getTeamName();
JComboBox<String> teamnamescombo = new JComboBox<String>(teamnames);

Object message[] = new Object[7];

message[0] = playerName;
message[1] = name;
message[2] = playerNumber;
message[3] = number;
message[4] = playerAddress;
message[5] = address;
message[6] = teamnamescombo;

int response = JOptionPane.showConfirmDialog(null, message, "Player Entry", JOptionPane.OK_CANCEL_OPTION);

if (response == JOptionPane.OK_OPTION) {


int pnum = Integer.parseInt(number.getText());
Player newPlay = new Player(name.getText(), pnum);
teams.get(teamnamescombo.getSelectedIndex()).getPlayers().add(newPlay);
JOptionPane.showMessageDialog(null, "Player has been successfully Added");
}
{

}
}



}

最后一个类是 BasketBallFile 类,它处理文件管理。

package ie.wit.basketball;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class BasketballFile {

public static void ArrayListToFile(ArrayList<Team> al) throws Exception {


try{
File f=new File("mydata.dat");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(al);

System.out.println("Written to file!");
oos.close();
fos.close();

}catch(Exception e) {
System.out.println("Error: "+e.getMessage());
throw e;
}


}

public static ArrayList<Team> ArrayListFromFile() throws Exception {
ArrayList<Team> al=new ArrayList<Team>();

try{
File f=new File("mydata.dat");
FileInputStream fis=new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
al=(ArrayList<Team>)ois.readObject();

System.out.println("Read from file!");
ois.close();
fis.close();

}catch(Exception e) {
System.out.println("Error: "+e.getMessage());

}

return al;

}


}

我不希望你告诉我具体怎么做,我只是不知道从哪里开始。我已经尝试用谷歌搜索这个,我尝试搜索教程,但我尝试的一切都没有用。我的 Java 知识非常基础,所以如果您能提供帮助,那就太好了。谢谢。

对不起,如果我也没有正确设置格式,这是我第一次使用这个网站。谢谢!

最佳答案

Java 中数据库管理的关键是 Java Persistence API .这是一个很大的话题,我个人只从 Java EE 的上下文中知道它,但是 it also works with Java SE .

仅具备基本的 Java 知识,这可能是一个巨大的飞跃,也许您应该在进入 ORM 的美妙世界之前充分了解关系数据库以及如何使用它们。对于大学作业中的额外分数,我可能只是将读/写转换为随机文件访问,而不是将整个列表一起序列化到文件中。 (这也不容易,但不需要您学习新概念。)

关于java - 将 ArrayList 添加到 SQL 数据库? (文件管理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40830401/

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