Ok. Poddaje się, chyba robię coś źle. Przy kompilacji nie dostaje errorów, ale dane się nie wyświetlają.
Application.java
Kopiuj
package com.desing.demo1;
import java.sql.SQLException;
import javax.swing.SwingUtilities;
import com.desing.demo1.controller.Controller;
import com.desing.demo1.model.MySQLPersonDAO;
import com.desing.demo1.model.Model;
import com.desing.demo1.view.View;
public class Application {
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
try {
runApp();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void runApp() throws SQLException, Exception
{
Model model = new Model();
View view = new View(model);
Controller controller = new Controller(view, model);
}
}
Model.java
Kopiuj
package com.desing.demo1.model;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Model {
private Set<Person> people = new HashSet<Person>();
public List<Person> getPeople() {
return new ArrayList<Person>(people);
}
public void load() throws Exception {
DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);
PersonDAO personDAO = factory.getPersonDAO();
people.clear();
people.addAll(personDAO.getPeople());
}
}
Controller.java
Kopiuj
package com.desing.demo1.controller;
import java.sql.SQLException;
import java.util.List;
import com.desing.demo1.model.MySQLPersonDAO;
import com.desing.demo1.model.Database;
import com.desing.demo1.model.Model;
import com.desing.demo1.model.Person;
import com.desing.demo1.model.PersonDAO;
import com.desing.demo1.view.AppListener;
import com.desing.demo1.view.CreateUserEvent;
import com.desing.demo1.view.CreateUserListener;
import com.desing.demo1.view.LoginFormEvent;
import com.desing.demo1.view.LoginListener;
import com.desing.demo1.view.View;
public class Controller implements CreateUserListener, AppListener {
private View view;
private Model model;
private MySQLPersonDAO dbPersonDAO = new MySQLPersonDAO();
public Controller(View view, Model model) throws SQLException, Exception {
this.view = view;
this.model = model;
}
public void userCreate(CreateUserEvent event) {
System.out.println("Login event " + event.getName() + ", " + event.getPassword());
}
public void getCategory() throws Exception{
model.load();
}
public void onOpen() {
try {
Database.getInstance().connect();
} catch (Exception e) {
view.showError("Cannot connect to database.");
}
}
public void onClose() {
Database.getInstance().disconnect();
}
}
Fragment klasy mapującej
Person.java
Kopiuj
public class Person {
private int id;
private String name;
private String password;
public Person(int id, String name, String password)
{
this.name = name;
this.id = id;
this.password = password;
}
public int getId() {
return id;
}
[...]
View.java
Tutaj chodzi o wywołanie metody categoryShow(), która się wywołuje, ale nie wyświetla danych
Kopiuj
package com.desing.demo1.view;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.desing.demo1.controller.Controller;
import com.desing.demo1.model.Database;
import com.desing.demo1.model.Model;
import com.desing.demo1.model.Person;
import com.desing.demo1.view.CreateUserListener;
import java.awt.*;
import java.util.List;
import javax.swing.*;
public class View extends JFrame implements ActionListener,CategoryListener, CompanyListener, ClientsListener {
private static final long serialVersionUID = 1L;
private Model model;
private AppListener appListener;
private CategoryListener CategoryListener;
private CompanyListener CompanyListener;
private ClientsListener ClientsListener;
private CreateUserListener createUserListener;
private DefaultListModel<Person> listModel;
private JList<Person> userList;
JTabbedPane tabbedPane = new JTabbedPane();
JPanel card1 = new JPanel();
JPanel card2 = new JPanel();
public View(Model model) throws HeadlessException {
super("MVC Demo");
this.model = model;
listModel = new DefaultListModel<Person>();
userList = new JList<Person>(listModel);
addComponentToPane(getContentPane());
pack();
setSize(600,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowOpened(WindowEvent e)
{
fireOpenEvent();
try{
Database.getInstance().connect();
}catch (Exception e1) {
JOptionPane.showMessageDialog(View.this, "Nie mozna sie polaczyc z baza danych","Error", JOptionPane.WARNING_MESSAGE);
e1.printStackTrace();
}
}
public void windowClosing(WindowEvent e)
{
fileCloseEvent();
System.out.println("Zamknieto polaczenie ziomek");
try{
Database.getInstance().disconnect();
}catch(Exception e2){
JOptionPane.showMessageDialog(View.this, "Nie można zakonczyć połączenia z baza danych","Error", JOptionPane.WARNING_MESSAGE);
}
}
});
}
private void fireOpenEvent() {
if (appListener != null) {
appListener.onOpen();
}
}
private void fileCloseEvent() {
if (appListener != null) {
appListener.onClose();
}
}
public void addComponentToPane(Container pane)
{
tabbedPane.addTab("Lista kategorii", card1);
tabbedPane.addTab("Lista firm", card2);
categoryShow();
clientsShow();
companyShow();
pane.add(tabbedPane, BorderLayout.CENTER);
}
public void setAppListener(AppListener appListener)
{
this.appListener = appListener;
}
public void setUserListener(CreateUserListener createUserListener) {
this.createUserListener = createUserListener;
}
public void showError(String string) {
System.out.println(string);
}
public void actionPerformed(ActionEvent e) {
}
public void clientsShow() {
}
public void companyShow() {
}
public void categoryShow() {
System.out.println("out");
listModel.clear();
List<Person> people = model.getPeople();
for (Person person : people) {
listModel.addElement(person);
System.out.println("przejscie");
}
card1.add(new JScrollPane(userList));
}
}
Oraz klasa DAO
Kopiuj
package com.desing.demo1.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class MySQLPersonDAO implements PersonDAO{
public List<Person> getPeople() throws SQLException
{
List<Person> people = new ArrayList<Person>();
Connection conn = Database.getInstance().getConnection();
System.out.println(conn);
String sql = "select id, name, password from people";
Statement selectStatement = conn.createStatement();
ResultSet results = selectStatement.executeQuery(sql);
while(results.next())
{
int id = results.getInt("id");
String name = results.getString("name");
String password = results.getString("password");
Person person = new Person(id, name, password);
people.add(person);
}
results.close();
selectStatement.close();
return people;
}
}
Edit:
Pacz z projektem http://www46.zippyshare.com/v/11932083/file.html
niezdecydowanyniezdecydowany