n which method is the call to the database being executed? 2. What is the name of the database this is being accessed? 3. What is the name of the database table being accessed? 4. Which

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Given the code below, what would be the answers for the following questions:

1. In which method is the call to the database being executed?

2. What is the name of the database this is being accessed?

3. What is the name of the database table being accessed?

4. Which column is being retrieved from the database table?

 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class guiDBExample extends JFrame implements ActionListener {

    String inputNumber;
    int sqlNumber;
    String partName = null;    
    String partDescription = null;
    String partPrice = null;
    JTextField name;
    JTextField number;
    
    public guiDBExample()
    {
        buildGUI();
    }

    public final void buildGUI() {
        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();
        JPanel panel5 = new JPanel();
       
        getContentPane().add(panel1, "North");
        getContentPane().add(panel2, "West");
        getContentPane().add(panel3, "Center");
        getContentPane().add(panel4, "East");
        getContentPane().add(panel5, "South");    
        
        JLabel partNumber = new JLabel("Part number ");
        panel1.add(partNumber);
        
        number = new JTextField("", 10);
        panel1.add(number);
                
        JLabel partNameLabel = new JLabel("Name ");
        panel1.add(partNameLabel);
        
        name = new JTextField("", 15);
        panel1.add(name);
 
        JButton read = new JButton("Read");
        read.addActionListener(this);
        panel5.add(read);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void getPartName()
            throws SQLException, ClassNotFoundException {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loaded");
            // Establish a connection
            Connection connection = DriverManager.getConnection
            ("jdbc:mysql://localhost/test");
            
            //Sample connection if you have a password set on your machine
            //Connection connection = DriverManager.getConnection
            //        ("jdbc:mysql://localhost/test","root", "MySQL");
            
            System.out.println("Database connected");
            // Create a statement
            Statement statement = connection.createStatement();
            // Execute a statement
            
            ResultSet resultSet = statement.executeQuery
            ("select * from Inventory WHERE partNumber = " + sqlNumber + ";");

            // Iterate through the result and print the student names
            while (resultSet.next())
            {
               partName = resultSet.getString(2);
             }
            // Close the connection
            connection.close();
            }        
            
 
    public void actionPerformed(ActionEvent action) {
        
        
        if(action.getActionCommand().equals("Read"))
        {
            try {
                inputNumber = number.getText();
                sqlNumber = Integer.parseInt(inputNumber);
                getPartName();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            name.setText(partName);

        }

    }
    
     public static void main(String[] args) {
             guiDBExample ex = new guiDBExample();
             //the next statement correctly sizes your GUI
             ex.pack();
             ex.setVisible(true);
         
     }
 
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Procedures in SQL
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education