package library;
import java.awt.*;
import java.sql.*;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Library extends JFrame {
private JPanel contentPane;
private final JList list1;
private final JList list2;
private final JList list3;
private final DefaultListModel defLiMod1 = new DefaultListModel();
private final DefaultListModel defLiMod2 = new DefaultListModel();
private final DefaultListModel defLiMod3 = new DefaultListModel();
private final JTextField txtTitle;
private final JTextField txtAuthor;
private final JTextField txtCategorie;
private final JTextField txtYear;
private final JLabel lblTitle;
private final JLabel lblAuthor;
private final JLabel lblCategorie;
private final JLabel lblYear;
private final JLabel lblAbout;
private final JEditorPane edAbout;
private final JScrollPane scAbout;
private final JButton btnLoad;
private final JButton btnAdd;
private final JLabel lblList1;
private final JLabel lblList2;
private final JLabel lblList3;
private final JScrollPane scList1;
private final JScrollPane scList2;
private final JScrollPane scList3;
private final JRadioButton rbtnSach;
private final JRadioButton rbtnFach;
private final JRadioButton rbtnRom;
private Connection connectLoad = null;
private PreparedStatement preparedStatementLoadSach = null;
private ResultSet resultSetLoadSach = null;
private PreparedStatement preparedStatementLoadFach = null;
private ResultSet resultSetLoadFach = null;
private PreparedStatement preparedStatementLoadRom = null;
private ResultSet resultSetLoadRom = null;
private java.sql.Statement statementLoadSach = null;
private java.sql.Statement statementLoadFach = null;
private java.sql.Statement statementLoadRom = null;
private Connection connectAdd = null;
private java.sql.Statement statementAdd = null;
private PreparedStatement preparedStatementAdd = null;
private ResultSet resultSetAdd = null;
// Password nicht vergessen
private final String url = "jdbc:mysql://localhost:3306/library?user=root&password=!&useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&autoReconnect=true&useSSL=false";
private final ButtonGroup buttonGroup = new ButtonGroup();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Library frame = new Library();
frame.setSize(1350, 1000);
frame.setResizable(false);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Library() {
// Frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1242, 615);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Other Components
lblTitle = new JLabel();
lblTitle.setBounds(960, 40, 50, 60);
lblTitle.setBackground(Color.WHITE);
lblTitle.setText("Title:");
lblTitle.setVisible(true);
contentPane.add(lblTitle);
txtTitle = new JTextField();
txtTitle.setBounds(960, 80, 300, 40);
txtTitle.setVisible(true);
contentPane.add(txtTitle);
lblAuthor = new JLabel();
lblAuthor.setBounds(960, 100, 81, 60);
lblAuthor.setBackground(Color.WHITE);
lblAuthor.setText("Author:");
lblAuthor.setVisible(true);
contentPane.add(lblAuthor);
txtAuthor = new JTextField();
txtAuthor.setBounds(960, 140, 300, 40);
txtAuthor.setVisible(true);
contentPane.add(txtAuthor);
lblCategorie = new JLabel();
lblCategorie.setBounds(960, 160, 100, 60);
lblCategorie.setBackground(Color.WHITE);
lblCategorie.setText("Kategory:");
lblCategorie.setVisible(true);
contentPane.add(lblCategorie);
txtCategorie = new JTextField();
txtCategorie.setBounds(960, 200, 300, 40);
txtCategorie.setVisible(true);
contentPane.add(txtCategorie);
lblYear = new JLabel();
lblYear.setBounds(960, 220, 100, 60);
lblYear.setBackground(Color.WHITE);
lblYear.setText("Year:");
lblYear.setVisible(true);
contentPane.add(lblYear);
txtYear = new JTextField();
txtYear.setBounds(960, 260, 300, 40);
txtYear.setVisible(true);
contentPane.add(txtYear);
lblAbout = new JLabel();
lblAbout.setBounds(960, 280, 300, 60);
lblAbout.setBackground(Color.WHITE);
lblAbout.setText("About:");
lblAbout.setVisible(true);
contentPane.add(lblAbout);
edAbout = new JEditorPane();
edAbout.setBounds(960, 320, 300, 200);
edAbout.setVisible(true);
scAbout = new JScrollPane(edAbout, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scAbout.setBounds(960, 320, 300, 200);
scAbout.setVisible(true);
contentPane.add(scAbout);
lblList1 = new JLabel();
lblList1.setBounds(0, 0, 300, 60);
lblList1.setText("Sachbücher");
lblList1.setVisible(true);
lblList1.setBackground(Color.WHITE);
contentPane.add(lblList1);
lblList2 = new JLabel();
lblList2.setBounds(320, 0, 300, 60);
lblList2.setText("Fachbücher");
lblList2.setVisible(true);
lblList2.setBackground(Color.WHITE);
contentPane.add(lblList2);
lblList3 = new JLabel();
lblList3.setBounds(640, 0, 300, 60);
lblList3.setText("Romane");
lblList3.setVisible(true);
lblList3.setBackground(Color.WHITE);
contentPane.add(lblList3);
list1 = new JList(defLiMod1);
list1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
moreInfo mi = new moreInfo();
mi.show(true);
}
}
});
list1.setBounds(0, 40, 300, 1000);
list1.setBackground(Color.WHITE);
list1.setVisible(true);
scList1 = new JScrollPane(list1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scList1.setBounds(0, 40, 300, 1000);
scList1.setVisible(true);
contentPane.add(scList1);
list2 = new JList(defLiMod2);
list1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
moreInfo mi = new moreInfo();
mi.show(true);
}
}
});
list2.setBounds(300, 40, 300, 1000);
list2.setBackground(Color.WHITE);
list2.setVisible(true);
scList2 = new JScrollPane(list2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scList2.setBounds(320, 40, 300, 1000);
scList2.setVisible(true);
contentPane.add(scList2);
list3 = new JList(defLiMod3);
list1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getClickCount() == 2) {
moreInfo mi = new moreInfo();
mi.show(true);
}
}
});
list3.setBounds(600, 40, 300, 1000);
list3.setBackground(Color.WHITE);
list3.setVisible(true);
scList3 = new JScrollPane(list3, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scList3.setBounds(640, 40, 300, 1000);
scList3.setVisible(true);
contentPane.add(scList3);
btnLoad = new JButton();
btnLoad.setBounds(1012, 550, 200, 60);
btnLoad.setText("Lade Bücher");
btnLoad.setVisible(true);
contentPane.add(btnLoad);
btnLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
loadBoocks();
} catch (Exception ex){
ex.printStackTrace();
System.out.println(ex);
}
}
});
rbtnSach = new JRadioButton();
buttonGroup.add(rbtnSach);
rbtnSach.setBounds(1012, 620, 200, 30);
rbtnSach.setText("Sachbuch");
contentPane.add(rbtnSach);
rbtnFach = new JRadioButton();
buttonGroup.add(rbtnFach);
rbtnFach.setBounds(1012, 640, 200, 30);
rbtnFach.setText("Fachbuch");
contentPane.add(rbtnFach);
rbtnRom = new JRadioButton();
buttonGroup.add(rbtnRom);
rbtnRom.setBounds(1012, 660, 200, 30);
rbtnRom.setText("Roman");
contentPane.add(rbtnRom);
btnAdd = new JButton();
btnAdd.setBounds(1012, 700, 200, 60);
btnAdd.setText("Einfügen");
btnAdd.setVisible(true);
contentPane.add(btnAdd);
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
addBoocks();
} catch (Exception ex){
ex.printStackTrace();
System.out.println(ex);
}
}
});
}
public void addBoocks() throws Exception {
String title = txtTitle.getText();
String author = txtAuthor.getText();
String categorie = txtCategorie.getText();
String year = txtYear.getText();
int yearINT = Integer.parseInt(year);
String about = edAbout.getText();
if (rbtnSach.isSelected() == true) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
statementAdd = con.createStatement();
statementAdd.executeUpdate("INSERT INTO sachbücher " + "VALUES (" + '"' + title + '"' + ", " + '"' + author + '"' + ", " + '"' +categorie + '"' + ", " + '"' + yearINT + '"' + ", " + '"' + about + '"' + ")");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
} else if(rbtnFach.isSelected() == true) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
statementAdd = con.createStatement();
statementAdd.executeUpdate("INSERT INTO fachbücher " + "VALUES (" + '"' + title + '"' + ", " + '"' + author + '"' + ", " + '"' +categorie + '"' + ", " + '"' + yearINT + '"' + ", " + '"' + about + '"' + ")");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
} else if (rbtnRom.isSelected() == true) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
statementAdd = con.createStatement();
statementAdd.executeUpdate("INSERT INTO romane " + "VALUES (" + '"' + title + '"' + ", " + '"' + author + '"' + ", " + '"' +categorie + '"' + ", " + '"' + yearINT + '"' + ", " + '"' + about + '"' + ")");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
}
public void loadBoocks() throws Exception{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url);
statementLoadSach = con.createStatement();
resultSetLoadSach = statementLoadSach.executeQuery("select Title from sachbücher");
statementLoadFach = con.createStatement();
resultSetLoadFach = statementLoadFach.executeQuery("select Title from fachbücher");
statementLoadRom = con.createStatement();
resultSetLoadRom = statementLoadRom.executeQuery("select Title from romane");
while (resultSetLoadSach.next())
{
String titleSach = resultSetLoadSach.getString("Title");
defLiMod1.addElement(titleSach);
}
while (resultSetLoadFach.next())
{
String titleFach = resultSetLoadFach.getString("Title");
defLiMod2.addElement(titleFach);
}
while (resultSetLoadRom.next())
{
String titleRom = resultSetLoadRom.getString("Title");
defLiMod3.addElement(titleRom);
}
System.out.println("Es hat geklappt");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
}