Commits

synthomat committed 7b1a0bf Merge

JDBC-Strategie implementiert.

Comments (0)

Files changed (5)

src/controller/ControllerShop.java

 import view.ViewShop;
 
 import strategies.BinaryStrategy;
+import strategies.JDBCStrategy;
 import strategies.XMLStrategy;
 import strategies.XStreamStrategy;
 
 		view.mi_open_binary.addActionListener(openListener);
 		view.mi_open_beans.addActionListener(openListener);
 		view.mi_open_xstream.addActionListener(openListener);
-
+		view.mi_readJDBC.addActionListener(openListener);
+		view.mi_readJPA.addActionListener(openListener);
 		
 		ActionListener saveListener = new ActionListener() {
 			  public void actionPerformed(ActionEvent e) {
 		view.mi_save_binary.addActionListener(saveListener);
 		view.mi_save_beans.addActionListener(saveListener);
 		view.mi_save_xstream.addActionListener(saveListener);
+		view.mi_writeJDBC.addActionListener(saveListener);
+		view.mi_writeJPA.addActionListener(saveListener);
 
 	}
 
 		
 		if (e.getSource() == view.mi_open_binary)
 			strategy = new BinaryStrategy();
-		if (e.getSource() == view.mi_open_beans)
+		else if (e.getSource() == view.mi_open_beans)
 			strategy = new XMLStrategy();
-		if (e.getSource() == view.mi_open_xstream)
+		else if (e.getSource() == view.mi_open_xstream)
 			strategy = new XStreamStrategy();
+		else if (e.getSource() == view.mi_readJDBC)
+			strategy = new JDBCStrategy();
+		else
+			return;
 
 		try {
 			ProductList pl = (ProductList)strategy.readObject();
 		
 		if (e.getSource() == view.mi_save_binary)
 			strategy = new BinaryStrategy();
-		if (e.getSource() == view.mi_save_beans)
+		else if (e.getSource() == view.mi_save_beans)
 			strategy = new XMLStrategy();
-		if (e.getSource() == view.mi_save_xstream)
+		else if (e.getSource() == view.mi_save_xstream)
 			strategy = new XStreamStrategy();
-			
+		else if (e.getSource() == view.mi_writeJDBC)
+			strategy = new JDBCStrategy();
+		else
+			return;
+
 		try {
 			strategy.writeObject(model.getProducts());
 		} catch (IOException e1) {

src/db/JDBCConnector.java

 package db;
 
+import hs.Product;
+
 import java.sql.*;
+import java.util.ArrayList;
 
 public class JDBCConnector {
 
+	String url, user, password;
+	Connection con;
+	Statement stmt;
+	PreparedStatement pstmt = null;
 	
-	public JDBCConnector() {
-		// Den Datenbank-Treiber laden
-		String url = "jdbc:postgresql://is024sv.uni-duisburg.de/ws1011";
-		String user = "ws1011";
-		String password = "ftpw10";
-		String query = "select * from products";
+	
+//	String url = "jdbc:postgresql://is024sv.uni-duisburg.de/ws1011";
+//	String user = "ws1011";
+//	String password = "ftpw10";
+//	String query = "select * from products";	
+	
+	public JDBCConnector(String url, String user, String password) {
+		this.url = url;
+		this.user = user;
+		this.password = password;
 
-		
-		PreparedStatement pstmt = null;
 		try {
-
 			// JDBC Treiber registrieren
 			DriverManager.registerDriver(new org.postgresql.Driver());
 
-			// �ber den Treiber eine Verbindung mit der Datenbank aufbauen
-			Connection con = DriverManager.getConnection(url, user, password);
+			// Über den Treiber eine Verbindung mit der Datenbank aufbauen
+			con = DriverManager.getConnection(url, user, password);
+		} catch (SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+	
+	public void test() {
+		
+		
+		String query = "select * from products";
+		try {
+			// Ein SQL-Anweisungsobjekt erzeugen
+			stmt = con.createStatement();
 
-			// F�r Metadaten meta daklarieren
-			DatabaseMetaData meta = con.getMetaData();
+			// Für Metadaten meta daklarieren
+			DatabaseMetaData meta = con.getMetaData();			
 
-			// Ein SQL-Anweisungsobjekt erzeugen
-			Statement stmt = con.createStatement();
-
-			// SQL-Anweisungsobjekt f�hrt die SQL-Anweisung aus und schreibt
+			// SQL-Anweisungsobjekt f�hrt die SQL-Anweisung aus und schreibt
 			// das Resultat in das ResultSet-Objekt
 			ResultSet rs = stmt.executeQuery(query);
 			System.out.println("Ergebnis folgender SQL-Anweisung \n" + query
 					+ ":\n");
 
-			// ResultSetMetaData-Objekt verwaltet Informationen �ber das
+			// ResultSetMetaData-Objekt verwaltet Informationen �ber das
 			// Ergebnis
 			// einer SQL-Anweisung
 			ResultSetMetaData rsmd = rs.getMetaData();
 				System.out.println(res.getString("TABLE_NAME"));
 			}
 			res.close();
-			// SQL-Anweisungsobjekt schlie�en
+			// SQL-Anweisungsobjekt schlie�en
 			stmt.close();
 
-			// Datenbankverbindung schlie�en
+			// Datenbankverbindung schlie�en
 			con.close();
 
-			// Fngt die SQL-Fehlermeldung ab und schreibt diese auf die
+			// Fängt die SQL-Fehlermeldung ab und schreibt diese auf die
 			// Standardausgabe
 		} catch (SQLException ex) {
 			System.err.println("SQLException : " + ex.getMessage());
 		}
 	}
 
-	void insert(String name, double price, int quantity) {
-		// pstmt = con.prepareStatement("INSERT INTO products(name,price,quantity) VALUES (?,?,?)");
+	public void insert(String name, double price, int quantity) {
+		try {
+			pstmt = con.prepareStatement("INSERT INTO products (name, price, quantity) VALUES (?, ?, ?)");
+			pstmt.setString(1, name);
+			pstmt.setDouble(2, price);
+			pstmt.setInt(3, quantity);
+			pstmt.executeUpdate();
+			pstmt.close();
+		} catch (SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 	}
-}
+	
+	public void insert(Product product) {
+		insert(product.getName(), product.getPrice(), product.getQuantity());
+	}
+	
+	public Product read(long productId) {
+		Product product = new Product();
+		try {
+			Statement stmt = con.createStatement();
+			ResultSet rs = stmt.executeQuery("SELECT * FROM products WHERE id = " + productId);
+			
+			while (rs.next()) {
+				
+				// neues Product anlegen
+				product = new Product(
+						rs.getString("name"),
+						rs.getInt("quantity"),
+						rs.getDouble("price"));
+				break;
+			}
+			stmt.close();
+		} catch (SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		
+		return product;
+	}
+	
+	public ArrayList<Product> readLast(int count) {
+		ArrayList<Product> list = new ArrayList<Product>();
+		
+		try {
+			Statement stmt = con.createStatement();
+			ResultSet rs = stmt.executeQuery("SELECT * FROM products LIMIT " + count);
+			
+			while (rs.next()) {
+				
+				// neues Product anlegen
+				list.add(new Product(
+						rs.getString("name"),
+						rs.getInt("quantity"),
+						rs.getDouble("price")));
+			}
+			stmt.close();
+		} catch (SQLException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return list;
+	}
+	
+	public void close() {
+		if (con != null) {
+			try {
+				con.close();
+			} catch (SQLException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+	}
+}

src/hs/HardwareShop.java

 	public static void main(String[] args) {
 		ModelShop model = new ModelShop();
 		
-		//ViewShop viewShop = new ViewShop(model);
-		//ViewCustomer viewCustomer = new ViewCustomer(model);
+		ViewShop viewShop = new ViewShop(model);
+		ViewCustomer viewCustomer = new ViewCustomer(model);
 		
-		//ControllerShop controllerShop = new ControllerShop(model, viewShop);
+		ControllerShop controllerShop = new ControllerShop(model, viewShop);
 		//ControllerCustomer controllerCustomer = new ControllerCustomer(model, viewCustomer);
-		JDBCConnector c = new JDBCConnector();
 	}
 
 }

src/strategies/JDBCStrategy.java

+package strategies;
+import hs.Product;
+import hs.ProductList;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Iterator;
+
+import db.JDBCConnector;
+
+import fpt.com.SerializableStrategy;
+
+public class JDBCStrategy implements SerializableStrategy {
+
+	private JDBCConnector c;
+
+	public JDBCStrategy() {
+		
+		try {
+			open(Type.READ_WRITE);
+			c.test();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		
+	}
+	@Override
+	public Object readObject() throws IOException, ClassNotFoundException {
+		ProductList pl = new ProductList();
+
+		try {
+			open(Type.READ_WRITE);
+
+			// Produktliste befüllen
+            for ( Product p : c.readLast(50)) {
+            	System.out.println(p.getName());
+            	pl.add(p);
+            }
+
+		} finally { // Make sure to close
+			close(Type.READ_WRITE);
+		}
+
+		return pl;
+	}
+
+	@Override
+	public void writeObject(Serializable obj) throws IOException {
+		ProductList pl = (ProductList)obj;
+		try {
+			open(Type.READ_WRITE);
+		
+			Iterator<fpt.com.Product> iter = pl.iterator();
+			
+			while( iter.hasNext()) {
+				Product prod = (Product)iter.next();
+				c.insert(prod);				
+			}
+		} finally {
+			close(Type.READ_WRITE);
+		}
+	}
+
+	@Override
+	public boolean isOpen() {
+		return false;
+	}
+
+	@Override
+	public void close(Type type) throws IOException {
+		c.close();
+	}
+
+	@Override
+	public void open(Type type) throws IOException {
+		c = new JDBCConnector(
+				"jdbc:postgresql://is024sv.uni-duisburg.de/ws1011",
+				"ws1011",
+				"ftpw10");
+	}
+
+	@Override
+	public void restart(Type type) throws IOException {
+		close(Type.READ_WRITE);
+		open(Type.READ_WRITE);
+	}
+}

src/view/ViewShop.java

 	JMenuBar menuBar;
 	JMenu menu_file;
 	JMenu menu_onOf;
-	JMenu menu_open, menu_save;
-	public JMenuItem mi_open_binary, mi_open_beans, mi_open_xstream;
-	public JMenuItem mi_save_binary, mi_save_beans, mi_save_xstream;
+	JMenu menu_open, menu_save; 
+	public JMenuItem mi_open_binary, mi_open_beans, mi_open_xstream, mi_readJDBC, mi_readJPA;
+	public JMenuItem mi_save_binary, mi_save_beans, mi_save_xstream, mi_writeJDBC, mi_writeJPA;
 	JMenuItem mi_exit;
 	
 	JRadioButtonMenuItem mi_online, mi_offline;
 				mi_open_xstream = new JMenuItem("XStream");
 				menu_open.add(mi_open_xstream);
 			
+				menu_open.addSeparator();
+				
+			    mi_readJDBC = new JMenuItem("JDBC");
+				menu_open.add(mi_readJDBC);
+				mi_readJPA = new JMenuItem("OpenJPA");
+				menu_open.add(mi_readJPA);
+				
 				
 			menu_save = new JMenu("Save as");
 			menu_file.add(menu_save);
 				mi_save_xstream = new JMenuItem("XStream");
 				menu_save.add(mi_save_xstream);
 		
+				
+				menu_save.addSeparator();
+				
+			    mi_writeJDBC = new JMenuItem("JDBC");
+				menu_save.add(mi_writeJDBC);
+				mi_writeJPA = new JMenuItem("OpenJPA");
+				menu_save.add(mi_writeJPA);
+				
 			menu_file.addSeparator();
 			mi_exit = new JMenuItem("Exit");
 			menu_file.add(mi_exit);