import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ornek1 implements ActionListener{
DefaultListModel lad,lmiktar,lfiyat,lid;
JTextField jtfad,jtfmiktar,jtffiyat;
JList jl1;
ornek1(){
JFrame jfrm = new JFrame("Alışveriş Sepeti");
jfrm.setSize(400,320);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLayout(new GridLayout(1,3));
lad= new DefaultListModel();
lmiktar= new DefaultListModel();
lfiyat= new DefaultListModel();
JPanel jpn1= new JPanel();
jl1 = new JList(lad);
JScrollPane jsp1 = new JScrollPane(jl1);
jsp1.setPreferredSize(new Dimension(100,200));
jpn1.add(jsp1);
jtfad= new JTextField();
jtfad.setPreferredSize(new Dimension(100,20));
jpn1.add(jtfad);
JButton jbtn1 = new JButton("Ekle");
jbtn1.setActionCommand("ekle");
jbtn1.addActionListener(this);
jpn1.add(jbtn1);
JPanel jpn2= new JPanel();
JList jl2 = new JList(lmiktar);
JScrollPane jsp2 = new JScrollPane(jl2);
jsp2.setPreferredSize(new Dimension(100,200));
jpn2.add(jsp2);
jtfmiktar= new JTextField();
jtfmiktar.setPreferredSize(new Dimension(100,20));
jpn2.add(jtfmiktar);
JButton jbtn2 = new JButton("Sil");
jbtn2.setActionCommand("sil");
jbtn2.addActionListener(this);
jpn2.add(jbtn2);
JPanel jpn3= new JPanel();
JList jl3 = new JList(lfiyat);
JScrollPane jsp3 = new JScrollPane(jl3);
jsp3.setPreferredSize(new Dimension(100,200));
jpn3.add(jsp3);
jtffiyat= new JTextField();
jtffiyat.setPreferredSize(new Dimension(100,20));
jpn3.add(jtffiyat);
JButton jbtn3 = new JButton("Güncelle");
jbtn3.setActionCommand("guncelle");
jbtn3.addActionListener(this);
jpn3.add(jbtn3);
jfrm.add(jpn1);
jfrm.add(jpn2);
jfrm.add(jpn3);
verigetir();
jfrm.setVisible(true);
}
public Connection baglan()
{
Connection conn=null;
try{
Class.forName("org.sqlite.JDBC");
conn=DriverManager.getConnection("jdbc:sqlite:hafta7.db");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0);
}
return conn;
}
public void verigetir()
{
Connection conn=null;
try{
conn = baglan();
lid = new DefaultListModel();
String sql = "select id,ad,miktar,fiyat from sepet;";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
lad.clear();
lmiktar.clear();
lfiyat.clear();
while(rs.next())
{
lid.addElement(rs.getInt("id"));
lad.addElement(rs.getString("ad"));
lmiktar.addElement(rs.getString("miktar"));
lfiyat.addElement(rs.getString("fiyat"));
}
}
catch(Exception ex) {}
}
public int alankontrol()
{
if(jtfad.getText().length()>0 && jtfmiktar.getText().length()>0 && jtffiyat.getText().length()>0){
int deger=-1;
try{
deger=Integer.parseInt(jtffiyat.getText());
}catch(Exception ex){
JOptionPane.showMessageDialog(null,"Fiyat numerik olmalı!","Hata",0);
}
return deger;
}
return -1;
}
public void actionPerformed(ActionEvent ae)
{
Connection conn = baglan();
if(ae.getActionCommand().equals("ekle"))
{
try{
int fiyat=alankontrol();
if(fiyat>-1)
{
String sql="insert into sepet(ad,miktar,fiyat) values(?,?,?);";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,jtfad.getText());
ps.setString(2,jtfmiktar.getText());
ps.setInt(3,fiyat);
ps.executeUpdate();
jtfad.setText("");
jtfmiktar.setText("");
jtffiyat.setText("");
}
}
catch(Exception ex){}
} else
if(ae.getActionCommand().equals("sil"))
{
try{
int selected=jl1.getSelectedIndex();
if(selected>-1)
{
int index=(int)lid.get(selected);
String sql="delete from sepet where id=?;";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,index);
ps.executeUpdate();
}
}
catch(Exception ex){}
} else
if(ae.getActionCommand().equals("guncelle"))
{
try{
int selected=jl1.getSelectedIndex();
int fiyat=alankontrol();
if(selected>-1 && fiyat>-1)
{
int index=(int)lid.get(selected);
String sql="update sepet set ad=?,miktar=?, fiyat=? where id=?;";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,jtfad.getText());
ps.setString(2,jtfmiktar.getText());
ps.setInt(3,fiyat);
ps.setInt(4,index);
ps.executeUpdate();
jtfad.setText("");
jtfmiktar.setText("");
jtffiyat.setText("");
}
}
catch(Exception ex){}
}
try
{
conn.close();
}
catch(Exception ex){}
verigetir();
}
public static void main(String[] asdasd)
{
new ornek1();
}
}
Java2 2015 Hafta-7 Eklentiler