Implemented a function to insert new users into the sqlite database.

Started to implement and design the GUI.
This commit is contained in:
Servicepoint
2014-10-07 18:39:23 +02:00
parent 1b07cbdf14
commit 60b96cb6cb
6 changed files with 246 additions and 52 deletions

View File

@@ -1,6 +1,9 @@
package de.katho.kBorrow; 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.DbConnector;
import de.katho.kBorrow.db.SqlConnector; import de.katho.kBorrow.db.SqlConnector;
@@ -9,6 +12,7 @@ import de.katho.kBorrow.gui.MainWindow;
public class Main { public class Main {
private DbConnector dbCon; private DbConnector dbCon;
private Settings set;
public static void main(String[] args){ public static void main(String[] args){
new Main(); new Main();
@@ -18,24 +22,41 @@ public class Main {
/* /*
* Create the apps main window. * Create the apps main window.
*/ */
Settings set = new Settings(); this.set = new Settings();
if(set.getProperty("dBType").equals("sqlite")){ if(set.getProperty("dBType").equals("sqlite")){
this.dbCon = new SqliteConnector(set.getProperty("sqlitePath")); 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")) { else if(set.getProperty("dBType").equals("mysql")) {
this.dbCon = new SqlConnector(); 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() { public void run() {
try { try {
MainWindow window = new MainWindow(); MainWindow window = new MainWindow((Main)this);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}); });*/
}
public boolean userSave(String pName, String pSurname) {
return this.dbCon.createUser(pName, pSurname);
} }
} }

View File

@@ -2,4 +2,5 @@ package de.katho.kBorrow.db;
public interface DbConnector { public interface DbConnector {
public boolean createUser(String pName, String pSurname);
} }

View File

@@ -2,4 +2,10 @@ package de.katho.kBorrow.db;
public class SqlConnector implements DbConnector{ public class SqlConnector implements DbConnector{
@Override
public boolean createUser(String pName, String pSurname) {
// TODO Auto-generated method stub
return false;
}
} }

View File

@@ -26,46 +26,35 @@ public class SqliteConnector implements DbConnector {
* @param pHandle This string contains the path to database file the connector has to use * @param pHandle This string contains the path to database file the connector has to use
* @throws FileNotFoundException, SQLException * @throws FileNotFoundException, SQLException
*/ */
public SqliteConnector(String pHandle) { public SqliteConnector(String pHandle) throws ClassNotFoundException, SQLException, IOException {
this.dbHandle = pHandle; this.dbHandle = pHandle;
this.sqlScheme = this.loadScheme(); this.sqlScheme = this.loadScheme();
try { File dbFile = new File(this.dbHandle);
File dbFile = new File(this.dbHandle); Class.forName("org.sqlite.JDBC");
Class.forName("org.sqlite.JDBC");
if(dbFile.exists()){ if(dbFile.exists()){
if(dbFile.isFile()){ if(dbFile.isFile()){
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
if(!this.isValidDB(this.sqlScheme, this.connection)){ if(!this.isValidDB(this.sqlScheme, this.connection)){
throw new SQLException("The given db file doesn't match the required sql schema."); throw new SQLException("The given db file doesn't match the required sql schema.");
}
else {
System.out.println("Db Scheme looks fine to me.");
}
} }
else { else {
throw new IOException("Provided db handle may not be a file but a directory or a symlink!"); System.out.println("Db Scheme looks fine to me.");
} }
} }
else { else {
System.out.println("There is no db file yet... creating a new db."); throw new IOException("Provided db handle may not be a file but a directory or a symlink!");
dbFile.createNewFile();
this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
this.initNewDB(this.sqlScheme, this.connection);
} }
} }
catch (ClassNotFoundException e){ else {
e.printStackTrace(); System.out.println("There is no db file yet... creating a new db.");
} catch (SQLException e) { dbFile.createNewFile();
// TODO Auto-generated catch block
e.printStackTrace(); this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle);
} catch (IOException e) { this.initNewDB(this.sqlScheme, this.connection);
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
@@ -196,4 +185,19 @@ public class SqliteConnector implements DbConnector {
return text.toString(); 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;
}
}
} }

View File

@@ -2,48 +2,180 @@ package de.katho.kBorrow.gui;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField; 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 JFrame frame;
private JTextField textField; private JTextField textField;
private JTextField textField_1; 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. * 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(); initialize();
this.frame.setVisible(true); this.frame.setVisible(true);
} }
/** /**
* Initialize the contents of the frame. * Initialize the contents of the frame.
* @throws UnsupportedLookAndFeelException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/ */
private void initialize() { private void initialize() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
frame = new JFrame(); this.frame = new JFrame();
frame.setBounds(100, 100, 450, 300); frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.frame.setBounds(100, 100, 600, 500);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); this.tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); this.frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
JPanel panel = new JPanel(); JPanel panel = new JPanel();
tabbedPane.addTab("New tab", null, panel, null); this.tabbedPane.addTab("New tab", null, panel, null);
textField_1 = new JTextField(); this.textField_1 = new JTextField();
panel.add(textField_1); panel.add(this.textField_1);
textField_1.setColumns(10); this.textField_1.setColumns(10);
textField = new JTextField(); this.textField = new JTextField();
panel.add(textField); panel.add(this.textField);
textField.setColumns(10); this.textField.setColumns(10);
JPanel panel_1 = new JPanel(); this.initUserTab();
tabbedPane.addTab("New tab", null, panel_1, null);
} }
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.
}
}
}
} }

View 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;
}
}