java - 使用 Java/JDBC 实现的 MySQL DB - 基本设计查询

标签 java mysql jdbc database-design

我必须用 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 个表。我不确定它是否遵循正确的设计原则。我遵循了提供示例代码的教程,但将其全部放在主要方法中。我当然不想这样做。

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();
    }
}

最佳答案

如果您正在寻找涉及数据库连接的 Java 程序架构,那么 DAO 是正确的模式。试试这个 http://www.oracle.com/technetwork/java/dataaccessobject-138824.html

关于java - 使用 Java/JDBC 实现的 MySQL DB - 基本设计查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325827/

相关文章:

java - 如何在 JScrollPane 中显示 JPanel?

java 。 "Game Loop"理论帮助

java - 将 JMS 监听器重新连接到 JBossMQ

Java Playwright 使用代理连接实现无浏览器

mysql - 如何使用mysql在count函数中编写select查询

mysql - 查找客户一起购买的产品

java - 加载驱动和注册驱动的区别

mysql - 用另一个表的结果更新一个表中的值

java - 在保存、更新和删除时发出事件的 DAO

java - 如何将 IF NOT EXISTS 语句用于 ALTER TABLE 语句?