|
|
|
วิธีการทำ Add/Search/Update/Delete/View ทำเป็น GUI ผ่านทาง Database |
|
|
|
|
|
|
|
Code (Java)
package GUI;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
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 java.util.*;
public class DeleteDlg extends JDialog implements ActionListener {
private static final String okString = "OK";
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
private static final String addString = "Add";
private static final String cancelString = "Cancel";
private Vector<Car> cars;
public DeleteDlg(JFrame frame, Vector data) {
super (frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel addCarLabel = new JLabel("Delete Car");
JLabel plateLabel = new JLabel("Plate:");
JButton addBtn = new JButton(addString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(addCarLabel);
pane.add(plateLabel);
pane.add(plateTxt);
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 = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, 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("DelCarDialog() 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());
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);
}
}
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:30:07 |
By :
Niyom.Tha |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมว่ามันไม่ถูกนะครับ
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 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:31:47 |
By :
shelby |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package GUI;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class SearchCarDlg extends JDialog implements ActionListener {
private static final String okString = "OK";
private static final int width = 300;
private static final int height = 200;
JTextField plateTxt;
private static final String addString = "Add";
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 addCarLabel = new JLabel("Delete Car");
JLabel plateLabel = new JLabel("Plate:");
JButton addBtn = new JButton(addString);
JButton cancelBtn = new JButton(cancelString);
plateTxt = new JTextField(10);
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
pane.add(addCarLabel);
pane.add(plateLabel);
pane.add(plateTxt);
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 = plateTxt.getPreferredSize();
plateTxt.setBounds(50, 25, 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("DelCarDialog() 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());
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);
}
}
void searchCar(Car car) {
System.out.println("CarDB.search()");
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:" + cars.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:35:14 |
By :
SparTaCus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Java)
package lect9;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class UpdateCarDlg extends JDialog implements ActionListener {
private static final String okString = "OK";
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 UpdateCarDlg(JFrame frame, Vector data) {
super(frame, true);
this.cars = data;
Container pane = getContentPane();
pane.setLayout(null);
JLabel addCarLabel = new JLabel("Updata 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("UpdareCarDialog() 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);
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);
}
}
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:37:26 |
By :
SparTaCus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
}
}
|
|
|
|
|
Date :
2017-09-11 23:52:57 |
By :
SarTaCus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|