I have to write a program that creates a GUI that prompts a user to enter a query, then return the result to the same screen. All the examples I've found in my text book and other course material only shows how to account for a predetermined query. How do I account for a dynamically changing query?
The database I have to access is Microsoft Access and I am supposed to use SQL based queries.
Also, does the query code go inside main or an actionperformed method?
Code so far (GUI and database connection written thus far)
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.*;
import java.sql.*;
public class DataBase extends JFrame implements ActionListener
{
public DataBase()
{
JPanel northpanel = new JPanel();
JPanel southpanel = new JPanel();
JPanel eastpanel = new JPanel();
eastpanel.setLayout(new GridLayout(1,2));
JButton submitQuery = new JButton("Submit Query");
JButton saveQuery = new JButton("Save Query");
JTextField queryField = new JTextField(18);
JScrollBar jscbHort = new JScrollBar(JScrollBar.HORIZONTAL);
JScrollBar jscbVert = new JScrollBar(JScrollBar.VERTICAL);
JTextArea queryResults = new JTextArea("This is the query result", 5, 20);
southpanel.setBorder(new TitledBorder("Query Results"));
submitQuery.addActionListener(this);
saveQuery.addActionListener(this);
northpanel.add(queryField);
eastpanel.add(submitQuery);
eastpanel.add(saveQuery);
southpanel.add(queryResults);
add(northpanel, BorderLayout.NORTH);
add(southpanel, BorderLayout.SOUTH);
add(eastpanel, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e)
{
}
public static void main(String[] args)throws SQLException, ClassNotFoundException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Drive loaded");
Connection connection = DriverManager.getConnection("jdbc:odbc:skiclub");
System.out.println("Database connected");
DataBase frame = new DataBase();
//set the window title
frame.setTitle("JAVA ITP220 - DataBase Program");
//set the window location
frame.setLocationRelativeTo(null);
//specify what happens when the close button is clicked
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the window size
frame.setSize(600, 250);
//display the window
frame.setVisible(true);
connection.close();
}
}