Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JButton;
- import javax.swing.LayoutStyle.ComponentPlacement;
- import java.awt.event.ActionListener;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import java.awt.event.ActionEvent;
- import java.sql.*;
- import javax.swing.JTextArea;
- import javax.swing.JPopupMenu;
- import java.awt.Component;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JTextPane;
- import javax.swing.JComboBox;
- import javax.swing.JTable;
- import java.awt.Font;
- public class Main extends JFrame {
- private JButton editAUserButton;
- private JButton addAUserButton;
- private JPanel contentPane;
- private JButton showUsersButton;
- private JButton deleteUserButton;
- private JButton showMovieButton;
- private JButton showRentedButton;
- private GroupLayout gl_contentPane;
- private JOptionPane optionPane;
- private JTextArea textArea;
- private JComboBox comboBox;
- private String name;
- private ResultSet res;
- private String address;
- private String newName;
- private static final String USERNAME = "root";
- private static final String PASSWORD = "*****";
- private static final String CONN_STRING = "jdbc:mysql://localhost:3306/rentmovie?verifyServerCertificate=false&useSSL=true";
- private ResultSet rs;
- private Statement stm;
- private Connection conn;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Main() {
- try{
- conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
- stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- rs = stm.executeQuery("SELECT * FROM users");
- res = stm.executeQuery("Select * from movie");
- }catch (SQLException e){
- System.out.println(e.getSQLState());
- System.out.println(e.getErrorCode());
- System.out.println(e.getMessage());
- }
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 721, 492);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- user();
- showMovieButton = new JButton("Show movies");
- showRentedButton = new JButton("Show rented movies");
- comboBox = new JComboBox();
- comboBox.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try{
- String query = "Select * from movie where genre=? ";
- PreparedStatement pst = conn.prepareStatement(query);
- pst.setString(1, (String)comboBox.getSelectedItem());
- ResultSet rs = pst.executeQuery();
- while(rs.next()){
- textArea.setText("MovieCode: " + rs.getInt("Code") + " Name: " + rs.getString("name") + " Price: " + rs.getInt("price") + " Genre: " + rs.getString("Genre") + "\n");
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- });
- textArea = new JTextArea();
- textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
- textArea.setEditable(false);
- gl_contentPane = new GroupLayout(contentPane);
- gl_contentPane.setHorizontalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
- .addContainerGap()
- .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
- .addPreferredGap(ComponentPlacement.UNRELATED)
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
- .addComponent(editAUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(deleteUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(showUsersButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
- .addComponent(addAUserButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(showMovieButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addComponent(showRentedButton)
- .addContainerGap())))
- .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addGap(83))))
- );
- gl_contentPane.setVerticalGroup(
- gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
- .addGroup(gl_contentPane.createSequentialGroup()
- .addGap(66)
- .addComponent(addAUserButton)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(editAUserButton)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(showUsersButton)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(deleteUserButton)
- .addGap(18)
- .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(ComponentPlacement.RELATED)
- .addComponent(showMovieButton)
- .addGap(50)
- .addComponent(showRentedButton))
- .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 430, GroupLayout.PREFERRED_SIZE))
- .addContainerGap(13, Short.MAX_VALUE))
- );
- contentPane.setLayout(gl_contentPane);
- fillComboBox();
- }
- private void user(){
- addUser();
- editUser();
- showUser();
- deleteUser();
- }
- private void addUser(){
- addAUserButton = new JButton("Add a user");
- addAUserButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- name = JOptionPane.showInputDialog("Enter your name: ");
- address = JOptionPane.showInputDialog("Enter your address: ");
- String query = User.addUser(name, address);
- try{
- stm = conn.createStatement();
- stm.executeUpdate(query);
- }catch(SQLException e){
- System.out.println(e.getMessage() );
- }
- }
- });
- }
- private void editUser(){
- editAUserButton = new JButton("Edit user");
- editAUserButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- String userID = JOptionPane.showInputDialog("Input the ID of user you want to Edit: ");
- int id = Integer.parseInt(userID);
- newName = JOptionPane.showInputDialog("Enter the new name of user " + id);
- String newAddress = JOptionPane.showInputDialog("Enter the new address of user " + id);
- String query = User.editUser(newName, newAddress, id);
- try{
- stm = conn.createStatement();
- stm.executeUpdate(query);
- }catch(SQLException e){
- System.out.println(e.getMessage());
- }
- }
- });
- }
- private void showUser(){
- showUsersButton = new JButton("Show users");
- showUsersButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- String query = User.displayUsers(rs);
- stm = conn.createStatement();
- rs = stm.executeQuery(query);
- textArea.setText("adhsj");
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- });
- }
- private void deleteUser(){
- deleteUserButton = new JButton("Delete user");
- deleteUserButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String userID = JOptionPane.showInputDialog("Input the ID of user you want to delete: ");
- int id = Integer.parseInt(userID);
- String query = User.deleteUser(id);
- try{
- stm = conn.createStatement();
- stm.executeUpdate(query);
- }catch(SQLException e2){
- System.out.println(e2.getMessage());
- }
- }
- });
- }
- private void showMovies(){
- showMovieButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- try{
- String query = Movie.showMovies(res);
- stm = conn.createStatement();
- rs = stm.executeQuery(query);
- textArea.setText(query);
- }catch(SQLException e){
- e.getSQLState();
- }
- }
- });
- }
- private void fillComboBox(){
- try{
- String query ="Select * from movie";
- stm = conn.createStatement();
- stm.executeQuery(query);
- comboBox.addItem(" ");
- while (res.next()){
- comboBox.addItem(rs.getString("Genre"));
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement