|
|
|
การทำ GUI/Insert/Update/Search/Delete ผ่าน Database |
|
|
|
|
|
|
|
Code (Java)
package lect9;
public class Car {
int plate;
String model;
String make;
public Car(int plate, String model, String make) {
this.plate = plate;
this.model = model;
this.make = make;
}
public String toString() {
return "plate:" + plate + ", model:" + model + ", make:" + make;
}
}
|
|
|
|
|
Date :
2017-09-11 21:17:39 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package lect9;
import java.io.*;
import java.sql.Connection;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CarDB {
private Vector<Car> Cars;
public CarDB() {
// Initialize, yet to be implemented more
Cars = new <Car>Vector();
}
private void updateCar() {
System.out.println("CarDB.updateCar()");
try {
System.out.print("Plate:");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
int plate = Integer.parseInt(temp);
System.out.print("Model:");
String model = scanner.nextLine();
System.out.print("Make:");
String make = scanner.nextLine();
String updateSql = "UPDATE CAR SET MODEL='" + model + "', MAKE='" + make + "' WHERE PLATE=" + plate;
System.out.println("updateSql:" + updateSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.executeUpdate(updateSql);
stmnt.close();
con.close();
System.out.println("Car updated successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void deleteCar() {
System.out.println("CarDB.deleteCar()");
try {
System.out.print("Plate:");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
int plate = Integer.parseInt(temp);
String deleteSql = "DELETE FROM CAR WHERE PLATE=" + plate;
System.out.println("deleteSql:" + deleteSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.executeUpdate(deleteSql);
stmnt.close();
con.close();
System.out.println("Deleted successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void searchCar() {
System.out.println("CarDB.search()");
try {
System.out.print("Plate:");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
int plate = Integer.parseInt(temp);
String searchSql = "SELECT * FROM CAR WHERE PLATE=" + plate;
System.out.println("selectSql:" + searchSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
ResultSet rs = stmnt.executeQuery(searchSql);
if (rs.next()) {
plate = rs.getInt("PLATE");
String model = rs.getString("MODEL");
String make = rs.getString("MAKE");
Car car = new Car(plate, model, make);
System.out.println("Car:" + car.toString());
}
stmnt.close();
con.close();
System.out.println("Searched successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void addCars() {
System.out.println("CarDB.addCars()");
try {
System.out.print("Plate:");
Scanner scanner = new Scanner(System.in);
String temp = scanner.nextLine();
int plate = Integer.parseInt(temp);
System.out.print("Model:");
String model = scanner.nextLine();
System.out.print("Make:");
String make = scanner.nextLine();
Car car = new Car(plate, model, make);
String insertSql = "INSERT INTO CAR VALUES" + "(" + car.plate + ", '" + car.model + "', '" + car.make
+ "')";
System.out.println("selectSql:" + insertSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.execute(insertSql);
stmnt.close();
con.close();
System.out.println("Car added successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
private void viewCars() {
System.out.println("CarDB.viewCars()");
String selectSql = "SELECT * FROM CAR";
System.out.println("selectSql:" + selectSql);
Vector<Car> cars = new Vector();
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
try {
stmnt = con.createStatement();
ResultSet rs = stmnt.executeQuery(selectSql);
while (rs.next()) {
int plate = rs.getInt("PLATE");
String model = rs.getString("MODEL");
String make = rs.getString("MAKE");
Car car = new Car(plate, model, make);
cars.add(car);
}
stmnt.close();
con.close();
System.out.println("Retrieved successfully.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
Iterator it = cars.iterator();
while (it.hasNext()) {
Car Car = (Car) it.next();
System.out.println(Car.toString());
}
}
private void mainMenu() {
boolean cont = true;
while (cont) {
System.out.println("=== Main Menu ===");
System.out.println("1 Add Car.");
System.out.println("2 Update Car.");
System.out.println("3 Search Car.");
System.out.println("4 Delete Car.");
System.out.println("9 View Cars.");
System.out.println("0 Exit");
System.out.print("Your choice:");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
if (choice.equals("1")) {
addCars();
} else if (choice.equals("2")) {
updateCar();
} else if (choice.equals("3")) {
searchCar();
} else if (choice.equals("4")) {
deleteCar();
} else if (choice.equals("9")) {
viewCars();
} else if (choice.equals("0")) {
break;
}
}
}
public static void main(String[] args) {
System.out.println("CarDB.main()");
CarDB CarDB = new CarDB();
CarDB.mainMenu();
}
}
|
ประวัติการแก้ไข 2017-09-11 21:21:43
|
|
|
|
Date :
2017-09-11 21:19:18 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package Mytest;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.util.*;
public class AddCarDlg extends JDialog implements ActionListener {
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
JTextField modelTxt;
JTextField makeTxt;
private static final String addString = "Add";
private static final String cancelString = "Cancel";
private Vector<Car> cars;
public AddCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel addCarLabel = new JLabel("Add Car");
JLabel plateLabel = new JLabel("Plate:");
JLabel modelLabel = new JLabel("Model:");
JLabel makeLabel = new JLabel("Make:");
JButton addBtn = new JButton(addString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
modelTxt = new JTextField(20);
makeTxt = new JTextField(20);
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(addCarLabel);
pane.add(plateLabel);
pane.add(modelLabel);
pane.add(makeLabel);
pane.add(plateTxt);
pane.add(modelTxt);
pane.add(makeTxt);
pane.add(addBtn);
pane.add(cancelBtn);
Dimension size = addCarLabel.getPreferredSize();
addCarLabel.setBounds((width - size.width) / 2, 5, size.width, size.height);
size = plateLabel.getPreferredSize();
plateLabel.setBounds(10, 25, size.width, size.height);
size = modelLabel.getPreferredSize();
modelLabel.setBounds(10, 50, size.width, size.height);
size = makeLabel.getPreferredSize();
makeLabel.setBounds(10, 75, size.width, size.height);
size = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, size.width, size.height);
size = modelTxt.getPreferredSize();
modelTxt.setBounds(50, 50, size.width, size.height);
size = makeTxt.getPreferredSize();
makeTxt.setBounds(50, 75, size.width, size.height);
size = addBtn.getPreferredSize();
addBtn.setBounds(width / 2 - size.width - 10, 105, size.width, size.height);
size = cancelBtn.getPreferredSize();
cancelBtn.setBounds(width / 2 + 10, 105, size.width, size.height);
pane.setPreferredSize(new Dimension(width, height));
pack();
setVisible(false);
System.out.println("AddCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(addString)) {
System.out.println("equals " + addString);
try {
int plate = Integer.parseInt(plateTxt.getText());
String model = modelTxt.getText();
String make = makeTxt.getText();
Car car = new Car(plate, model, make);
addCar(car);
plateTxt.setText(null);
modelTxt.setText(null);
makeTxt.setText(null);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
} else if (actionCommand.equals(cancelString)) {
System.out.println("equals " + cancelString);
setVisible(false);
}
}
private void addCar(Car car) {
try {
String insertSql = "INSERT INTO CAR VALUES" + "(" + car.plate + ", '" + car.model + "', '" + car.make
+ "')";
System.out.println("selectSql:" + insertSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.execute(insertSql);
stmnt.close();
con.close();
System.out.println("Car added successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2017-09-11 21:24:37 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package lect9;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class UpdateCarDlg extends JDialog implements ActionListener {
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
JTextField modelTxt;
JTextField makeTxt;
private static final String updateString = "Update";
private static final String cancelString = "Cancel";
private Vector<Car> cars;
public UpdateCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel updateCarLabel = new JLabel("Add Car");
JLabel plateLabel = new JLabel("Plate:");
JLabel modelLabel = new JLabel("Model:");
JLabel makeLabel = new JLabel("Make:");
JButton updateBtn = new JButton(updateString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
modelTxt = new JTextField(20);
makeTxt = new JTextField(20);
updateBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(updateCarLabel);
pane.add(plateLabel);
pane.add(modelLabel);
pane.add(makeLabel);
pane.add(plateTxt);
pane.add(modelTxt);
pane.add(makeTxt);
pane.add(updateBtn);
pane.add(cancelBtn);
Dimension size = updateCarLabel.getPreferredSize();
updateCarLabel.setBounds((width - size.width) / 2, 5, size.width, size.height);
size = plateLabel.getPreferredSize();
plateLabel.setBounds(10, 25, size.width, size.height);
size = modelLabel.getPreferredSize();
modelLabel.setBounds(10, 50, size.width, size.height);
size = makeLabel.getPreferredSize();
makeLabel.setBounds(10, 75, size.width, size.height);
size = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, size.width, size.height);
size = modelTxt.getPreferredSize();
modelTxt.setBounds(50, 50, size.width, size.height);
size = makeTxt.getPreferredSize();
makeTxt.setBounds(50, 75, size.width, size.height);
size = updateBtn.getPreferredSize();
updateBtn.setBounds(width / 2 - size.width - 10, 105, size.width, size.height);
size = cancelBtn.getPreferredSize();
cancelBtn.setBounds(width / 2 + 10, 105, size.width, size.height);
pane.setPreferredSize(new Dimension(width, height));
pack();
setVisible(false);
System.out.println("AddCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(updateString)) {
System.out.println("equals " + updateString);
try {
int plate = Integer.parseInt(plateTxt.getText());
String model = modelTxt.getText();
String make = makeTxt.getText();
Car car = new Car(plate, model, make);
updateCar(car);
plateTxt.setText(null);
modelTxt.setText(null);
makeTxt.setText(null);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
} else if (actionCommand.equals(cancelString)) {
System.out.println("equals " + cancelString);
setVisible(false);
}
}
private void updateCar(Car car) {
try {
String updateSql = "UPDATE CAR SET MODEL='" + car.model + "', MAKE='" + car.make + "' WHERE PLATE=" + car.plate;
System.out.println("updateSql:" + updateSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.executeUpdate(updateSql);
stmnt.close();
con.close();
System.out.println("Car updated successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate.");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2017-09-11 21:34:23 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package Mytest;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.util.*;
public class DeleteCarDlg extends JDialog implements ActionListener {
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
private static final String deleteString = "Delete";
private static final String cancelString = "Cancel";
private static final String plateStr = "Plate";
private static final String modelStr = "Model";
private static final String makeStr = "Make";
Container pane = null;
JPanel dataPanel = null;
JScrollPane scrollPane = null;
private Vector<Car> cars;
public DeleteCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel deleteCarLabel = new JLabel("Delete Car");
JLabel plateLabel = new JLabel("Plate:");
JButton deleteBtn = new JButton(deleteString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
deleteBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(deleteCarLabel);
pane.add(plateLabel);
pane.add(plateTxt);
pane.add(deleteBtn);
pane.add(cancelBtn);
Dimension size = deleteCarLabel.getPreferredSize();
deleteCarLabel.setBounds((width - size.width) / 2, 5, size.width, size.height);
size = plateLabel.getPreferredSize();
plateLabel.setBounds(10, 25, size.width, size.height);
size = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, size.width, size.height);
size = deleteBtn.getPreferredSize();
deleteBtn.setBounds(width / 2 - size.width - 10, 105, size.width, size.height);
size = cancelBtn.getPreferredSize();
cancelBtn.setBounds(width / 2 + 10, 105, size.width, size.height);
pane.setPreferredSize(new Dimension(width, height));
pack();
setVisible(false);
System.out.println("DeleteCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(deleteString)) {
System.out.println("equals " + deleteString);
try {
int plate = Integer.parseInt(plateTxt.getText());
Car car = new Car(plate, actionCommand, actionCommand);
deleteCar(car);
plateTxt.setText(null);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
} else if (actionCommand.equals(cancelString)) {
System.out.println("equals " + cancelString);
setVisible(false);
}
}
private void deleteCar(Car car) {
try {
String deleteSql = "DELETE FROM CAR WHERE PLATE=" + car.plate;
System.out.println("deleteSql:" + deleteSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
stmnt.executeUpdate(deleteSql);
stmnt.close();
con.close();
System.out.println("Deleted successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2017-09-11 21:38:06 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package Mytest;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.util.*;
public class SearchCarDlg extends JDialog implements ActionListener {
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
JTextField modelTxt;
JTextField makeTxt;
private static final String searchString = "search";
private static final String cancelString = "Cancel";
private Vector<Car> cars;
public SearchCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel searchCarLabel = new JLabel("Search Car");
JLabel plateLabel = new JLabel("Plate:");
JButton searchBtn = new JButton(searchString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
modelTxt = new JTextField(20);
makeTxt = new JTextField(20);
searchBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(searchCarLabel);
pane.add(plateLabel);
pane.add(plateTxt);
pane.add(searchBtn);
pane.add(cancelBtn);
Dimension size = searchCarLabel.getPreferredSize();
searchCarLabel.setBounds((width - size.width) / 2, 5, size.width, size.height);
size = plateLabel.getPreferredSize();
plateLabel.setBounds(10, 25, size.width, size.height);
size = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, size.width, size.height);
size = searchBtn.getPreferredSize();
searchBtn.setBounds(width / 2 - size.width - 10, 105, size.width, size.height);
size = cancelBtn.getPreferredSize();
cancelBtn.setBounds(width / 2 + 10, 105, size.width, size.height);
pane.setPreferredSize(new Dimension(width, height));
pack();
setVisible(false);
System.out.println("SearchCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(searchString)) {
System.out.println("equals " + searchString);
try {
int plate = Integer.parseInt(plateTxt.getText());
Car car = new Car(plate, actionCommand, actionCommand);
searchCar(car);
plateTxt.setText(null);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
} else if (actionCommand.equals(cancelString)) {
System.out.println("equals " + cancelString);
setVisible(false);
}
}
private void searchCar(Car car) {
try {
String searchSql = "SELECT * FROM CAR WHERE PLATE=" + car.plate;
System.out.println("selectSql:" + searchSql);
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
stmnt = con.createStatement();
ResultSet rs = stmnt.executeQuery(searchSql);
if (rs.next()) {
car.plate = rs.getInt("PLATE");
car.model = rs.getString("MODEL");
car.make = rs.getString("MAKE");
Car cars = new Car(car.plate, car.model, car.make);
System.out.println("Car:" + car.toString());
}
stmnt.close();
con.close();
System.out.println("Searched successfully.");
}
} catch (NumberFormatException ex) {
System.err.println("Error! Invalid plate");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
|
|
|
|
|
Date :
2017-09-11 21:38:25 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package Mytest;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JPanel;
import java.util.*;
public class ViewCarLabelDlg extends JDialog implements ActionListener {
private static final int panelWidth = 300;
private static final int panelHeight = 200;
private static final String plateStr = "Plate";
private static final String modelStr = "Model";
private static final String makeStr = "Make";
private static final JLabel plateHead = new JLabel(plateStr);
private static final JLabel modelHead = new JLabel(modelStr);
private static final JLabel makeHead = new JLabel(makeStr);
Container pane = null;
JPanel dataPanel = null;
JScrollPane scrollPane = null;
private static final String okString = "OK";
public ViewCarLabelDlg(JFrame frame) {
super(frame, true);
Container pane = getContentPane();
pane.setLayout(new GridLayout(3, 0));
JLabel addCarLabel = new JLabel("View Car");
dataPanel = new JPanel();
dataPanel.setLayout(null);
dataPanel.setPreferredSize(new Dimension(panelWidth, panelHeight));
dataPanel.add(plateHead);
dataPanel.add(modelHead);
dataPanel.add(makeHead);
Dimension size = plateHead.getPreferredSize();
plateHead.setBounds(5, 5, size.width, size.height);
size = modelHead.getPreferredSize();
modelHead.setBounds(55, 5, size.width, size.height);
size = makeHead.getPreferredSize();
makeHead.setBounds(175, 5, size.width, size.height);
JButton okBtn = new JButton(okString);
okBtn.addActionListener(this);
pane.add(addCarLabel);
size = addCarLabel.getPreferredSize();
addCarLabel.setBounds(10, 75, size.width, size.height);
scrollPane = new JScrollPane(dataPanel);
pane.add(scrollPane);
pane.add(okBtn);
pack();
setVisible(false);
System.out.println("ViewCarDialog() done!");
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
if (actionCommand.equals(okString)) {
System.out.println("equals " + okString);
try {
setVisible(false);
} catch (Exception ex) {
System.err.println("Error! Invalid data.");
}
}
}
void viewCar() {
System.out.println("CarDB.viewCars()");
String selectSql = "SELECT * FROM CAR";
System.out.println("selectSql:" + selectSql);
Vector<Car> cars = new Vector();
cars.removeAllElements();
ConnectionDB connDB = new ConnectionDB();
Connection con = connDB.getConnection();
Statement stmnt = null;
if (con != null) {
try {
stmnt = con.createStatement();
ResultSet rs = stmnt.executeQuery(selectSql);
while (rs.next()) {
int plate = rs.getInt("PLATE");
String model = rs.getString("MODEL");
String make = rs.getString("MAKE");
Car car = new Car(plate, model, make);
cars.add(car);
}
stmnt.close();
con.close();
System.out.println("Retrieved successfully.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
dataPanel.removeAll();
dataPanel.add(plateHead);
dataPanel.add(modelHead);
dataPanel.add(makeHead);
Dimension size = plateHead.getPreferredSize();
plateHead.setBounds(5, 5, size.width, size.height);
size = modelHead.getPreferredSize();
modelHead.setBounds(55, 5, size.width, size.height);
size = makeHead.getPreferredSize();
makeHead.setBounds(175, 5, size.width, size.height);
int y = 35;
JLabel label = null;
int idx = 0;
Iterator it = cars.iterator();
while (it.hasNext()) {
Car car = (Car) it.next();
System.out.println(car.toString());
label = new JLabel("" + car.plate);
dataPanel.add(label);
size = label.getPreferredSize();
label.setBounds(5, y, size.width, size.height);
label = new JLabel(car.model);
dataPanel.add(label);
size = label.getPreferredSize();
label.setBounds(55, y, size.width, size.height);
label = new JLabel(car.make);
dataPanel.add(label);
size = label.getPreferredSize();
label.setBounds(175, y, size.width, size.height);
y += 30;
idx += 1;
}
pack();
setVisible(true);
}
}
|
|
|
|
|
Date :
2017-09-11 21:38:52 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package Mytest;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.BorderFactory;
import java.util.*;
public class MainFrame extends JPanel implements ActionListener {
private static int frameWidth = 400;
private static int frameHeight = 300;
AddCarDlg addCarDlg;
ViewCarLabelDlg viewCarDlg;
DeleteCarDlg deleteCarDlg;
SearchCarDlg searchCarDlg;
UpdateCarDlg updateCarDlg;
Vector<String> cars;
private static final String mainString = "Main Menu";
private static final String carString = "Add car";
private static final String viewString = "View cars";
private static final String deleteString = "Delete cars";
private static final String searchString = "Search cars";
private static final String updateString = "Update cars";
private static final String exitString = "Exit";
public MainFrame(JFrame frame) {
super();
cars = new Vector();
addCarDlg = new AddCarDlg(frame, cars);
viewCarDlg = new ViewCarLabelDlg(frame);
deleteCarDlg = new DeleteCarDlg(frame, cars);
searchCarDlg = new SearchCarDlg(frame, cars);
updateCarDlg = new UpdateCarDlg(frame, cars);
int panelWidth = 300;
int panelHeight = 350;
setLayout(null);
JLabel mainLabel = new JLabel("Main Menu");
JButton addcarBtn = new JButton(carString);
JButton viewBtn = new JButton(viewString);
JButton exitBtn = new JButton(exitString);
JButton deleteBtn = new JButton(deleteString);
JButton searchBtn = new JButton(searchString);
JButton updateBtn = new JButton(updateString);
addcarBtn.addActionListener(this);
viewBtn.addActionListener(this);
deleteBtn.addActionListener(this);
searchBtn.addActionListener(this);
updateBtn.addActionListener(this);
exitBtn.addActionListener(this);
add(mainLabel);
add(addcarBtn);
add(viewBtn);
add(deleteBtn);
add(searchBtn);
add(exitBtn);
add(updateBtn);
Dimension size = mainLabel.getPreferredSize();
mainLabel.setBounds((panelWidth - size.width) / 2, 20, size.width, size.height);
size = addcarBtn.getPreferredSize();
addcarBtn.setBounds((panelWidth - size.width) / 2, 60, size.width, size.height);
size = viewBtn.getPreferredSize();
viewBtn.setBounds((panelWidth - size.width) / 2, 100, size.width, size.height);
size = deleteBtn.getPreferredSize();
deleteBtn.setBounds((panelWidth - size.width) / 2, 140, size.width, size.height);
size = searchBtn.getPreferredSize();
searchBtn.setBounds((panelWidth - size.width) / 2, 180, size.width, size.height);
size = updateBtn.getPreferredSize();
updateBtn.setBounds((panelWidth - size.width) / 2, 220, size.width, size.height);
size = exitBtn.getPreferredSize();
exitBtn.setBounds((panelWidth - size.width) / 2, 260, size.width, size.height);
this.setPreferredSize(new Dimension(panelWidth, panelHeight));
}
public void actionPerformed(ActionEvent evt) {
String actionCommand = evt.getActionCommand();
System.out.println("actionCommand:" + actionCommand);
if (actionCommand.equals(carString)) {
System.out.println("equals " + carString);
addCarDlg.setVisible(true);
} else if (actionCommand.equals(viewString)) {
System.out.println("equals " + viewString);
viewCarDlg.viewCar();
} else if (actionCommand.equals(deleteString)) {
System.out.println("equals " + deleteString);
deleteCarDlg.setVisible(true);
} else if (actionCommand.equals(searchString)) {
System.out.println("equals " + searchString);
searchCarDlg.setVisible(true);
} else if (actionCommand.equals(updateString)) {
System.out.println("equals " + updateString);
updateCarDlg.setVisible(true);
} else if (actionCommand.equals(exitString)) {
System.out.println("equals " + exitString);
System.exit(0);
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("GUI Mini Scm");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add contents to the window.
frame.add(new MainFrame(frame));
// Display the window.
// frame.setPreferredSize(new Dimension(frameWidth, frameHeight));
// frame.setSize(frameWidth, frameHeight);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
|
|
|
|
|
Date :
2017-09-11 21:39:33 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|