【发布时间】:2017-03-12 13:42:06
【问题描述】:
我必须用 Java(国际象棋联赛)实现一个基本的 MySQL 数据库。它有 4 个表:
PLAYER(PlayerName, DateOfBirth, FIDERating, FIDETitle, ClubName∗)
CLUB(ClubName, Address, DateFormed)
GAME(GameID, DatePlayed, BoardNum, Score, MatchID∗, WhitePlayer,
BlackPlayer)
MATCH(MatchID, MatchDate, Venue, Score, WinningClub, LosingClub)
我必须实现存储过程来查询数据库并包含适当的触发器。
我假设我的讲师在我使用 Java 时对正确的面向对象设计原则给予了肯定。
我在将这些原则应用于创建数据库时遇到了麻烦,我是否应该有一个类来创建数据库和表,然后再有一个类来存储执行存储过程和触发器的方法?我还打算制作一个菜单类来处理用户选择,例如选择查询(存储过程)。如果有任何关于在此任务中使用 OOD 原则的指导,我会(非常)乐意听到它。
到目前为止,我有一个类来创建数据库和 1 个表。我不确定它是否符合正确的设计原则。我遵循了提供示例代码的教程,但将其全部放在 main 方法中。我当然不想这样做。
public class ImplementDatabase
{
private String user; // MySQL user account
private String pass; // MySQL account password
private String host; // MySQL host
Connection conn; // application needs to communicate with JDBC driver
Statement st; // issuing commands against the connection is reqiured
Scanner keyboard; // get input from user
/* When instantiated the JDBC driver attempts to load */
public ImplementDatabase()
{
try
{
Class.forName ("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("Could not load the driver");
}
}
public void createDatabase()
{
try
{
this.readLogin();
// prompts user to enter login info to console
this.conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306", user, pass);
this.st = this.conn.createStatement();
int Result = this.st.executeUpdate("CREATE DATABASE ChessLeague");
System.out.println("You are successfully connected to the "
+ "Chess League Database");
}
catch (SQLException ex)
{
Logger.getLogger(ImplementDatabase.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Please check that the login information you"
+ "have provided is correct");
}
}
public void createPlayerTable()
{
try
{
this.st = this.conn.createStatement();
this.st.executeUpdate("CREATE TABLE IF NOT EXISTS PLAYER"
+ "(PlayerName VARCHAR(30)PRIMARY KEY,"
+ "DateOfBirth DATE,"
+ "FIDERating tinyint,"
+ "ClubName FOREIGN KEY fk_club(Clubname) REFERENCES club(ClubName)");
// Create Actor table
}
catch (SQLException ex)
{
Logger.getLogger(ImplementDatabase.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void readLogin()
{
this.keyboard = new Scanner(System.in);
System.out.println("Please enter username:");
this.user = keyboard.next();
System.out.println("\nPlease enter password:");
this.pass = keyboard.next();
System.out.println("\nPlease enter host or ip address:");
this.host = keyboard.next();
}
}
【问题讨论】:
-
Stack Overflow 用于修复代码问题。这个问题离题了,因为它太宽泛了(而且也有点基于意见)。如果您有一些代码要分享,也许人们可以给您具体的指示
-
我明白了,我有一种痛苦的感觉,我应该在软件工程部发帖。我只有一类代码,我不确定它是否符合 OOD。我将编辑我的帖子以包含它。我知道这个帖子是否必须关闭。
-
Java 非常面向对象。 SQL 是非常非 OO 的。每个人都有不同的思考上限。
-
好的,我明白你的意思了!会记住的。
-
请为您的问题提供一个更具描述性的标题;标题应该引起人们的兴趣,而目前它几乎只有标签。
标签: java mysql jdbc database-design