Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 107,668

HOME > Java Programming > Java Forum > การทำ GUI/Insert/Update/Search/Delete ผ่าน Database





 

การทำ GUI/Insert/Update/Search/Delete ผ่าน Database

 



Topic : 128869



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์




Code (Java)
package Mytest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionDB {
	public Connection getConnection() {

		System.out.println("-------- Oracle JDBC Connection Testing ------");

		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");

		} catch (ClassNotFoundException e) {

			System.out.println("Where is your Oracle JDBC Driver?");
			e.printStackTrace();
		}

		Connection connection = null;
		System.out.println("Oracle JDBC Driver Registered!");

		try {

			connection = DriverManager.getConnection("jdbc:oracle:thin:@10.30.2.50:1521:prog2", "59011211174", "59011211174");

		} catch (SQLException e) {

			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
		}

		if (connection != null) {
			System.out.println("You made it, take control your database now!");
		} else {
			System.out.println("Failed to make connection!");
		}

		System.out.println("Connected to database");
		return connection;
	}
}





Tag : Java, JAVA









ประวัติการแก้ไข
2017-09-11 21:12:20
2017-09-11 21:12:47
2017-09-11 21:16:06
2017-09-11 21:28:55
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-09-11 21:10:20 By : shelby View : 510 Reply : 8
 

 

No. 1



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 2



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 

 

No. 3



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 4



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 5



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 6



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 7



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 


 

No. 8



โพสกระทู้ ( 10 )
บทความ ( 0 )



สถานะออฟไลน์


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
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : การทำ GUI/Insert/Update/Search/Delete ผ่าน Database
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: Voake, Comcube, รับทำเว็บไซต์ รับเขียนโปรแกรม

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2019 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 , 08-9968-0655 อัตราราคา คลิกที่นี่