Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. package uk.ac.hw.macs.CompanyDatabase;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8.  
  9. public class JDBCMySQLApp extends JFrame
  10. implements ActionListener
  11. {
  12. //CONNECTING TO MySQL
  13. //declare driver
  14. final String driver = "com.mysql.jdbc.Driver";
  15. //declare the fixed part of the url
  16. final String urlstart = "jdbc:mysql://localhost/";
  17. String urluserpass;
  18.  
  19. //database connection
  20. Connection con;
  21.  
  22. //swing components
  23. JScrollPane scroll;
  24. JTable tab;
  25. JButton parQuery,ownSQL, ownQueries;
  26. JComboBox tableList, descList;
  27. JLabel tableHeading;
  28. QueryTable quTable;
  29. JTextArea feedback;
  30.  
  31. //first part of SQL to list complete table
  32. String selectAll = "Select * from ";
  33.  
  34. //instructions for switchboard
  35. String tableListLabel = "Choose a name to see the contents of that table or view ";
  36. String descListLabel = "Choose a description to see the details of table or view ";
  37. String ownSQLLabel = "Enter your own SQL query ";
  38. String interactiveLabel = "Choose from a set of queries";
  39.  
  40. //connects to database and sets up GUI
  41. public JDBCMySQLApp()
  42. {
  43. //initialises frame
  44. super();
  45. this.setTitle(DBInfo.dbName + " Heriot-Warr Marketing");
  46. this.setSize(750, 650);
  47. this.addWindowListener(new WindowCloser());
  48.  
  49. //connects to database
  50. connectToDatabase();
  51. //MAKE TOP PANEL
  52. JPanel topPanel = new JPanel();
  53. topPanel.setLayout(new GridLayout(5,3));
  54.  
  55. // Add each table to the table combo box
  56. tableList = new JComboBox();
  57. tableList.setEditable(true);
  58. for (int i = 0; i < DBInfo.tables.length; i++)
  59. {
  60. tableList.addItem(DBInfo.tables[i]);
  61. }
  62. tableList.addActionListener(this);
  63. topPanel.add(new JLabel(tableListLabel, SwingConstants.RIGHT));
  64. topPanel.add(tableList);
  65.  
  66.  
  67. // Add each description and view to the query combo box
  68. descList = new JComboBox();
  69. descList.setEditable(true);
  70.  
  71. for (int i = 0; i < DBInfo.descQueries.length; i++)
  72. {
  73. descList.addItem(DBInfo.descQueries[i].getDesc());
  74. }
  75. descList.addActionListener(this);
  76. topPanel.add(new JLabel(descListLabel, SwingConstants.RIGHT));
  77. topPanel.add(descList);
  78.  
  79.  
  80. //add own queries button
  81. ownQueries = new JButton("Set of queries");
  82. ownQueries .addActionListener(this);
  83. topPanel.add(new JLabel("Set of queries", SwingConstants.RIGHT));
  84. topPanel.add(ownQueries);
  85.  
  86. //add own SQL Query button
  87. ownSQL = new JButton("New SQL Query");
  88. ownSQL.addActionListener(this);
  89. topPanel.add(new JLabel(ownSQLLabel, SwingConstants.RIGHT));
  90. topPanel.add(ownSQL);
  91.  
  92.  
  93. //add table heading
  94. tableHeading = new JLabel("");
  95. tableHeading.setForeground(Color.black);
  96. tableHeading.setFont(new Font("SansSerif", Font.BOLD, 16));
  97. topPanel.add(tableHeading);
  98.  
  99. //addTextArea
  100. JPanel sPanel = new JPanel();
  101. feedback = new JTextArea(5,60);
  102. JScrollPane jsp = new JScrollPane(feedback);
  103. sPanel.add(jsp);
  104.  
  105.  
  106. //CREATE TABLE IN SCROLLPANE
  107. //The table will contain the query results
  108. quTable = new QueryTable();
  109. tab = new JTable(quTable);
  110. scroll = new JScrollPane(tab);
  111.  
  112.  
  113. //ADD PANELS TO CONTENT PANE
  114. getContentPane().add(topPanel, BorderLayout.NORTH);
  115. getContentPane().add(sPanel, BorderLayout.SOUTH);
  116. getContentPane().add(scroll, BorderLayout.CENTER);
  117.  
  118. }
  119.  
  120.  
  121. /////////////////////////////////////////////////////////////////
  122. //METHOD actionPerformed
  123. //Handles all actions from GUI
  124. //If table or view listing required, execute 'Select All'
  125. // (table or view is displayed in scrollpane)
  126. //Else If set of queries or own SQL query is required
  127. // open specialised frame
  128. public void actionPerformed(ActionEvent evt)
  129. {
  130. Object source = evt.getSource();
  131.  
  132. //If table/view listing required, execute 'Select All'
  133. // (table is displayed in scrollpane)
  134. if (source == tableList)
  135. {
  136. String sql ="";
  137. try
  138. {
  139. String table = (String) tableList.getSelectedItem();
  140. if (!table.equals("")) {
  141. Statement stmt = con.createStatement();
  142. sql = selectAll + table;
  143. ResultSet rs= stmt.executeQuery(sql);
  144. quTable.formatTable(rs);
  145.  
  146. }
  147. else {
  148. quTable.clearTable();
  149. }
  150. tableHeading.setText(table);
  151. feedback.setText("");
  152. }
  153. catch (SQLException e)
  154. {
  155. feedback.setText(e + sql);
  156. }
  157. }
  158.  
  159. //If description listing required, find table/view name,
  160. // then execute 'Select All'
  161. // (table displayed in scrollpane)
  162. else if (source == descList)
  163. {
  164. String sql = "";
  165. try
  166. {
  167. String queryDesc = (String)descList.getSelectedItem();
  168. if (!queryDesc.equals("")) {
  169. DescQuery dq = DBInfo.findDescQuery(queryDesc);
  170. sql = selectAll + dq.getName();
  171. Statement stmt = con.createStatement();
  172. ResultSet rs= stmt.executeQuery(sql);
  173. quTable.formatTable(rs);
  174. tableHeading.setText(dq.getDesc());
  175. feedback.setText("");
  176. }
  177. }
  178. catch (SQLException e)
  179. {
  180. feedback.setText(e + sql);
  181. }
  182. }
  183.  
  184. else if (source == ownQueries) {
  185. feedback.setText("");
  186. OwnQueries pq = new OwnQueries(con);
  187. pq.setVisible(true);
  188. }
  189. // if own SQL Query required, open SQL frame
  190. else if (source == ownSQL)
  191. {
  192. feedback.setText("");
  193. SQLQuery sq = new SQLQuery(con);
  194. sq.setVisible(true);
  195. }
  196.  
  197. }
  198.  
  199.  
  200. /////////////////////////////////////////////////////////////////
  201. // Obtain user name and password
  202. // Connect to database
  203. void connectToDatabase()
  204. {
  205. // Obtain user name and password
  206.  
  207. UserPassDialog upd = new UserPassDialog(this);
  208. upd.setLocation(200,200);
  209. LogOn logonInfo = upd.showDialog();
  210. String username = logonInfo.getUName();
  211. String password = logonInfo.getPWord();
  212. String dbname = "hwmarketing";
  213.  
  214. String connectDetails = urlstart + dbname + "?user=" + username + "&password="
  215. + password;
  216.  
  217. // Connect to database
  218. try
  219. {
  220. //connect to the database
  221. Class.forName(driver).newInstance();
  222. con = DriverManager.getConnection(connectDetails);
  223. }
  224. catch(Exception e)
  225. {
  226. System.out.println("Could not initialize the database."
  227. + e.getMessage());
  228. e.printStackTrace();
  229. System.exit(0);
  230. }
  231.  
  232. }
  233.  
  234.  
  235.  
  236. /////////////////////////////////////////////////////////////////
  237. public static void main(String args[])
  238. {
  239. JDBCMySQLApp f = new JDBCMySQLApp();
  240. f.setVisible(true);
  241. }
  242.  
  243.  
  244.  
  245. /////////////////////////////////////////////////////////////////
  246. // Inner class for window closing
  247. // Close DB conection then exit
  248. class WindowCloser extends WindowAdapter
  249. {
  250. public void windowClosing(WindowEvent event)
  251. {
  252. try
  253. {
  254. if (con != null)
  255. {
  256. con.close();
  257. }
  258. System.exit(0);
  259. }
  260.  
  261. catch(Exception e)
  262. {
  263. System.out.println("Could not close the current connection.");
  264. e.printStackTrace();
  265. }
  266.  
  267. }
  268. }
  269.  
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement