package displayeditor;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Main extends JFrame implements ActionListener
{
/* Gegevens voor verbinding */
protected final String dbUser = "***_editor";
protected final String dbPassword = "*****";
protected final String dbName = "deb20951_deeditor";
/* Gegevens voor verbinding */
protected String jdbcURL_erde = "jdbc:mysql://www.***.nl:3306/";
protected String jdbcDriver = "org.gjt.mm.mysql.Driver";
Container c;
JTextField t1,t2,t3,t4,tf;
JLabel l1,l2,l3,l4,lid,lb,logo,logobvl;
JButton b1,b2,b3,b4,b5,b6,b7,b8;
Connection con;
Statement st;
PreparedStatement ps;
ResultSet rs;
Vector v;
int index = 0;
public Main ()
//Tekstlabels aanmaken
{
c=getContentPane();
l1=new JLabel("");
l2=new JLabel("Display naam");
l3=new JLabel("Telefoonnummer");
l4=new JLabel("Notitie");
lid=new JLabel("");
logo=new JLabel("Created");
logobvl=new JLabel("BV.");
v=new Vector(16,1);
//Tekstvelden aanmaken
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
t4=new JTextField(10);
//Knoppen definieren en koppelen aan een methode
b1=new JButton("Nieuw"); b1.addActionListener(this);
b2=new JButton("Opslaan"); b2.addActionListener(this);
b3=new JButton("Verwijder"); b3.addActionListener(this);
b4=new JButton("Update"); b4.addActionListener(this);
b5=new JButton("Annuleren"); b5.addActionListener(this);
b6=new JButton("Volgende"); b6.addActionListener(this);
b7=new JButton("Vorige"); b7.addActionListener(this);
b8=new JButton("Sluit Programma"); b8.addActionListener(this);
//Plaats van de knoppen en velden. left,top,weidth,height
l2.setBounds(10,40,150,20); t2.setBounds(120,40,150,20);
l3.setBounds(10,70,150,20); t3.setBounds(120,70,150,20);
l4.setBounds(10,100,150,20); t4.setBounds(120,100,150,20);
b7.setBounds(10,150,100,40); b6.setBounds(120,150,100,40); b4.setBounds(230,150,100,40);
b1.setBounds(10,200,100,20); b2.setBounds(120,200,100,20); b3.setBounds(230,200,100,20);
b5.setBounds(10,230,100,20); b8.setBounds(120,230,210,20);
//Alles op z'n plaats
c.add(l2);c.add(l2);c.add(l3);c.add(l4);c.add(lid);
c.add(t1);c.add(t2);c.add(t3);c.add(t4);
c.add(b1);c.add(b2);c.add(b3);c.add(b4);c.add(b5);c.add(b6);c.add(b7);c.add(b8);
//logos op de plaats
logo.setForeground(Color.LIGHT_GRAY);
Font f1=new Font("Arial",Font.BOLD,13);
logo.setFont(f1);
c.add("South",logo);
logobvl.setForeground(Color.DARK_GRAY);
Font f2=new Font("Arial",Font.BOLD,24);
logobvl.setFont(f2);
c.add("North",logobvl);
l1.setBounds(1,1,1,1);
c.add(l1);
//ODBC connectie naar MySQL
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.print(
"connecting to the database 4" + jdbcURL_erde + dbName + " ... " );
con = DriverManager.getConnection(
jdbcURL_erde + dbName, dbUser, dbPassword );
System.out.println( "done" );
st=con.createStatement();
rs=st.executeQuery("select * from displayeditor order by de_ID desc");
while(rs.next())
{
v.addElement(rs.getString(1));
v.addElement(rs.getString(2));
v.addElement(rs.getString(3));
v.addElement(rs.getString(4));
}
}
catch(Exception ex)
{
System.out.println("error is:"+ex);
}
//aanmaken van het scherm
setTitle("Dave");
setSize(400,330);
setVisible(true);
}
public static void main(String ages[])
//aanroepen van de klasse
{
Main Main = new Main();
}
//De methodes achter de knoppen
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b8) // Sluiten Knop
{
int x;
x=JOptionPane.showConfirmDialog(this,"Weet u zeker dat u wilt afsluiten?","Afsluiten",JOptionPane.YES_NO_OPTION);
if(x==0)
System.exit(0);
}
else if(ae.getSource()==b1) //Nieuw
{
t1.requestFocus();
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
else if(ae.getSource()==b2) //Opslaan
{
String s1,s2,s3,s4;
s1=t1.getText();
s2=t2.getText();
s3=t3.getText();
s4=t4.getText();
if(s2.length()==0||s3.length()==0)
{
JOptionPane.showMessageDialog(null,"Vul Display naam en telefoonnummer","Vul juiste gegevens!",JOptionPane.INFORMATION_MESSAGE);
return; }
int response=JOptionPane.showConfirmDialog(null,"Wil je een nieuw record opslaan?","Weet je het zeker?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE );
if(response==0)
{
try
//opslaan van een nieuw record in MySQL
{
ps=con.prepareStatement("insert into displayeditor values(?,?,?,?)");
ps.setString(1, null);
ps.setString(2,s2);
ps.setString(3,s3);
ps.setString(4,s4);
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"Het is opgeslagen!");
}
catch(Exception e1){System.out.println(e1);}
} }
else if(ae.getSource()==b3) //Verwijderen
{
try
{
ps=con.prepareStatement("delete from displayeditor where de_ID=? ");
ps.setString(1,t1.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"Het is verwijderd!");
}
catch(Exception e2)
{
System.out.println("error is"+e2);
}
}
else if(ae.getSource()==b4)//update
{
try
{
ps=con.prepareStatement("UPDATE displayeditor SET de_DisplayNaam=?,de_TelefoonNummer=?,de_Notitie=? WHERE de_ID=?" );
ps.setString(1,t2.getText());
ps.setString(2,t3.getText());
ps.setString(3,t4.getText());
ps.setString(4,t1.getText());
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"De update is uitgevoerd!");
}
catch(Exception e4)
{
System.out.println("error is "+e4);
}
}
else if(ae.getSource()==b5) // Annuleren
{
t1.setEnabled(true);
int x;
x=JOptionPane.showConfirmDialog(this,"Wil je annuleren?","Annuleren",JOptionPane.YES_NO_OPTION);
if(x==0)
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
else if(ae.getSource()==b6) //Volgende
{
try
{
if(index==v.capacity()-1)
{
JOptionPane.showMessageDialog(this,"Sorry, laatste record");
}
else
{
t1.setText(""+v.elementAt(index));
index++;
t2.setText(""+v.elementAt(index));
index++;
t3.setText(""+v.elementAt(index));
index++;
t4.setText(""+v.elementAt(index));
index++;
}
}
catch(Exception e8)
{
System.out.print("error"+e8);
}
}
else if(ae.getSource()==b7) // Vorige
{
try
{
if(index==1)
{
JOptionPane.showMessageDialog(this,"Dit is het eerste record");
}
else
{
index=index-8;
t1.setText(""+v.elementAt(index));
index++;
t2.setText(""+v.elementAt(index));
index++;
t3.setText(""+v.elementAt(index));
index++;
t4.setText(""+v.elementAt(index));
index++;
}
}
catch(Exception e9)
{System.out.println("error"+e9);}
}
} }