Professional Documents
Culture Documents
Data Science Using JDBC and Mysql With Object Oriented Approach and Apache Netbeans Ide 1St Edition Vivian Siahaan
Data Science Using JDBC and Mysql With Object Oriented Approach and Apache Netbeans Ide 1St Edition Vivian Siahaan
https://ebookmeta.com/product/object-oriented-data-analysis-1st-
edition-marron/
https://ebookmeta.com/product/object-oriented-modeling-and-
design-using-uml-2nd-edition-singh/
https://ebookmeta.com/product/concise-guide-to-object-oriented-
programming-an-accessible-approach-using-java-undergraduate-
topics-in-computer-science-1st-edition-kingsley-sage/
https://ebookmeta.com/product/object-oriented-data-analysis-1st-
edition-james-stephen-marron/
Python 3 Object oriented Programming Building robust
and maintainable software with object oriented design
patterns in Python 2nd Edition Phillips
https://ebookmeta.com/product/python-3-object-oriented-
programming-building-robust-and-maintainable-software-with-
object-oriented-design-patterns-in-python-2nd-edition-phillips/
https://ebookmeta.com/product/common-data-sense-for-
professionals-a-process-oriented-approach-for-data-science-
projects-1st-edition-rajesh-jugulum/
https://ebookmeta.com/product/object-oriented-python-irv-kalb/
https://ebookmeta.com/product/object-oriented-programming-with-
swift-2-1st-edition-gaston-c-hillar/
https://ebookmeta.com/product/pro-apache-netbeans-building-
applications-on-the-rich-client-platform-1st-edition-ioannis-
kostaras/
DATA SCIENCE
USING JDBC AND MYSQL
WITH OBJECT-ORIENTED APPROACH
AND APACHE NETBEANS IDE
DATA SCIENCE
USING JDBC AND MYSQL
WITH OBJECT-ORIENTED APPROACH
AND APACHE NETBEANS IDE
First Edition
VIVIAN SIAHAAN
RISMON HASIHOLAN SIANIPAR
graduating from SMAN 3 Pematang Siantar 3, the writer traveled to the city of
Jogjakarta. In 1998 and 2001 the author completed his Bachelor of Engineering
(S.T) and Master of Engineering (M.T) education in the Electrical Engineering of Gadjah
Mada University, under the guidance of Prof. Dr. Adhi Soesanto and Prof. Dr. Thomas Sri
Widodo, focusing on research on non-stationary signals by analyzing their energy using time-
frequency maps. Because of its non-stationary nature, the distribution of signal energy
becomes very dynamic on a time-frequency map. By mapping the distribution of energy in the
time-frequency field using discrete wavelet transformations, one can design non-linear filters
so that they can analyze the pattern of the data contained in it. In 2003, the author received a
completed his Master of Engineering (M.Eng) and Doctor of Engineering (Dr.Eng) education
at Yamaguchi University, under the guidance of Prof. Dr. Hidetoshi Miike. Both the master's
thesis and his doctoral thesis, R.H. Sianipar combines SR-FHN (Stochastic Resonance
Fitzhugh-Nagumo) filter strength with cryptosystem ECC (elliptic curve cryptography) 4096-
bit both to suppress noise in digital images and digital video and maintain its authenticity. The
results of this study have been documented in international scientific journals and officially
patented in Japan. One of the patents was published in Japan with a registration number 2008-
Japan, particularly in the fields of cryptography, cryptanalysis and audio / image / video digital
(cryptanalysis) on a number of intelligence data that are the object of research studies in
Japan. R.H. Sianipar has a number of Japanese patents, and has written a number of national /
international scientific articles, and dozens of national books. R.H. Sianipar has also
watermarking, and digital forensics. In a number of workshops, R.H. Sianipar helps Prof.
computer vision, and others, which are used as training materials. Field of interest in the study
cryptography, digital communication, digital forensics, and data compression / coding. Until
now, R.H. Sianipar continues to develop applications related to analysis of signal, image, and
digital video, both for research purposes and for commercial purposes based on the Python
In this project, you will design the form for every table
and you will plot: top 10 film distribution by release
year; top 10 film distribution by rating; top 10 film
distribution by rental duration; top 10 film distribution
by language; film distribution by categorized rental
rate; film distribution by categorized length; film
distribution by categorized replacement cost; top 10
film distribution by actor name; top 10 actor name
distribution by average rental rate; top 10 actor name
distribution by average replacement cost; film
distribution by rating; rating distribution by average
rental rate; rating distribution by average replacement
cost; top 10 film distribution by category name,
category distribution by average replacement cost;
category distribution by average rental rate; category
distribution by length; top 10 city distribution by by
country; top 10 address distribution by district, top 10
address distribution by country; top 10 address
distribution by city; top 10 address distribution by
district; top 10 address distribution by country; top
10 address distribution by city; top 10 inventory
distribution by release year; top 10 inventory
distribution by film rating; top 10 inventory
distribution by film language; top 10 inventory
distribution by film rental duration; top 10 inventory
distribution by city; top 10 inventory distribution by
country; top 10 customer distribution by country; top
10 customer distribution by city; top 10 customer
distribution by district; top 10 customer distribution
by store country; top 10 customer distribution by store
city; top 10 customer distribution by store district; top
10 staff distribution by country; top 10 staff
distribution by city; rental distribution by year of
rental date; rental distribution by month of rental date;
10 rental distribution by week of rental date; rental
distribution by day of rental date; rental distribution
by quarter of rental date; rental distribution by film
release year; rental distribution by film duration;
rental distribution by film rating; top 10 rental
distribution by staff name; rental distribution by film
language; top 10 rental distribution by film title;
rental distribution by customer active; top 10 rental
distribution by film category; top 10 rental distribution
by actor name; top 10 rental distribution by customer
name; top 10 rental distribution by customer city; top
10 rental distribution by customer country, top 10
rental distribution by customer district; payment
distribution by year of payment date; payment
distribution by month of payment date; top 10
payment distribution by week of payment date;
payment distribution by day of payment date;
payment distribution by quarter of payment date;
payment distribution by film release year; payment
distribution by film duration; payment distribution by
film rating; top 10 payment distribution by staff
name; payment distribution by film language; top 10
payment distribution by film title; payment
distribution by customer active; top 10 payment
distribution by film category; top 10 payment
distribution by actor name; top 10 payment
distribution by customer name; top 10 payment
distribution by customer city; top 10 payment
distribution by customer country; and top 10 payment
distribution by customer district.
CONTENT
CONTENT
PROJECT AND UTILITY CLASS 1
DESCRIPTION 1
CREATING PROJECT AND UTILITY CLASS 3
SOURCE CODE 17
ACTOR FORM 26
CREATING AND POPULATING ACTOR 26
TABLE 32
DESIGNING GUI 35
POPULATING TABLE AND COMBOBOXES 38
DISPLAYING AND NAVIGATING DATA 44
ROW BY ROW 49
UPDATING RECORD 51
UPDATING RECORD DIRECTLY ON JTABLE 54
INSERTING NEW RECORD 55
DELETING RECORD
SOURCE CODE
67
67
LANGUAGE FORM 72
CREATING AND POPULATING LANGUAGE 75
TABLE 78
DESIGNING GUI 84
POPULATING TABLE AND COMBOBOXES 88
DISPLAYING AND NAVIGATING DATA 90
ROW BY ROW 93
UPDATING RECORD 94
UPDATING RECORD DIRECTLY ON JTABLE
INSERTING NEW RECORD
DELETING RECORD 106
SOURCE CODE 106
111
114
CATEGORY FORM 118
CREATING AND POPULATING CATEGORY 123
TABLE 128
DESIGNING GUI 130
POPULATING TABLE AND COMBOBOXES 133
134
DISPLAYING AND NAVIGATING DATA
ROW BY ROW
UPDATING RECORD
UPDATING RECORD DIRECTLY ON JTABLE 145
INSERTING NEW RECORD 145
DELETING RECORD 156
SOURCE CODE 158
163
171
177
FILM FORM 179
CREATING AND POPULATING FILM TABLE 182
DESIGNING GUI 183
POPULATING TABLE AND COMBOBOXES 191
DISPLAYING AND NAVIGATING DATA
ROW BY ROW
UPDATING RECORD 209
UPDATING RECORD DIRECTLY ON JTABLE 209
INSERTING NEW RECORD 217
DELETING RECORD 219
PLOTTING CHART 224
SOURCE CODE 233
238
241
FILM ACTOR FORM 242
CREATING AND POPULATING FILM 246
ACTOR TABLE
DESIGNING GUI
POPULATING TABLE AND COMBOBOXES 261
DISPLAYING AND NAVIGATING DATA 261
ROW BY ROW 269
UPDATING RECORD 272
INSERTING NEW RECORD 276
DELETING RECORD 285
PLOTTING CHART 291
SOURCE CODE 294
294
297
FILM CATEGORY FORM
CREATING AND POPULATING FILM
CATEGORY TABLE 311
DESIGNING GUI 311
POPULATING TABLE AND COMBOBOXES 317
DISPLAYING AND NAVIGATING DATA 320
ROW BY ROW 323
UPDATING RECORD 329
INSERTING NEW RECORD 334
DELETING RECORD 336
PLOTTING CHART 339
SOURCE CODE 340
399
CITY FORM 399
CREATING AND POPULATING CITY TABLE 407
DESIGNING GUI 411
POPULATING TABLE AND COMBOBOXES 416
DISPLAYING AND NAVIGATING DATA 424
ROW BY ROW 429
UPDATING RECORD 431
UPDATING RECORD DIRECTLY ON JTABLE 434
INSERTING NEW RECORD 435
DELETING RECORD 437
PLOTTING CHART
SOURCE CODE
452
452
ADDRESS FORM 460
464
CREATING AND POPULATING ADDRESS 468
TABLE 476
DESIGNING GUI 481
POPULATING TABLE AND COMBOBOXES 483
DISPLAYING AND NAVIGATING DATA 486
ROW BY ROW 487
UPDATING RECORD 489
UPDATING RECORD DIRECTLY ON JTABLE
INSERTING NEW RECORD
DELETING RECORD 504
PLOTTING CHART 504
SOURCE CODE 514
519
523
STORE FORM 533
CREATING AND POPULATING STORE 538
TABLE 541
DESIGNING GUI 542
POPULATING TABLE AND COMBOBOXES 545
DISPLAYING AND NAVIGATING DATA
ROW BY ROW
UPDATING RECORD 562
UPDATING RECORD DIRECTLY ON JTABLE 562
INSERTING NEW RECORD 572
DELETING RECORD 576
PLOTTING CHART 580
SOURCE CODE 590
596
599
INVENTORY FORM 602
CREATING AND POPULATING INVENTORY 603
TABLE 606
DESIGNING GUI
POPULATING TABLE AND COMBOBOXES
DISPLAYING AND NAVIGATING DATA 626
ROW BY ROW 626
UPDATING RECORD 635
INSERTING NEW RECORD 639
DELETING RECORD 643
PLOTTING CHART 655
SOURCE CODE 659
663
664
CUSTOMER FORM 666
CREATING AND POPULATING CUSTOMER
TABLE
DESIGNING GUI 685
POPULATING TABLE AND COMBOBOXES 685
DISPLAYING AND NAVIGATING DATA 697
ROW BY ROW 703
UPDATING RECORD 707
UPDATING RECORD DIRECTLY ON JTABLE 719
INSERTING NEW RECORD 725
DELETING RECORD 728
PLOTTING CHART 729
SOURCE CODE 738
PAYMENT FORM
CREATING AND POPULATING PAYMENT
TABLE
DESIGNING GUI
POPULATING TABLE AND COMBOBOXES
DISPLAYING AND NAVIGATING DATA
ROW BY ROW
UPDATING RECORD
INSERTING NEW RECORD
DELETING RECORD
PLOTTING CHART
SOURCE CODE
MAIN FORM
DESIGNING GUI
PROJECT
AND UTILITY CLASS
PROJECT
AND UTILITY CLASS
DESCRIPTION
DESCRIPTION
1 url =
2 jdbc:mysql://localhost:3306/sakila
3 user = vivian
password = vivian
Step Then, create a public class named Utility. Define two new methods: getConnect
4 testConnection().
1 package Sakila;
2 import java.io.FileInputStream;
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.util.Properties;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9 import java.sql.*;
10 import java.awt.BorderLayout;
11 import java.awt.Color;
12 import java.awt.Component;
13 import java.awt.RenderingHints;
14 import java.awt.image.BufferedImage;
15 import java.io.File;
16 import java.io.IOException;
17 import javax.swing.JOptionPane;
18 import java.text.DecimalFormat;
19 import java.text.NumberFormat;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import javax.imageio.ImageIO;
26 import javax.swing.ImageIcon;
27 import javax.swing.JComboBox;
28 import javax.swing.JFileChooser;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JPanel;
32 import javax.swing.JTable;
33 import javax.swing.JTextField;
34 import javax.swing.SwingUtilities;
35 import javax.swing.UIManager;
36 import javax.swing.UnsupportedLookAndFeelException;
37 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
38 import javax.swing.table.DefaultTableCellRenderer;
39 import javax.swing.table.TableCellRenderer;
40 import javax.swing.table.TableColumn;
41 import org.jfree.chart.ChartFactory;
42 import org.jfree.chart.ChartPanel;
43 import org.jfree.chart.JFreeChart;
44 import org.jfree.chart.labels.ItemLabelAnchor;
45 import org.jfree.chart.labels.ItemLabelPosition;
46 import
47 org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
48 import
49 org.jfree.chart.labels.StandardPieSectionLabelGenerator;
50 import org.jfree.chart.plot.CategoryPlot;
51 import org.jfree.chart.plot.PiePlot;
52 import org.jfree.chart.plot.PlotOrientation;
53 import
54 org.jfree.chart.renderer.category.CategoryItemRenderer;
55 import org.jfree.chart.ui.TextAnchor;
56 import org.jfree.data.category.DefaultCategoryDataset;
57 import org.jfree.data.general.DefaultPieDataset;
58
59 public class Utility {
60 private static final Logger LOGGER =
61 Logger.getLogger(Utility.class.getName());
62
63
64 public static Connection getConnection() throws
65 SQLException {
66 Connection conn = null;
67 Properties props = new Properties();
68 try (FileInputStream fis = new
69 FileInputStream("database.properties")) {
70 props.load(fis);
71 String url = props.getProperty("url");
72 String user = props.getProperty("user");
73 String password =
74 props.getProperty("password");
75
76 conn = DriverManager.getConnection(url, user,
77 password);
78 } catch (IOException ex) {
79 LOGGER.log(Level.SEVERE, null, ex);
80 }
81 return conn;
82 }
83
84 public static void testConnection() {
85 try (Connection conn = getConnection()) {
String message = String.format("Successfully
connected to %s database", conn.getCatalog());
LOGGER.log(Level.INFO, message);
} catch (SQLException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
}
}
Next, the method loops through the ResultSet and retrieves the first column's value
row, casts it to a generic type T, and adds it to the combo box using the jcb.a
method. Finally, the method closes the ResultSet.
If any SQL exception is thrown, the method displays an error message dialog box
exception message.
JOptionPane.showMessageDialog(obj_form,
ex.getMessage(),
"ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
}
Step Then, add a new method named table_renderer(). This method is used to custo
6 rendering of a JTable object. The method takes a JTable object as its input param
sets the default cell renderer of the JTable object to a custom renderer. The custom
overrides the getTableCellRendererComponent() method of the TableCellR
interface, and sets the background color of alternate rows to white and light gray, t
easier to read the data in the table.
The method achieves this by creating a new TableCellRenderer object and over
getTableCellRendererComponent() method. The method checks if the row n
even or odd, and sets the background color of the cell accordingly. If the row n
even, the background color is set to white, and if the row number is odd, the ba
color is set to light gray. The method returns the component with the updated ba
color.
This method can be called after populating a JTable object to apply the cus
rendering.
Step Then, add a new method named set_column_header(). This method sets the colum
7 for a JTable. The method takes two arguments: the JTable object and a Stri
containing the column headers.
The method loops through the columns of the table using a for loop and gets th
model of the table header for each column using the getColumnModel() method
sets the header value of each column using the setHeaderValue() method
corresponding value from the header array.
Finally, the method returns the same header array that was passed as input argumen
It first sets a boolean variable found to false to indicate that the specified value ha
been found. Then it iterates over each item in the JComboBox using a for loop.
Inside the loop, it retrieves the item at the current index and compares it to the
value using the Objects.equals() method. If the item matches the specified value, i
selected index of the JComboBox to the current index and sets found to true to ind
the value was found. It then breaks out of the loop since there is no need to
iterating.
If the specified value is not found in the JComboBox, it sets the selected index
indicate that no item is selected.
comboBox.setSelectedIndex(-1);
}
}
Step Then, add a new method named item_to_display(). This method is used to ha
9 pagination of data displayed in a form. It takes in the SQL statements for ge
minimum and maximum values of the data, a text field where the current page n
displayed, and a string indicating the direction of pagination (first, previous, next, la
The method first checks if the text field is empty or null. If it is, it returns the def
number of 1. Otherwise, it tries to parse the integer value of the text field and sets t
current page number. If the parsing fails due to an invalid integer format, an error
is displayed and the default page number is returned.
If any errors occur during the SQL execution, an error message is displayed. Th
then returns the updated current page number.
currentValue++;
} else {
currentValue = 1;
}
}
} catch (SQLException
ex) {
JOptionPane.showMessageDialog(frm,
ex.getMessage(),
"ERROR",
JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException
ex){
JOptionPane.showMessageDialog(frm,
ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
case "last":
try (Connection conn =
getConnection();
PreparedStatement
ps = conn.prepareStatement(sql_max)) {
ResultSet rs =
ps.executeQuery();
if (rs.next()) {
currentValue =
rs.getInt(1);
}
} catch (SQLException
ex) {
JOptionPane.showMessageDialog(frm,
ex.getMessage(),
"ERROR",
JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException
ex){
JOptionPane.showMessageDialog(frm,
ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
default:
break;
}
return currentValue;
}
Step Then, add a new method named setLookAndFeel().This method sets the look and
10 JFrame to the Nimbus look and feel, and then customizes the colors of va
elements. The Nimbus look and feel is a cross-platform look and feel that was intro
Java 6.
The method first sets the Nimbus look and feel using UIManager.setLookAnd
then sets various UI colors using UIManager.put(). Finally, it updates the U
JFrame using SwingUtilities.updateComponentTreeUI().
UIManager.put("nimbusLightBackground", new
Color(220, 220, 220));
UIManager.put("nimbusOrange",
new Color(191, 98, 4));
UIManager.put("nimbusRed", new
Color(169, 46, 34));
UIManager.put("nimbusSelectedText", new
Color(255, 255, 255));
UIManager.put("nimbusSelectionBackground",
new Color(104, 93, 156));
SwingUtilities.updateComponentTreeUI(frame);
} catch
(UnsupportedLookAndFeelException e) {
System.err.println("Nimbus:
Unsupported Look and feel!");
}
}
These are two utility methods for creating datasets for pie charts and ba
respectively. Both methods take a Map<String, Double> object, where each key r
a category and its associated value is the count for that category.
Both methods catch any exceptions that may occur and display an error message in
box if necessary.
ps.setObject(1, item);
try (ResultSet rs =
ps.executeQuery()) {
if (rs.next()) {
result =
rs.getObject(columnName);
} else {
// no row
found, clear the form fields
JOptionPane.showMessageDialog(null,
"ITEM NOT FOUND");
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,
ex.getMessage(), "ERROR",
JOptionPane.ERROR_MESSAGE);
}
return result;
}
Step Then, still in Utility class, define a new method named categorize_values().Thi
14 takes a list of numerical values and a map that defines the ranges for different cate
returns a map that contains the count of values that fall within each category.
The method first initializes an empty map of counts for each category using the k
the input map. Then, it loops through each value in the input list and categorizes i
corresponding range by comparing it to the values in the input map. If the value fa
a range, the corresponding category is set for that value. Finally, the count
corresponding category is incremented in the counts map.
The method returns the counts map, which contains the count of values for each
The key of the map represents the category, and the value represents the count of v
fall within that category.
return counts;
}
Both methods also catch any exceptions that occur during the execution of the SQ
and display an error message using a JOptionPane.
1 public static
2 DefaultCategoryDataset
3 create_bar_dataset(String query,
4 String valColumn, String
5 labelColumn){
6 // Creates a
7 DefaultCategoryDataset object
8 DefaultCategoryDataset
9 dataset = new
10 DefaultCategoryDataset();
11
12 try(Connection conn =
13 getConnection(); Statement stmt =
14 conn.createStatement();
15 ResultSet rs =
16 stmt.executeQuery(query)){
17
18 while (rs.next()) {
19 Object value =
20 rs.getObject(valColumn);
21 String label =
22 rs.getString(labelColumn);
23
24 if (value
25 instanceof Number) {
26
27 dataset.addValue(((Number)
28 value).doubleValue(), labelColumn,
29 label);
30 } else if (value
31 instanceof String) {
32 double
33 doubleValue =
34 Double.parseDouble((String) value);
35
36 dataset.addValue(doubleValue,
37 labelColumn, label);
38 } else {
39 throw new
40 RuntimeException("Unsupported value
41 type: " +
42 value.getClass().getName());
43 }
44 }
45 } catch (Exception ex){
46
47 JOptionPane.showMessageDialog(null,
48 ex.getMessage(),
49
50 "ERROR",JOptionPane.ERROR_MESSAGE);
51 }
52 return dataset;
53 }
54
55 public static DefaultPieDataset
56 create_pie_dataset(String query,
57 String valColumn, String
58 labelColumn){
59 // Creates a
60 DefaultPieDataset object
61 DefaultPieDataset dataset =
new DefaultPieDataset();
try(Connection conn =
getConnection(); Statement stmt =
conn.createStatement();
ResultSet rs =
stmt.executeQuery(query)){
while (rs.next()) {
Object value =
rs.getObject(valColumn);
String label =
rs.getString(labelColumn);
if (value
instanceof Number) {
dataset.setValue(label, ((Number)
value).doubleValue());
} else if (value
instanceof String) {
double
doubleValue =
Double.parseDouble((String) value);
dataset.setValue(label,
doubleValue);
} else {
throw new
RuntimeException("Unsupported value
type: " +
value.getClass().getName());
}
}
} catch (Exception ex){
JOptionPane.showMessageDialog(null,
ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
return dataset;
}
Step Then, define two new methods named read_image() and show_image(). Th
16 methods are related to displaying images on a Swing-based user interface.
The read_image() method allows the user to select an image file using the JFile
dialog box and then display it on a given JLabel control. It takes three parameters:
jframe: a JFrame instance which is used as the parent of the file choos
box
jtfPhotoPath: a JTextField instance that will display the absolute pat
selected image file
label: a JLabel instance which is used to display the selected image
The method first creates a JFileChooser object and sets the current directory to t
home directory. It then shows the dialog box and waits for the user to select a fi
user chooses a valid image file that can be read, the method reads the image file
ImageIO.read() method and displays it on the given JLabel control. It also sets th
the given JTextField control to the absolute path of the selected image file. If the
file is not a valid image file or cannot be read, the method shows an error mess
message dialog box and resets the JLabel and JTextField controls.
Step Next, define a new method named findMaxInt(). This method is used to find the m
17 integer value in a specific column of a MySQL table. It takes in three parame
connection object to the database, the name of the table, and the name of the c
search for the maximum value in.
Inside the method, an initial value of the maximum value is set to the smallest
integer value. The method then constructs a SQL query string to find the maximum
the specified column of the specified table. The query string is executed using a S
object, and the resulting ResultSet is processed to obtain the maximum value found
If there are no results in the ResultSet, the initial minimum value set at the sta
method is returned. Otherwise, the maximum value found in the ResultSet is return
Step Then, define an overloaded read_image() method with only two parameters:
18
1 public void read_image(JFrame jframe, JLabel label) {
2 BufferedImage im = null;
3 try {
4 JFileChooser fileChooser = new JFileChooser();
5 fileChooser.setCurrentDirectory(new
6 File(System.getProperty("user.home")));
7 int result = fileChooser.showOpenDialog(jframe);
8 if (result == JFileChooser.APPROVE_OPTION) {
9 File fileChosen = fileChooser.getSelectedFile();
10 if (fileChosen.isFile() && fileChosen.canRead())
11 {
12 im = ImageIO.read(fileChosen);
13
14 // Display image on jLabelImage
15 ImageIcon icon = new ImageIcon(im);
16 label.setIcon(icon);
17 } else {
18 throw new IOException("Selected file is not
19 a valid image file or cannot be read.");
20 }
21 }
22 } catch (IOException e) {
23 JOptionPane.showMessageDialog(jframe, "Error reading
24 image file: " + e.getMessage(),
25 "Error", JOptionPane.ERROR_MESSAGE);
26 label.setIcon(null);
27 } catch (Exception e) {
28 ImageIcon icon = new
29 ImageIcon(getClass().getClassLoader().getResource("dummy.png"));
30 label.setIcon(icon);
31 }
}
while (rs.next()) {
T value = (T) rs.getObject(1);
jcb.addItem(value);
}
switch (direction) {
case "first":
try (Connection conn = getConnection();
PreparedStatement ps =
conn.prepareStatement(sql_min)) {
ResultSet rs = ps.executeQuery();
if (rs.next()) {
currentValue = rs.getInt(1);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frm,
ex.getMessage(),
"ERROR", JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException ex){
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
case "prev":
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql_max))
{
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int max = rs.getInt(1);
if (currentValue > 1) {
currentValue--;
} else {
currentValue = max;
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR", JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException ex){
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
case "next":
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql_max))
{
ResultSet rs = ps.executeQuery();
if (rs.next()) {
int max = rs.getInt(1);
if (currentValue < max) {
currentValue++;
} else {
currentValue = 1;
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR", JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException ex){
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
case "last":
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql_max))
{
ResultSet rs = ps.executeQuery();
if (rs.next()) {
currentValue = rs.getInt(1);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR", JOptionPane.ERROR_MESSAGE);
}catch(java.lang.NumberFormatException ex){
JOptionPane.showMessageDialog(frm, ex.getMessage(),
"ERROR",JOptionPane.ERROR_MESSAGE);
}
break;
default:
break;
}
return currentValue;
}
/**
* Sets the Nimbus look and feel for a JFrame.
* @param frame the JFrame to set the look and feel for
*/
public static void setLookAndFeel(JFrame frame) {
try {
// Set the Nimbus look and feel
UIManager.setLookAndFeel(new NimbusLookAndFeel());
Method.
1. 2. 3.
Ammonia-nitrogen in one kilogram of soil.
Sum 99.6 „ „
Sum 0.00244 „
Sum 0.000054 „