尝试" SELECT *来自表"时出现Mysql错误

时间:2016-01-15 14:28:48

标签: java mysql

我的代码:

public static void main(String[] args)
{
    WindowMaker("Main Menu", 600, 400);
}

public static void mainMenu(JFrame frame, Container contentPane)
{
    JPanel buttonPanel = new JPanel();
    buttonPanel.setAlignmentX(Component.TOP_ALIGNMENT);
    buttonPanel.setLayout(new GridLayout(0, 1));

    for(int i = 0; i < 3; i++)
    {
        JButton button = new JButton("TEXT");
        if(i == 0)
        {
            button.setText("PLAY");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    playMenu = true;
                    WindowMaker("Play Menu", 600, 400);
                    frame.dispose();
                }
            });
        }
        else if (i == 1)
        {
            button.setText("ADD WORDS");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("ADD");
                }
            });
        }
        else if(i == 2)
        {
            button.setText("VIEW WORDS");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    viewMenu = true;
                    WindowMaker("View Word Banks", 600, 400);
                    frame.dispose();
                }
            });
        }

        button.setAlignmentX(Component.TOP_ALIGNMENT);
        button.setPreferredSize(new Dimension(600, 75));
        buttonPanel.add(button);
    }

    contentPane.add(buttonPanel);
}

public static void playMenu(JFrame frame, Container contentPane)
{
    JPanel playPane = new JPanel();
    playPane.setAlignmentX(Component.TOP_ALIGNMENT);
    playPane.setAlignmentY(Component.CENTER_ALIGNMENT);
    playPane.setLayout(new GridLayout(0, 1));

    JLabel words = new JLabel("Word Bank : " + selected);
    words.setFont(new Font("Title", Font.ROMAN_BASELINE, 25));
    words.setHorizontalAlignment(JLabel.CENTER);
    playPane.add(words);

    words.setFont(new Font("Title", Font.ROMAN_BASELINE, 25));
    words.setHorizontalAlignment(JLabel.CENTER);
    playPane.add(words);
    for(int i = 0; i < 3; i++)
    {

        JButton button = new JButton("TEXT");
        if(i == 0)
        {
            button.setText("GO");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    playing = true;
                    WindowMaker("Playing", 600, 400);
                    frame.dispose();
                }
            });
        }
        else if (i == 1)
        {
            button.setText("SELECT WORD BANK");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    selectMenu = true;
                    WindowMaker("Select Word Bank", 600, 400);
                    frame.dispose();
                }
            });
        }
        else if(i == 2)
        {
            button.setText("BACK");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    mainMenu = true;
                    WindowMaker("Main Menu", 600, 400);
                    frame.dispose();
                }
            });
        }

        button.setAlignmentX(Component.TOP_ALIGNMENT);
        button.setPreferredSize(new Dimension(600, 75));
        playPane.add(button);
    }

    contentPane.add(playPane);
}

public static void viewMenu(JFrame frame, Container contentPane)
{
    JPanel viewPane = new JPanel();
    viewPane.setAlignmentX(Component.TOP_ALIGNMENT);
    viewPane.setAlignmentY(Component.CENTER_ALIGNMENT);
    viewPane.setLayout(new GridLayout(0, 1));

    JLabel table = new JLabel("BLANK");

    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CatchPhrase", "root", "vader123");
        DatabaseMetaData md = (DatabaseMetaData) con.getMetaData();
        ResultSet rs = md.getTables(null, null, "%", null);
        while(rs.next())
        {
            table.setText(rs.getString(3));
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    table.setFont(new Font("Title", Font.ROMAN_BASELINE, 25));
    table.setHorizontalAlignment(JLabel.CENTER);
    table.setVerticalAlignment(JLabel.NORTH);
    viewPane.add(table);
    contentPane.add(viewPane);
}

public static void selectMenu(JFrame frame, Container contentPane)
{
    JPanel selectPane = new JPanel();
    selectPane.setAlignmentX(Component.TOP_ALIGNMENT);
    selectPane.setAlignmentY(Component.CENTER_ALIGNMENT);
    selectPane.setLayout(new GridLayout(0, 1));

    JButton table = new JButton("BLANK");

    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CatchPhrase", "root", "vader123");
        DatabaseMetaData md = (DatabaseMetaData) con.getMetaData();
        ResultSet rs = md.getTables(null, null, "%", null);
        while(rs.next())
        {
            table.setText(rs.getString(3));
            table.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    selected = table.getText();
                    playMenu = true;
                    WindowMaker("Play Menu", 600, 400);
                    frame.dispose();
                }
            });
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    table.setPreferredSize(new Dimension(600, 100));
    table.setMaximumSize(new Dimension(600, 100));
    table.setFont(new Font("Title", Font.ROMAN_BASELINE, 25));
    selectPane.add(table);
    contentPane.add(selectPane);
}

