Advertisement
Guest User

Main

a guest
Dec 20th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.GroupLayout;
  10. import javax.swing.GroupLayout.Alignment;
  11. import javax.swing.JButton;
  12. import javax.swing.LayoutStyle.ComponentPlacement;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ItemEvent;
  15. import java.awt.event.ItemListener;
  16. import java.awt.event.ActionEvent;
  17. import java.sql.*;
  18. import javax.swing.JTextArea;
  19. import javax.swing.JPopupMenu;
  20. import java.awt.Component;
  21. import java.awt.event.MouseAdapter;
  22. import java.awt.event.MouseEvent;
  23. import javax.swing.JTextPane;
  24. import javax.swing.JComboBox;
  25. import javax.swing.JTable;
  26. import java.awt.Font;
  27. public class Main extends JFrame {
  28. private JButton editAUserButton;
  29. private JButton addAUserButton;
  30. private JPanel contentPane;
  31. private JButton showUsersButton;
  32. private JButton deleteUserButton;
  33. private JButton showMovieButton;
  34. private JButton showRentedButton;
  35. private GroupLayout gl_contentPane;
  36. private JOptionPane optionPane;
  37. private JTextArea textArea;
  38. private JComboBox comboBox;
  39. private String name;
  40. private ResultSet res;
  41. private String address;
  42. private String newName;
  43. private static final String USERNAME = "root";
  44. private static final String PASSWORD = "*****";
  45. private static final String CONN_STRING = "jdbc:mysql://localhost:3306/rentmovie?verifyServerCertificate=false&useSSL=true";
  46. private ResultSet rs;
  47. private Statement stm;
  48. private Connection conn;
  49. /**
  50. * Launch the application.
  51. */
  52. public static void main(String[] args) {
  53. EventQueue.invokeLater(new Runnable() {
  54. public void run() {
  55. try {
  56. Main frame = new Main();
  57. frame.setVisible(true);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61.  
  62. }
  63. });
  64. }
  65.  
  66. /**
  67. * Create the frame.
  68. */
  69. public Main() {
  70. try{
  71. conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
  72. stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  73. rs = stm.executeQuery("SELECT * FROM users");
  74. res = stm.executeQuery("Select * from movie");
  75.  
  76. }catch (SQLException e){
  77. System.out.println(e.getSQLState());
  78. System.out.println(e.getErrorCode());
  79. System.out.println(e.getMessage());
  80. }
  81. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  82. setBounds(100, 100, 721, 492);
  83. contentPane = new JPanel();
  84. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  85. setContentPane(contentPane);
  86.  
  87. user();
  88.  
  89. showMovieButton = new JButton("Show movies");
  90.  
  91.  
  92. showRentedButton = new JButton("Show rented movies");
  93.  
  94. comboBox = new JComboBox();
  95. comboBox.addActionListener(new ActionListener() {
  96. public void actionPerformed(ActionEvent arg0) {
  97. try{
  98. String query = "Select * from movie where genre=? ";
  99. PreparedStatement pst = conn.prepareStatement(query);
  100. pst.setString(1, (String)comboBox.getSelectedItem());
  101. ResultSet rs = pst.executeQuery();
  102.  
  103. while(rs.next()){
  104. textArea.setText("MovieCode: " + rs.getInt("Code") + " Name: " + rs.getString("name") + " Price: " + rs.getInt("price") + " Genre: " + rs.getString("Genre") + "\n");
  105. }
  106.  
  107. }catch(Exception e){
  108. e.printStackTrace();
  109. }
  110. }
  111. });
  112.  
  113.  
  114. textArea = new JTextArea();
  115. textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
  116. textArea.setEditable(false);
  117. gl_contentPane = new GroupLayout(contentPane);
  118. gl_contentPane.setHorizontalGroup(
  119. gl_contentPane.createParallelGroup(Alignment.LEADING)
  120. .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
  121. .addContainerGap()
  122. .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
  123. .addPreferredGap(ComponentPlacement.UNRELATED)
  124. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  125. .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
  126. .addPreferredGap(ComponentPlacement.UNRELATED)
  127. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
  128. .addComponent(editAUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  129. .addComponent(deleteUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  130. .addComponent(showUsersButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
  131. .addComponent(addAUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  132. .addComponent(showMovieButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  133. .addGroup(gl_contentPane.createSequentialGroup()
  134. .addComponent(showRentedButton)
  135. .addContainerGap())))
  136. .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
  137. .addPreferredGap(ComponentPlacement.RELATED)
  138. .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  139. .addGap(83))))
  140. );
  141. gl_contentPane.setVerticalGroup(
  142. gl_contentPane.createParallelGroup(Alignment.LEADING)
  143. .addGroup(gl_contentPane.createSequentialGroup()
  144. .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
  145. .addGroup(gl_contentPane.createSequentialGroup()
  146. .addGap(66)
  147. .addComponent(addAUserButton)
  148. .addPreferredGap(ComponentPlacement.RELATED)
  149. .addComponent(editAUserButton)
  150. .addPreferredGap(ComponentPlacement.RELATED)
  151. .addComponent(showUsersButton)
  152. .addPreferredGap(ComponentPlacement.RELATED)
  153. .addComponent(deleteUserButton)
  154. .addGap(18)
  155. .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
  156. .addPreferredGap(ComponentPlacement.RELATED)
  157. .addComponent(showMovieButton)
  158. .addGap(50)
  159. .addComponent(showRentedButton))
  160. .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE))
  161. .addContainerGap(13, Short.MAX_VALUE))
  162. );
  163. contentPane.setLayout(gl_contentPane);
  164. fillComboBox();
  165. }
  166.  
  167. private void user(){
  168. addUser();
  169. editUser();
  170. showUser();
  171. deleteUser();
  172. }
  173.  
  174. private void addUser(){
  175. addAUserButton = new JButton("Add a user");
  176. addAUserButton.addActionListener(new ActionListener() {
  177.  
  178. public void actionPerformed(ActionEvent arg0) {
  179. name = JOptionPane.showInputDialog("Enter your name: ");
  180. address = JOptionPane.showInputDialog("Enter your address: ");
  181. String query = User.addUser(name, address);
  182. try{
  183. stm = conn.createStatement();
  184. stm.executeUpdate(query);
  185. }catch(SQLException e){
  186. System.out.println(e.getMessage() );
  187. }
  188. }
  189. });
  190.  
  191. }
  192.  
  193. private void editUser(){
  194. editAUserButton = new JButton("Edit user");
  195. editAUserButton.addActionListener(new ActionListener() {
  196. public void actionPerformed(ActionEvent arg0) {
  197. String userID = JOptionPane.showInputDialog("Input the ID of user you want to Edit: ");
  198. int id = Integer.parseInt(userID);
  199. newName = JOptionPane.showInputDialog("Enter the new name of user " + id);
  200. String newAddress = JOptionPane.showInputDialog("Enter the new address of user " + id);
  201. String query = User.editUser(newName, newAddress, id);
  202. try{
  203. stm = conn.createStatement();
  204. stm.executeUpdate(query);
  205.  
  206. }catch(SQLException e){
  207. System.out.println(e.getMessage());
  208. }
  209. }
  210. });
  211. }
  212.  
  213. private void showUser(){
  214.  
  215. showUsersButton = new JButton("Show users");
  216. showUsersButton.addActionListener(new ActionListener() {
  217. public void actionPerformed(ActionEvent e) {
  218. try {
  219. String query = User.displayUsers(rs);
  220. stm = conn.createStatement();
  221. rs = stm.executeQuery(query);
  222. textArea.setText("adhsj");
  223.  
  224. } catch (SQLException e1) {
  225. e1.printStackTrace();
  226. }
  227. }
  228. });
  229. }
  230.  
  231. private void deleteUser(){
  232. deleteUserButton = new JButton("Delete user");
  233. deleteUserButton.addActionListener(new ActionListener() {
  234. public void actionPerformed(ActionEvent e) {
  235. String userID = JOptionPane.showInputDialog("Input the ID of user you want to delete: ");
  236. int id = Integer.parseInt(userID);
  237. String query = User.deleteUser(id);
  238. try{
  239. stm = conn.createStatement();
  240. stm.executeUpdate(query);
  241. }catch(SQLException e2){
  242. System.out.println(e2.getMessage());
  243. }
  244. }
  245. });
  246.  
  247. }
  248. private void showMovies(){
  249. showMovieButton.addActionListener(new ActionListener() {
  250. public void actionPerformed(ActionEvent arg0) {
  251. try{
  252. String query = Movie.showMovies(res);
  253. stm = conn.createStatement();
  254. rs = stm.executeQuery(query);
  255. textArea.setText(query);
  256. }catch(SQLException e){
  257. e.getSQLState();
  258. }
  259. }
  260. });
  261.  
  262. }
  263.  
  264. private void fillComboBox(){
  265. try{
  266. String query ="Select * from movie";
  267. stm = conn.createStatement();
  268. stm.executeQuery(query);
  269. comboBox.addItem(" ");
  270. while (res.next()){
  271. comboBox.addItem(rs.getString("Genre"));
  272. }
  273. }catch(Exception e){
  274. e.printStackTrace();
  275. }
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement