ok, ich hab nun zumindest die meisten grundlagen so einigermaßen drauf, habe aber noch nie etwas größeres als ein oder zwei klassen programmiert.
nun möchte ich eben mal ein etwas größeres programm schreiben mit GUI, Datenbank usw.
hab nun schonmal angefangen und poste mal meinen bisherigen code.
ich wollte mal ein paar erfahrenere java prorgammierern bitten, mir zu sagen was noch nicht passt und was ich noch verbessern kann in bezug auf javadoc, allgemeiner aufbau, namen-wahl für klassen, methoden und variablen usw.
wäre für ein paar gute tips sehr dankbar.
nicht dass ich erst wenn ich viel weiter bin, zu hören bekomme, dass mein stil oder sonst etwas überhaut nicht passt oder so.
außerdem wollte ich die sachen noch etwas nach client und server trennen. im bezug auf GUI und datenbank muss man eben darauf achten, dass der client nur die GUI ist, und der server dann eigentlich alles weitere, also DB connection, abfragen und die ausgabe in tabellen, das gehört alles zum server.
ich mache am besten daher zwei packages, eins server, eins client.
liege ich da in etwa richtig?
also, wäre sehr dankbar für einige tips, was bei meinem code noch nicht so passt...
nun möchte ich eben mal ein etwas größeres programm schreiben mit GUI, Datenbank usw.
hab nun schonmal angefangen und poste mal meinen bisherigen code.
ich wollte mal ein paar erfahrenere java prorgammierern bitten, mir zu sagen was noch nicht passt und was ich noch verbessern kann in bezug auf javadoc, allgemeiner aufbau, namen-wahl für klassen, methoden und variablen usw.
wäre für ein paar gute tips sehr dankbar.
nicht dass ich erst wenn ich viel weiter bin, zu hören bekomme, dass mein stil oder sonst etwas überhaut nicht passt oder so.
außerdem wollte ich die sachen noch etwas nach client und server trennen. im bezug auf GUI und datenbank muss man eben darauf achten, dass der client nur die GUI ist, und der server dann eigentlich alles weitere, also DB connection, abfragen und die ausgabe in tabellen, das gehört alles zum server.
ich mache am besten daher zwei packages, eins server, eins client.
liege ich da in etwa richtig?
also, wäre sehr dankbar für einige tips, was bei meinem code noch nicht so passt...
Code:
package version1_1.copy.copy;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* StartWindow is the start of the Application. You can login and connect to the DB.
*
* @author CHUCKY
* @version 1.1 Build 02.01.2005
*/
public class StartWindow extends JFrame implements ActionListener
{
private JButton loginButton, dataBaseConnectionButton, exitButton;
private JPanel buttonPanel;
private JFrame startWindow;
private String login = "login", exit = "exit", connect = "connect";
private LoginWindow loginWindow;
private DataBaseConnection dataBaseConnection;
/**
* Creates a StartWindow.
*
* @param title Title of the Window.
*/
public StartWindow(String title)
{
super(title);
JFrame.setDefaultLookAndFeelDecorated(true);
Container contentPane = getContentPane();
buttonPanel = new JPanel(new BorderLayout());
buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 50, 320, 300));
addButtons();
contentPane.add(buttonPanel);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setSize(600, 400);
setVisible(true);
}
/**
* Adds the Buttons to the Window.
*
*/
public void addButtons()
{
loginButton = new JButton("Login");
loginButton.setActionCommand(login);
loginButton.addActionListener(this);
dataBaseConnectionButton = new JButton("Connect to DB");
dataBaseConnectionButton.setActionCommand(connect);
dataBaseConnectionButton.addActionListener(this);
dataBaseConnectionButton.setEnabled(true); //usually: setEnabled(false)
//only for test purpose
exitButton = new JButton("Exit");
exitButton.setActionCommand(exit);
exitButton.addActionListener(this);
buttonPanel.add(loginButton, BorderLayout.WEST);
buttonPanel.add(dataBaseConnectionButton, BorderLayout.CENTER);
buttonPanel.add(exitButton, BorderLayout.EAST);
}
/**
* Implements the method from the ActionListener Interface.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(exit))
{
System.out.println("Programm Exit.");
System.exit(0);
}
if (e.getActionCommand().equals(login))
{
System.out.println("Open Login Window.");
loginWindow = new LoginWindow(this);
}
if (e.getActionCommand().equals(connect))
{
System.out.println("Connect to DataBase.");
dataBaseConnection = new DataBaseConnection();
//dataBaseConnection.connectToDataBase();
//dataBaseConnection.doQuery();
new SQLWindow();
}
}
/**
* Enables the DataBaseConnectionButton if the login was commited correctly.
*
*/
public void checkDataBasePermission()
{
if (loginWindow.isAccessAllowed())
{
dataBaseConnectionButton.setEnabled(true);
}
}
}
/**
* LoginWindow opens to make the Login.
*
* You have to enter your name and password and then click the login button
* to login. the window will close after pressing the login button, either you have
* entered the correct nor wrong password.
*
* @author CHUCKY
* @version 1.1 Build 02.01.2005
*/
package version1_1.copy.copy;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JFrame;
public class LoginWindow extends JDialog implements ActionListener
{
private StartWindow startWindow;
private String cancel = "cancel", login = "login", name = "name",
password = "password";
private Container contentPane;
private JButton cancelButton, loginButton;
private JTextField nameTextField;
private JPasswordField passwordPasswordField;
private String[] nameArray = {"kai", "gabi"};
private String[] passwordArray = {"abc", "123"};
private boolean isAccessAllowed = false;
/**
* Creates a LoginWindow.
*
* @param startWindow Referenced StartWindow, which starts this constructor.
*/
public LoginWindow(StartWindow startWindow)
{
super(startWindow, "LoginWindow");
this.startWindow = startWindow;
setDefaultLookAndFeelDecorated(true);
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(4, 1));
addWidgets();
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
pack();
setSize(400, 200);
setVisible(true);
setResizable(false);
}
/**
* Adds all Widgets to the LoginWindow.
*
* Adds all Widgets like Buttons and TextFields to the LoginWindow.
*
*/
public void addWidgets()
{
cancelButton = new JButton("Cancel");
cancelButton.setActionCommand(cancel);
cancelButton.addActionListener(this);
contentPane.add(cancelButton);
nameTextField = new JTextField(10);
nameTextField.setActionCommand(name);
nameTextField.addActionListener(this);
contentPane.add(nameTextField);
passwordPasswordField = new JPasswordField(10);
passwordPasswordField.setActionCommand(password);
passwordPasswordField.addActionListener(this);
contentPane.add(passwordPasswordField);
loginButton = new JButton("Login");
loginButton.setActionCommand(login);
loginButton.addActionListener(this);
contentPane.add(loginButton);
}
/**
* Implements the method from the ActionListener Interface.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(cancel))
{
System.out.println("Login canceled.");
setVisible(false);
}
if (e.getActionCommand().equals(login))
{
if (checkStatus())
{
System.out.println("Login ok.");
isAccessAllowed = true;
startWindow.checkDataBasePermission();
}
else
{
System.out.println("Login failed. Login Window closed.");
}
setVisible(false);
}
if (e.getActionCommand().equals(name))
{
}
if (e.getActionCommand().equals(password))
{
}
}
/**
* Checks the Status for the Login.
*
* Checks if the entered name and password are correct and returns true if
* name and password are correct. if true, you are allowed to login.
*
* @return Returns true, if name and password are correct. login is allowed then.
*/
private boolean checkStatus()
{
boolean nameCorrect = false, passwordCorrect = false;
if (nameTextField.getText().length() > 0)
{
for (int i=0; i<nameArray.length; i++)
{
if (nameTextField.getText().equals(nameArray[i]))
{
System.out.println("name ok");
nameCorrect = true;
}
}
}
if (passwordPasswordField.getPassword().length > 0)
{
for (int i=0; i<passwordArray.length; i++)
{
if (passwordPasswordField.getText().equals(passwordArray[i]))
{
System.out.println("password ok");
passwordCorrect = true;
}
}
}
if (nameCorrect && passwordCorrect)
{
System.out.println("Login done.");
return true;
}
return false;
}
/**
* @return Returns the isAccessAllowed.
*/
public boolean isAccessAllowed() {
return isAccessAllowed;
}
}
package version1_1.copy.copy;
/**
* The DataBaseOperations Interface determines the database operations, which you can
* use in the SQL Window.
*
* @author CHUCKY
* @version 1.1 Build 03.01.2005
*/
public interface DataBaseOperations
{
/**
* Does the first query in this application. (only for test purposes)
*
*/
public void doQuery();
}
package version1_1.copy.copy;
/**
* DataBaseConnection connects to the MYSQL Database and opens a SQL Window with the
* result from the transaction, if the connection was done successfully.
*
* @author CHUCKY
* @version 1.1 Build 02.01.2005
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DataBaseConnection
{
private Connection conn = null;
private SQLWindow sqlWindow;
public DataBaseConnection()
{
}
/**
* Connects to the MYSQL DataBase and opens a SQLWindow with the result of the
* transaction, if the connection was done successfully.
*
*/
public void connectToDataBase()
{
String driver = "com.mysql.jdbc.Driver";
try
{
Class.forName(driver).newInstance();
System.out.println("Class Loaded.");
}
catch (Exception e)
{
System.out.println("Class not found.");
e.printStackTrace();
}
String password = "dummies";
String url = "jdbc:mysql://localhost:3306/";
String dataBase = "coffeebreak";
String user = "root";
try
{
conn = DriverManager.getConnection(url+dataBase, user, password);
System.out.println("Connection done.");
}
catch (SQLException sqle)
{
System.err.println("Connection failed with: " + url + dataBase + " "
+ user + " " + password);
System.err.println("Error message: " + sqle.getMessage());
}
}
public void doQuery()
{
Statement stmt = null;
ResultSet rset = null;
String doQuery = "SELECT COF_NAME FROM COFFEES";
String cof_name = "";
connectToDataBase();
try
{
stmt = conn.createStatement();
rset = stmt.executeQuery(doQuery);
while (rset.next())
{
cof_name = rset.getString("cof_name");
System.out.println(cof_name);
}
}
catch (SQLException sqle)
{
System.err.println("SQL Exception: ");
System.err.println(sqle.getMessage());
}
}
}
package version1_1.copy.copy;
/**
* Creates a new Window where you can make queries and manipulations to the
* connected database.
*
* @author CHUCKY
* @version 1.1 Build 02.01.2005
*/
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SQLWindow extends JFrame implements ActionListener
{
private JButton doQueryButton;
private DataBaseConnection dataBaseConnection;
//private DataBaseOperationsImpl dataBaseOperationsImpl;
private String doQuery = "doQuery";
public SQLWindow()
{
super("SQL Window");
JFrame.setDefaultLookAndFeelDecorated(true);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
addButtons();
contentPane.add(doQueryButton);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setSize(1000, 600);
setResizable(false);
setVisible(true);
}
public void addButtons()
{
doQueryButton = new JButton("Do a Query");
doQueryButton.setActionCommand(doQuery);
doQueryButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(doQuery))
{
dataBaseConnection = new DataBaseConnection();
dataBaseConnection.doQuery();
//dataBaseOperationsImpl = new DataBaseOperationsImpl();
//dataBaseOperationsImpl.doQuery();
}
}
}
package version1_1.copy.copy;
/**
* Starts the Application through the main method.
*
* @author CHUCKY
* @version 1.1 Build 02.01.2005
*/
public class Run
{
public static void main(String[] args)
{
new StartWindow("StartWindow");
}
}