Note, some of the code could be hard to understand as a bit is in Swedish.
Hi!
I am trying to learn some sql/java. I created some tables in an sql-database, and a java-program to access and modify the tables. I am not that experienced in programming so I might have made some mistakes and I don't always know all the terminology. Anyway, here goes.
I made a GUI where you can switch from different tables using a scrollpane. I also have buttons and textfields for navigation and user input. I manged to find out how to change from different tables and how to add rows of data. I also ahve classes to run the program and classes to access to the database, and some methods in them,
This is how i show the table called Movies:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnShowMovies) {
try {
ResultSet rs = MysqlDB.statement
.executeQuery("SELECT name as 'Name', CategoryID as 'Category', price as 'Price' FROM Movie");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
tableModel.setDataVector(content, headers);
This is how I add rows to tables:
else if (e.getSource() == btnAddStaff) {
try {
int result = MysqlDB.statement
.executeUpdate("INSERT INTO Staff VALUES ('"+tfNumber.getText()+"', '"+tfFirstName.getText()+"', '"+tfLastName.getText()+"' )");
It seems a bit more complicated to delete rows and to change data in them. Mainly I am not sure how I should make the program understand which row that I want to change or delete. If it is possible to select a row in the scrollpane or somehow search in an array after the movie or staff that I want to change, I make the input using Textforms.
Any help would be great.
/Marcus
Complete code:
package projekt;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class DBMethods {
public static String[] getHeaders(ResultSet rs) throws SQLException {
ResultSetMetaData meta;
String[] headers;
meta = rs.getMetaData();
headers = new String[meta.getColumnCount()];
for(int i=0; i<headers.length; i++) {
headers[i] = meta.getColumnLabel(i+1);
}
return headers;
}
public static Object[][] getContent(ResultSet rs) throws SQLException {
ResultSetMetaData rsmt;
Object[][] content;
int rows, cols;
rsmt = rs.getMetaData();
rs.last();
rows = rs.getRow();
cols = rsmt.getColumnCount();
content = new Object[rows][cols];
for(int row = 0; row<rows; row++) {
rs.absolute(row + 1); // Flytta till rätt rad i resultatmängden
for(int col = 0; col<cols; col++) {
content [row][col] = rs.getObject(col + 1);
}
}
return content;
}
public static void setColumnWidth(JTable table, int[] colWidth) {
TableColumnModel columnModel = table.getColumnModel();
int count = Math.min(table.getColumnCount(),colWidth.length);
for(int i=0; i<count; i++) {
columnModel.getColumn(i).setPreferredWidth(colWidth[i]);
}
}
}
package projekt;
import java.sql.*;
public class MysqlDB {
public static Connection connection;
public static Statement statement;
public static void showResultSet(ResultSet resultSet) throws SQLException {
ResultSetMetaData meta = resultSet.getMetaData();
String res = "";
int colCount = meta.getColumnCount();
for(int i=1; i<=colCount; i++)
res += meta.getColumnLabel(i) + ", ";
res += "\n";
while(resultSet.next()) {
for(int i=1; i<=colCount; i++)
res += resultSet.getObject(i).toString() + ", ";
res += "\n";
}
System.out.println(res);
}
public static void kopplaUpp() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
MysqlDB.connection = DriverManager.getConnection("jdbc:mysql://195.178.226.29:3306/da129a09c122439","DA129A09C122439","122439");
MysqlDB.statement = MysqlDB.connection.createStatement();
} catch(ClassNotFoundException e1) {
System.out.println("Databas-driver hittades ej: " +e1);
}
}
public static void kopplaNer() throws SQLException {
MysqlDB.statement.close();
MysqlDB.connection.close();
}
public static void main(String[] args) {
try {
MysqlDB.kopplaUpp();
ResultSet result = MysqlDB.statement.executeQuery("SELECT * FROM Person");
MysqlDB.showResultSet(result);
MysqlDB.kopplaNer();
} catch(SQLException e) {
System.out.println(e);
}
}
}
package projekt;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class ProjectController extends JFrame {
private ProjectGUI gui;
public ProjectController() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Projekt");
this.gui = new ProjectGUI(this);
getContentPane().add(gui, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ProjectController();
}
});
}
}
package projekt;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
public class ProjectGUI extends JPanel implements ActionListener {
private ProjectController controller;
private JTable table;
private DefaultTableModel tableModel;
private double betyg1 = 0, betyg2 = 0, betyg3 = 0;
private CardLayout cardLayout = new CardLayout();
private JPanel centerPanel= new JPanel(cardLayout);
//JPanels
private JPanel pnlMov1 = new JPanel(null);
private JPanel pnlMov2 = new JPanel(null);
private JPanel pnlMov3 = new JPanel(null);
private JPanel pnlActions = new JPanel(null);
private JPanel tbSelection = new JPanel ();
private JPanel tbNewStaff = new JPanel ();
private JPanel tbNewMovie = new JPanel ();
private JScrollPane scroll = new JScrollPane();
//JLabels
private JLabel lblBg = new JLabel(new ImageIcon("C:/Programmering/projekt/movieratings.png"));
private JLabel lblMov1 = new JLabel(new ImageIcon("C:/Programmering/projekt/jaws.jpg"));
private JLabel lblMov2 = new JLabel(new ImageIcon("C:/Programmering/projekt/deruntergang.jpg"));
private JLabel lblMov3 = new JLabel(new ImageIcon("C:/Programmering/projekt/harrypotter.jpg"));
private JLabel lblPoor1 = new JLabel( "Dålig" );
private JLabel lblBest1 = new JLabel( "Bäst" );
private JLabel lblRating1 = new JLabel();
private JLabel lblPoor2= new JLabel( "Dålig" );
private JLabel lblBest2 = new JLabel( "Bäst" );
private JLabel lblRating2 = new JLabel();
private JLabel lblPoor3= new JLabel( "Dålig" );
private JLabel lblBest3 = new JLabel( "Bäst" );
private JLabel lblRating3 = new JLabel();
private JLabel lblKategori = new JLabel ("Välj kategori");
private JLabel lblLogin = new JLabel ("Login: ");
private JLabel lblPassword = new JLabel ("Lösenord: ");
private JLabel lblTitle = new JLabel ("Sök titel");
private JLabel lblPersonnr = new JLabel ("Personnummer");
private JLabel lblFirstName = new JLabel ("Förnamn");
private JLabel lblLastName = new JLabel ("Efternamn");
private JLabel lblStreet = new JLabel ("Gatuadress");
private JLabel lblZip = new JLabel ("Postnummer");
private JLabel lblCity = new JLabel ("Ort");
private JLabel lblPhone1 = new JLabel ("Telefon 1");
private JLabel lblPhone2 = new JLabel ("Telefon 2");
private JLabel lblId = new JLabel ("Varu ID");
private JLabel lblMovieTitle = new JLabel ("Filmtitel");
private JLabel lblCategory = new JLabel ("Kategori ID");
private JLabel lblPrice = new JLabel ("Pris");
private JLabel lblQty = new JLabel ("Antal");
//JTextFields
private JTextField tfLogin = new JTextField ();
private JTextField tfPassword = new JTextField ();
private JTextField tfTitle = new JTextField ();
private JTextField tfPersonnr = new JTextField ();
private JTextField tfFirstName = new JTextField ();
private JTextField tfLastName = new JTextField ();
private JTextField tfStreet = new JTextField ();
private JTextField tfZip = new JTextField ();
private JTextField tfCity = new JTextField ();
private JTextField tfPhone1 = new JTextField ();
private JTextField tfPhone2 = new JTextField ();
private JTextField tfId = new JTextField ();
private JTextField tfMovieTitle = new JTextField ();
private JTextField tfCategory = new JTextField ();
private JTextField tfPrice = new JTextField ();
private JTextField tfQty = new JTextField ();
private Choice chsKategori = new Choice();
//JRadioButtons
private JRadioButton rb1Mov1 = new JRadioButton();
private JRadioButton rb2Mov1 = new JRadioButton();
private JRadioButton rb3Mov1 = new JRadioButton();
private JRadioButton rb4Mov1 = new JRadioButton();
private JRadioButton rb5Mov1 = new JRadioButton();
private JRadioButton rb1Mov2 = new JRadioButton();
private JRadioButton rb2Mov2 = new JRadioButton();
private JRadioButton rb3Mov2 = new JRadioButton();
private JRadioButton rb4Mov2 = new JRadioButton();
private JRadioButton rb5Mov2 = new JRadioButton();
private JRadioButton rb1Mov3 = new JRadioButton();
private JRadioButton rb2Mov3 = new JRadioButton();
private JRadioButton rb3Mov3 = new JRadioButton();
private JRadioButton rb4Mov3 = new JRadioButton();
private JRadioButton rb5Mov3 = new JRadioButton();
//JButtons
private JButton btnLogin = new JButton ("Logga in");
private JButton btnNewuser = new JButton ("Ny användare");
private JButton btnVote1 = new JButton("Rösta");
private JButton btnVote2 = new JButton("Rösta");
private JButton btnVote3 = new JButton("Rösta");
private JButton btnSelection = new JButton ("Sök");
private JButton btnRate = new JButton ("Visa topprankade");
private JButton btnAlfa = new JButton ("Välj");
private JButton btnShowStaff = new JButton ("Visa personal");
private JButton btnAddStaff = new JButton ("Lägg till");
private JButton btnChangeStaff = new JButton ("Ändra");
private JButton btnDeleteStaff = new JButton ("Ta bort");
private JButton btnShowMovies = new JButton ("Visa filmer");
private JButton btnAddMovie = new JButton ("Lägg till");
private JButton btnChangeMovie = new JButton ("Ändra");
private JButton btnDeleteMovie = new JButton ("Ta bort");
//Kommunicerar med ProjectController
public ProjectGUI(ProjectController controller) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
this.controller = controller;
// design av panelen
setPreferredSize(new Dimension(1000, 750));
setLayout(null);
try {
MysqlDB.kopplaUpp();
ResultSet rs = MysqlDB.statement
.executeQuery("SELECT namn as 'Namn', kategoriID as 'Kategori', pris as 'Pris' FROM Vara");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
// table = new JTable(content, headers);
tableModel = new DefaultTableModel(content, headers);
table = new JTable(tableModel);
scroll = new JScrollPane(table);
} catch (SQLException e) {}
lblBg.setBounds(0, 0, 1000, 750);
lblLogin.setBounds(790, 20, 80, 20);
lblLogin.setForeground( new Color( 255, 227, 218 ) );
lblLogin.setFont(new Font("SansSerif",Font.PLAIN,14));
tfLogin.setBounds(840, 20, 120, 20);
btnLogin.setBounds(840, 70, 120, 20);
btnNewuser.setBounds(840, 95, 120, 20);
//btnNewuser.setForeground( new Color( 255, 227, 218 ) );
//btnNewuser.setFont(new Font("SansSerif",Font.PLAIN,12));
lblPassword.setBounds(765, 45, 80, 20);
lblPassword.setForeground( new Color( 255, 227, 218 ) );
tfPassword.setBounds(840, 45, 120, 20);
lblPassword.setFont(new Font("SansSerif",Font.PLAIN,14));
//Actions
pnlActions.setBounds(0,390,1000,420);
pnlActions.setOpaque(false);
btnSelection.setBounds(420, 435, 200, 20);
btnRate.setBounds(420, 460, 200, 20);
btnAlfa.setBounds(420, 485, 200, 20);
lblKategori.setBounds(20, 460, 100, 30);
chsKategori.setBounds(20, 485, 250, 30);
chsKategori.add ("Alla");
chsKategori.add ("Thriller");
chsKategori.add ("Drama");
chsKategori.add ("Fantasy");
chsKategori.add ("Action");
//Lägger till JLabels och position
lblTitle.setBounds(20, 420, 220, 20);
add(lblTitle);
//Skapar TitledBorder till JPanels
tbSelection.setBorder(new TitledBorder(new EtchedBorder(), "Urval"));
tbSelection.setOpaque(false);
//Formaterar och anger position
tbSelection.setLayout(null);
tbSelection.setBounds(10, 400, 630, 150);
//Lägger till JScrollPane och position
scroll.setBounds(650,408,340,140);
//Lägger till textfält och position
tfTitle.setBounds(20, 445, 250, 20);
//Personal
//Skapar TitledBorder till JPanels
tbNewStaff.setBorder(new TitledBorder(new EtchedBorder(), "Ny personal", TitledBorder.DEFAULT_POSITION, TitledBorder.DEFAULT_POSITION, new Font ("Sanserif", Font.BOLD, 15), new Color( 255, 227, 218 )));
tbNewStaff.setForeground( new Color( 255, 227, 218 ) );
tbNewStaff.setOpaque(false);
tbNewMovie.setBorder(new TitledBorder(new EtchedBorder(), "Ny film", TitledBorder.DEFAULT_POSITION, TitledBorder.DEFAULT_POSITION, new Font ("Sanserif", Font.BOLD, 15), new Color( 255, 227, 218 )));
tbNewMovie.setForeground( new Color( 255, 227, 218 ) );
tbNewMovie.setOpaque(false);
//Formaterar och anger position
tbNewStaff.setLayout(null);
tbNewStaff.setBounds(10, 550, 630, 190);
tbNewMovie.setLayout(null);
tbNewMovie.setBounds(650, 550, 340, 190);
//Skapar JLabel och JTextField i Ny personal
lblPersonnr.setBounds(20, 580, 100, 20);
lblPersonnr.setForeground( new Color( 255, 227, 218 ) );
tfPersonnr.setBounds(20, 600, 180, 20);
lblFirstName.setBounds(220, 580, 100, 20);
lblFirstName.setForeground( new Color( 255, 227, 218 ) );
tfFirstName.setBounds(220, 600, 180, 20);
lblLastName.setBounds(440, 580, 100, 20);
lblLastName.setForeground( new Color( 255, 227, 218 ) );
tfLastName.setBounds(440, 600, 180, 20);
lblStreet.setBounds(20, 620, 100, 20);
lblStreet.setForeground( new Color( 255, 227, 218 ) );
tfStreet.setBounds(20, 640, 180, 20);
lblZip.setBounds(220, 620, 100, 20);
lblZip.setForeground( new Color( 255, 227, 218 ) );
tfZip.setBounds(220, 640, 180, 20);
lblCity.setBounds(440, 620, 100, 20);
lblCity.setForeground( new Color( 255, 227, 218 ) );
tfCity.setBounds(440, 640, 180, 20);
lblPhone1.setBounds(20, 660, 100, 20);
lblPhone1.setForeground( new Color( 255, 227, 218 ) );
tfPhone1.setBounds(20, 680, 180, 20);
lblPhone2.setBounds(220, 660, 100, 20);
lblPhone2.setForeground( new Color( 255, 227, 218 ) );
tfPhone2.setBounds(220, 680, 180, 20);
btnShowStaff.setBounds (120, 710, 100, 20);
btnShowStaff.setFont(new Font("SansSerif",Font.PLAIN,10));
btnAddStaff.setBounds(230, 710, 100, 20);
btnAddStaff.setFont(new Font("SansSerif",Font.PLAIN,10));
btnChangeStaff.setBounds(340, 710, 100, 20);
btnChangeStaff.setFont(new Font("SansSerif",Font.PLAIN,10));
btnDeleteStaff.setBounds(450, 710, 100, 20);
btnDeleteStaff.setFont(new Font("SansSerif",Font.PLAIN,10));
//Skapar JLabel och JTextField i Ny Film
lblId.setBounds(660, 580, 100, 20);
lblId.setForeground( new Color( 255, 227, 218 ) );
tfId.setBounds(660, 600, 140, 20);
lblMovieTitle.setBounds(830, 580, 100, 20);
lblMovieTitle.setForeground( new Color( 255, 227, 218 ) );
tfMovieTitle.setBounds(830, 600, 140, 20);
lblCategory.setBounds(660, 620, 100, 20);
lblCategory.setForeground( new Color( 255, 227, 218 ) );
tfCategory.setBounds(660, 640, 140, 20);
lblPrice.setBounds(660, 660, 100, 20);
lblPrice.setForeground( new Color( 255, 227, 218 ) );
tfPrice.setBounds(660, 680, 140, 20);
lblQty.setBounds(830, 660, 100, 20);
lblQty.setForeground( new Color( 255, 227, 218 ) );
tfQty.setBounds(830, 680, 140, 20);
btnShowMovies.setBounds(658, 710, 80, 20);
btnShowMovies.setFont(new Font("SansSerif",Font.PLAIN,9));
btnAddMovie.setBounds(745, 710, 75, 20);
btnAddMovie.setFont(new Font("SansSerif",Font.PLAIN,9));
btnChangeMovie.setBounds (828, 710, 75, 20);
btnChangeMovie.setFont(new Font("SansSerif",Font.PLAIN,9));
btnDeleteMovie.setBounds(910, 710, 75, 20);
btnDeleteMovie.setFont(new Font("SansSerif",Font.PLAIN,9));
//Film 1
pnlMov1.setBounds(180,135,170,245);
pnlMov1.setBackground(Color.black);
lblPoor1.setBounds(190, 324, 40, 30);
lblPoor1.setForeground( new Color( 255, 227, 218 ) );
lblPoor1.setFont(new Font("SansSerif",Font.PLAIN,14));
lblBest1.setBounds(310, 324, 50, 30);
lblBest1.setForeground( new Color( 255, 227, 218 ) );
lblBest1.setFont(new Font("SansSerif",Font.PLAIN,14));
lblRating1.setBounds(225, 130, 170, 40);
lblRating1.setForeground( new Color( 255, 227, 218 ) );
lblRating1.setFont(new Font("SansSerif",Font.BOLD,16));
rb1Mov1.setBounds( 225, 330, 20, 20 );
rb2Mov1.setBounds( 240, 330, 20, 20 );
rb3Mov1.setBounds( 255, 330, 20, 20 );
rb4Mov1.setBounds( 270, 330, 20, 20 );
rb5Mov1.setBounds( 285, 330, 20, 20 );
rb1Mov1.setOpaque(false);
rb2Mov1.setOpaque(false);
rb3Mov1.setOpaque(false);
rb4Mov1.setOpaque(false);
rb5Mov1.setOpaque(false);
btnVote1.setBounds (230, 355, 70, 20);
lblMov1.setBounds(207,165,114,155);
//Film 2
pnlMov2.setBounds(420,135,170,245);
pnlMov2.setBackground(Color.black);
lblPoor2.setBounds(430, 324, 40, 30);
lblPoor2.setForeground( new Color( 255, 227, 218 ) );
lblPoor2.setFont(new Font("SansSerif",Font.PLAIN,14));
lblBest2.setBounds(550, 324, 50, 30);
lblBest2.setForeground( new Color( 255, 227, 218 ) );
lblBest2.setFont(new Font("SansSerif",Font.PLAIN,14));
lblRating2.setBounds(465, 130, 170, 40);
lblRating2.setForeground( new Color( 255, 227, 218 ) );
lblRating2.setFont(new Font("SansSerif",Font.BOLD,16));
rb1Mov2.setBounds( 465, 330, 20, 20 );
rb2Mov2.setBounds( 480, 330, 20, 20 );
rb3Mov2.setBounds( 495, 330, 20, 20 );
rb4Mov2.setBounds( 510, 330, 20, 20 );
rb5Mov2.setBounds( 525, 330, 20, 20 );
rb1Mov2.setOpaque(false);
rb2Mov2.setOpaque(false);
rb3Mov2.setOpaque(false);
rb4Mov2.setOpaque(false);
rb5Mov2.setOpaque(false);
btnVote2.setBounds (470, 355, 70, 20);
lblMov2.setBounds(447,165,114,155);
//Film 3
pnlMov3.setBounds(660,135,170,245);
pnlMov3.setBackground(Color.black);
lblPoor3.setBounds(670, 324, 40, 30);
lblPoor3.setForeground( new Color( 255, 227, 218 ) );
lblPoor3.setFont(new Font("SansSerif",Font.PLAIN,14));
lblBest3.setBounds(790, 324, 50, 30);
lblBest3.setForeground( new Color( 255, 227, 218 ) );
lblBest3.setFont(new Font("SansSerif",Font.PLAIN,14));
lblRating3.setBounds(705, 130, 170, 40);
lblRating3.setForeground( new Color( 255, 227, 218 ) );
lblRating3.setFont(new Font("SansSerif",Font.BOLD,16));
rb1Mov3.setBounds( 705, 330, 20, 20 );
rb2Mov3.setBounds( 720, 330, 20, 20 );
rb3Mov3.setBounds( 735, 330, 20, 20 );
rb4Mov3.setBounds( 750, 330, 20, 20 );
rb5Mov3.setBounds( 765, 330, 20, 20 );
rb1Mov3.setOpaque(false);
rb2Mov3.setOpaque(false);
rb3Mov3.setOpaque(false);
rb4Mov3.setOpaque(false);
rb5Mov3.setOpaque(false);
btnVote3.setBounds (710, 355, 70, 20);
lblMov3.setBounds(687,165,114,155);
// lägger till ovanstående funktioner
//Film 1
add (lblMov1);
add (rb1Mov1);
add (rb2Mov1);
add (rb3Mov1);
add (rb4Mov1);
add (rb5Mov1);
add (lblPoor1);
add (lblBest1);
add (lblRating1);
add (btnVote1);
add (pnlMov1);
//Film 2
add (lblMov2);
add (rb1Mov2);
add (rb2Mov2);
add (rb3Mov2);
add (rb4Mov2);
add (rb5Mov2);
add (lblPoor2);
add (lblBest2);
add (lblRating2);
add (btnVote2);
add (pnlMov2);
//Film 3
add (lblMov3);
add (rb1Mov3);
add (rb2Mov3);
add (rb3Mov3);
add (rb4Mov3);
add (rb5Mov3);
add (lblPoor3);
add (lblBest3);
add (lblRating3);
add (btnVote3);
add (pnlMov3);
//Actions
add(tfTitle);
add (lblTitle);
add (lblKategori);
add (chsKategori);
add (btnSelection);
add (btnRate);
add (btnAlfa);
add(tbSelection);
add(scroll);
add (pnlActions);
//Personal
add (lblPersonnr);
add (lblFirstName);
add (lblLastName);
add (lblStreet);
add (lblZip);
add (lblCity);
add (lblPhone1);
add (lblPhone2);
add (tfPersonnr);
add (tfFirstName);
add (tfLastName);
add (tfStreet);
add (tfZip);
add (tfCity);
add (tfPhone1);
add (tfPhone2);
add (btnShowStaff);
add (btnAddStaff);
add (btnChangeStaff);
add (btnDeleteStaff);
add (tbNewStaff);
//Ny film
add (lblId);
add (lblMovieTitle);
add (lblCategory);
add (lblPrice);
add (lblQty);
add (tfId);
add (tfMovieTitle);
add (tfCategory);
add (tfPrice);
add (tfQty);
add (btnShowMovies);
add (btnAddMovie);
add (btnChangeMovie);
add (btnDeleteMovie);
add (tbNewMovie);
//Resten
add (lblLogin);
add (tfLogin);
add (lblPassword);
add (tfPassword);
add (btnNewuser);
add (btnLogin);
add (lblBg);
//Lägger in betyg
lblRating1.setText("Betyg: " + betyg1);
lblRating2.setText("Betyg: " + betyg2);
lblRating3.setText("Betyg: " + betyg3);
// registrerar lyssnare
btnShowStaff.addActionListener(this);
btnAddStaff.addActionListener(this);
btnChangeStaff.addActionListener(this);
btnDeleteStaff.addActionListener(this);
btnShowMovies.addActionListener(this);
btnAddMovie.addActionListener(this);
btnDeleteMovie.addActionListener(this);
btnChangeMovie.addActionListener(this);
btnNewuser.addActionListener(this);
btnLogin.addActionListener(this);
}
// byta till sidan med JLabel
private class Show1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
cardLayout.show(centerPanel, "TEXTSIDA"); // val av synlig sida
}
}
// byta till sidan med JButton
private class Show2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
cardLayout.show(centerPanel, "KNAPPSIDA"); // val av synlig sida
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnShowMovies) {
try {
ResultSet rs = MysqlDB.statement
.executeQuery("SELECT namn as 'Namn', kategoriID as 'Kategori', pris as 'Pris' FROM Vara");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
tableModel.setDataVector(content, headers);
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnShowStaff) {
try {
ResultSet rs = MysqlDB.statement
.executeQuery("SELECT personnr as 'Personnr.', anvandarnamn as 'Användarnamn', losenord as 'Lösenord' FROM Personal");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
tableModel.setDataVector(content, headers);
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnAddStaff) {
try {
System.out.println("INSERT INTO Personal VALUES ("+tfPersonnr.getText()+", "+tfFirstName.getText()+", "+tfLastName.getText()+" )");
int result = MysqlDB.statement
.executeUpdate("INSERT INTO Personal VALUES ('"+tfPersonnr.getText()+"', '"+tfFirstName.getText()+"', '"+tfLastName.getText()+"' )");
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnChangeStaff) {
try {
ResultSet rs = MysqlDB.statement
.executeQuery("INSERT INTO Personal VALUES (tfPersonnr.getText, tfFirstName.getText, tfLastName.getText )");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
tableModel.setDataVector(content, headers);
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnAddMovie) {
try {
int result = MysqlDB.statement
.executeUpdate("INSERT INTO Vara VALUES ('"+tfId.getText()+"', '"+tfTitle.getText()+"', '"+tfCategory.getText()+"', '"+tfPrice.getText()+"', '"+tfQty.getText()+"' )");
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnChangeMovie) {
try {
ResultSet rs = MysqlDB.statement
.executeQuery("INSERT INTO Vara VALUES ()");
String[] headers = DBMethods.getHeaders(rs);
Object[][] content = DBMethods.getContent(rs);
tableModel.setDataVector(content, headers);
} catch (SQLException e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnDeleteMovie) {
try {
System.out.println("deltestaff");
} catch (Exception e1) {
System.out.println(e1);
}
} else if (e.getSource() == btnDeleteStaff) {
try {
System.out.println("deltemovie");
} catch (Exception e1) {
System.out.println(e1);
}
}
}
}