Implemented a function to insert new users into the sqlite database.
Started to implement and design the GUI.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package de.katho.kBorrow;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
import de.katho.kBorrow.db.DbConnector;
|
||||
import de.katho.kBorrow.db.SqlConnector;
|
||||
@@ -9,6 +12,7 @@ import de.katho.kBorrow.gui.MainWindow;
|
||||
|
||||
public class Main {
|
||||
private DbConnector dbCon;
|
||||
private Settings set;
|
||||
|
||||
public static void main(String[] args){
|
||||
new Main();
|
||||
@@ -18,24 +22,41 @@ public class Main {
|
||||
/*
|
||||
* Create the apps main window.
|
||||
*/
|
||||
Settings set = new Settings();
|
||||
this.set = new Settings();
|
||||
|
||||
if(set.getProperty("dBType").equals("sqlite")){
|
||||
try {
|
||||
this.dbCon = new SqliteConnector(set.getProperty("sqlitePath"));
|
||||
} catch (ClassNotFoundException | SQLException | IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
else if(set.getProperty("dBType").equals("mysql")) {
|
||||
this.dbCon = new SqlConnector();
|
||||
}
|
||||
try {
|
||||
MainWindow window = new MainWindow(this);
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
/*EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
MainWindow window = new MainWindow();
|
||||
MainWindow window = new MainWindow((Main)this);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
});*/
|
||||
}
|
||||
|
||||
public boolean userSave(String pName, String pSurname) {
|
||||
return this.dbCon.createUser(pName, pSurname);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@ package de.katho.kBorrow.db;
|
||||
|
||||
public interface DbConnector {
|
||||
|
||||
public boolean createUser(String pName, String pSurname);
|
||||
}
|
||||
|
||||
@@ -2,4 +2,10 @@ package de.katho.kBorrow.db;
|
||||
|
||||
public class SqlConnector implements DbConnector{
|
||||
|
||||
@Override
|
||||
public boolean createUser(String pName, String pSurname) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,12 +26,11 @@ public class SqliteConnector implements DbConnector {
|
||||
* @param pHandle This string contains the path to database file the connector has to use
|
||||
* @throws FileNotFoundException, SQLException
|
||||
*/
|
||||
public SqliteConnector(String pHandle) {
|
||||
public SqliteConnector(String pHandle) throws ClassNotFoundException, SQLException, IOException {
|
||||
|
||||
this.dbHandle = pHandle;
|
||||
this.sqlScheme = this.loadScheme();
|
||||
|
||||
try {
|
||||
File dbFile = new File(this.dbHandle);
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
|
||||
@@ -58,16 +57,6 @@ public class SqliteConnector implements DbConnector {
|
||||
this.initNewDB(this.sqlScheme, this.connection);
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e){
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidDB(Hashtable<String, String> pScheme, Connection pConn){
|
||||
try {
|
||||
@@ -196,4 +185,19 @@ public class SqliteConnector implements DbConnector {
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
public boolean createUser(String pName, String pSurname){
|
||||
try {
|
||||
Statement st = this.connection.createStatement();
|
||||
String query = "INSERT INTO user (name, surname) VALUES ('"+pName+"', '"+pSurname+"')";
|
||||
|
||||
st.executeUpdate(query);
|
||||
|
||||
return true;
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,48 +2,180 @@ package de.katho.kBorrow.gui;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
public class MainWindow {
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
|
||||
|
||||
import javax.swing.border.TitledBorder;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JButton;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
|
||||
|
||||
|
||||
import de.katho.kBorrow.Main;
|
||||
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
public class MainWindow implements ActionListener {
|
||||
|
||||
private Main mainObject;
|
||||
private boolean userModeSave = true;
|
||||
|
||||
private JFrame frame;
|
||||
private JTextField textField;
|
||||
private JTextField textField_1;
|
||||
private JTabbedPane tabbedPane;
|
||||
private JPanel panelUserEdit;
|
||||
private JLabel lblUserName;
|
||||
private JLabel lblUserSurname;
|
||||
private JTextField textFieldUserName;
|
||||
private JTextField textFieldUserSurname;
|
||||
private JButton btnUserSave;
|
||||
private JLabel lblUserStatus;
|
||||
private JTable userTable;
|
||||
private JScrollPane scrollUserList;
|
||||
private JPanel panelUserList;
|
||||
|
||||
/**
|
||||
* Create the application.
|
||||
* @throws UnsupportedLookAndFeelException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InstantiationException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public MainWindow() {
|
||||
public MainWindow(Main pMainObject) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
|
||||
this.mainObject = pMainObject;
|
||||
initialize();
|
||||
this.frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the contents of the frame.
|
||||
* @throws UnsupportedLookAndFeelException
|
||||
* @throws IllegalAccessException
|
||||
* @throws InstantiationException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
private void initialize() {
|
||||
frame = new JFrame();
|
||||
frame.setBounds(100, 100, 450, 300);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
private void initialize() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
|
||||
this.frame = new JFrame();
|
||||
frame.setResizable(false);
|
||||
this.frame.setBounds(100, 100, 600, 500);
|
||||
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
|
||||
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
|
||||
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
|
||||
this.tabbedPane = new JTabbedPane(JTabbedPane.TOP);
|
||||
this.frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
tabbedPane.addTab("New tab", null, panel, null);
|
||||
this.tabbedPane.addTab("New tab", null, panel, null);
|
||||
|
||||
textField_1 = new JTextField();
|
||||
panel.add(textField_1);
|
||||
textField_1.setColumns(10);
|
||||
this.textField_1 = new JTextField();
|
||||
panel.add(this.textField_1);
|
||||
this.textField_1.setColumns(10);
|
||||
|
||||
textField = new JTextField();
|
||||
panel.add(textField);
|
||||
textField.setColumns(10);
|
||||
this.textField = new JTextField();
|
||||
panel.add(this.textField);
|
||||
this.textField.setColumns(10);
|
||||
|
||||
JPanel panel_1 = new JPanel();
|
||||
tabbedPane.addTab("New tab", null, panel_1, null);
|
||||
this.initUserTab();
|
||||
}
|
||||
|
||||
private void initUserTab(){
|
||||
JPanel panelUser = new JPanel();
|
||||
this.tabbedPane.addTab("Benutzer verwalten", null, panelUser, null);
|
||||
|
||||
panelUser.setLayout(null);
|
||||
|
||||
panelUserList = new JPanel();
|
||||
panelUserList.setBounds(0, 0, 589, 344);
|
||||
panelUserList.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Benutzerliste", TitledBorder.LEADING, TitledBorder.TOP, null, null));
|
||||
panelUser.add(panelUserList);
|
||||
panelUserList.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
userTable = new JTable(new UserTableModel());
|
||||
userTable.setFillsViewportHeight(true);
|
||||
|
||||
scrollUserList = new JScrollPane(userTable);
|
||||
panelUserList.add(scrollUserList);
|
||||
scrollUserList.setViewportBorder(null);
|
||||
|
||||
// User-Edit-Pane
|
||||
panelUserEdit = new JPanel();
|
||||
panelUserEdit.setBounds(0, 355, 589, 87);
|
||||
panelUserEdit.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Benutzer hinzuf\u00FCgen / bearbeiten", TitledBorder.LEADING, TitledBorder.TOP, null, null));
|
||||
panelUserEdit.setLayout(null);
|
||||
panelUser.add(this.panelUserEdit);
|
||||
|
||||
lblUserName = new JLabel("Vorname");
|
||||
lblUserName.setBounds(6, 27, 55, 20);
|
||||
|
||||
textFieldUserName = new JTextField();
|
||||
textFieldUserName.setBounds(71, 27, 120, 20);
|
||||
textFieldUserName.setColumns(10);
|
||||
|
||||
lblUserSurname = new JLabel("Nachname");
|
||||
lblUserSurname.setBounds(6, 58, 62, 20);
|
||||
|
||||
textFieldUserSurname= new JTextField();
|
||||
textFieldUserSurname.setBounds(70, 58, 121, 20);
|
||||
textFieldUserSurname.setColumns(10);
|
||||
|
||||
btnUserSave = new JButton("Speichern");
|
||||
btnUserSave.addActionListener(this);
|
||||
btnUserSave.setBounds(333, 58, 86, 20);
|
||||
|
||||
lblUserStatus = new JLabel("");
|
||||
lblUserStatus.setBounds(6, 89, 413, 14);
|
||||
|
||||
|
||||
panelUserEdit.add(this.lblUserName);
|
||||
panelUserEdit.add(this.textFieldUserName);
|
||||
panelUserEdit.add(this.lblUserSurname);
|
||||
panelUserEdit.add(this.textFieldUserSurname);
|
||||
panelUserEdit.add(this.textFieldUserSurname);
|
||||
panelUserEdit.add(this.btnUserSave);
|
||||
panelUserEdit.add(this.lblUserStatus);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(e.getSource() == this.btnUserSave){
|
||||
if(this.userModeSave){
|
||||
if(this.mainObject.userSave(this.textFieldUserName.getText(), this.textFieldUserSurname.getText())){
|
||||
this.lblUserStatus.setText("Benutzer \""+this.textFieldUserName.getText()+" "+this.textFieldUserSurname.getText()+"\" erfolgreich hinzugef<65>gt.");
|
||||
this.textFieldUserName.setText("");
|
||||
this.textFieldUserSurname.setText("");
|
||||
}
|
||||
else {
|
||||
this.lblUserStatus.setText("Benutzer konnte nicht erstellt werden.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Hier kommt die Funktion rein, die Benutzer editieren l<>sst.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
30
src/de/katho/kBorrow/gui/UserTableModel.java
Normal file
30
src/de/katho/kBorrow/gui/UserTableModel.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package de.katho.kBorrow.gui;
|
||||
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
public class UserTableModel extends AbstractTableModel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 435829735305533728L;
|
||||
|
||||
@Override
|
||||
public int getColumnCount() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValueAt(int arg0, int arg1) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user