public static void playing(JFrame frame, Container contentPane)
{
    JPanel playPane = new JPanel();
    playPane.setAlignmentX(Component.TOP_ALIGNMENT);
    playPane.setAlignmentY(Component.CENTER_ALIGNMENT);
    playPane.setLayout(new GridLayout(0, 1));

    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CatchPhrase", "root", "vader123");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM DEFAULT");
        while(rs.next())
        {
            System.out.println(rs.getString(1));
        }
        st.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    JLabel time = new JLabel();
    time.setHorizontalAlignment(JLabel.CENTER);
    Runnable ticker = new Runnable() {
        public void run() {
            timer--;
            time.setText("Time : " + Integer.toString(timer));

            if(timer == 0)
            {
                gameOver = true;
                WindowMaker("Game Over", 600, 400);
                frame.dispose();
            }
        }
    };
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(ticker, 0, 1, TimeUnit.SECONDS);

    JLabel word = new JLabel();

    JButton next = new JButton("NEXT");
    next.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            i++;


            if(i == 1)
            {
                word.setText("Crust");
            }
            else if(i == 2)
            {
                word.setText("Core");
            }
            else if(i == 3)
            {
                word.setText("Mantle");
                i = 0;
            }
        }
    });
    playPane.add(time);
    playPane.add(word);
    playPane.add(next);
    contentPane.add(playPane);
}

public static void WindowMaker(String title, int width, int height)
{
    JFrame frame = new JFrame("Catch Phrase - " + title);
    frame.setMinimumSize(new Dimension(width, height));
    frame.setResizable(false);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));

    JPanel panel = new JPanel();
    panel.setAlignmentX(Component.TOP_ALIGNMENT);
    panel.setAlignmentY(Component.CENTER_ALIGNMENT);
    JLabel name = new JLabel("Catch Phrase");
    name.setFont(new Font("Title", Font.ROMAN_BASELINE, 50));
    panel.add(name);
    contentPane.add(panel);

    if(mainMenu)
    {
        mainMenu(frame, contentPane);
        mainMenu = false;
    }
    else if(playMenu)
    {
        playMenu(frame, contentPane);
        playMenu = false;
    }
    else if(viewMenu)
    {
        viewMenu(frame, contentPane);
        viewMenu = false;
    }
    else if(selectMenu)
    {
        selectMenu(frame, contentPane);
        selectMenu = false;
    }
    else if(playing)
    {
        playing(frame, contentPane);
        playing = false;
    }

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

我的错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT' at line 1

我正在研究我的计算机科学最终项目,并且我的数据库连接存在问题。我之前使用过mysql,从来没有遇到过这个问题。我花了几个小时试图解决它,不能再投入了。任何和所有有关正在发生的事情的帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

DEFAULTMySQL reserved word。将您的表重命名为其他内容。

答案 1 :(得分:2)

您可能需要修改您的表名,因为DEFAULTKEYWORD中的Mysql