My code:
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);
}
My Error:
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
I'm working on my final project for computer science and having an issue with my database connectoin. I've worked with mysql before and never had this problem. I spent a handful of hours trying to figure it out and can't dedicate anymore. Any and all help as to what is going on would be appreciated
DEFAULT
is a MySQL reserved word . Rename your table to something else.
您可能需要修改表名,因为DEFAULT
是Mysql
的KEYWORD
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.