import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import javax.swing.*;
public class market implements ActionListener {
JList[] jlist;
DefaultListModel[] dlm;
JComboBox jcb;
JTextField jtf;
JLabel jlab;
public market() {
JFrame jfrm = new JFrame("Market");
jfrm.setSize(250,275);
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpn_main = new JPanel();
jpn_main.setLayout(new GridLayout(1,3));
jpn_main.setPreferredSize(new Dimension(250,275));
JPanel[] jpn = new JPanel[3];
jlist = new JList[3];
dlm= new DefaultListModel[5]; // 3 tanesi jlist , +2 tanesi id ler icin (urunlerin id bilgisi, sepetin id bilgisi)
for(int i =0;i<jlist.length;i++)
{
jpn[i]= new JPanel();
dlm[i]= new DefaultListModel();
jlist[i]=new JList(dlm[i]);
JScrollPane jscp = new JScrollPane(jlist[i]);
jscp.setPreferredSize(new Dimension(80,200));
jpn[i].add(jscp);
jpn_main.add(jpn[i]);
}
jcb = new JComboBox();
jcb.setPreferredSize(new Dimension(80,25));
jtf= new JTextField();
jtf.setPreferredSize(new Dimension(80,25));
jlab = new JLabel("Tutar: 0");
jlab.setPreferredSize(new Dimension(80,25));
jpn[0].add(jcb);
jpn[1].add(jtf);
jpn[2].add(jlab);
JButton jbtn_ekle = new JButton("Ekle");
jbtn_ekle.addActionListener(this);
jbtn_ekle.setActionCommand("ekle");
jbtn_ekle.setPreferredSize(new Dimension(80,25));
JButton jbtn_sil = new JButton("Sil");
jbtn_sil.addActionListener(this);
jbtn_sil.setPreferredSize(new Dimension(80,25));
jpn[0].add(jbtn_ekle);
jpn[1].add(jbtn_sil);
jfrm.add(jpn_main);
combodoldur();
listedoldur();
jfrm.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("ekle"))
{
// ekle
Connection conn =baglan();
try {
String sql = "insert into sepet(urunid,miktar) values(?,?);";
int secilen = jcb.getSelectedIndex();
int miktar = Integer.parseInt(jtf.getText());
if(secilen>0)
{
int urunid = (int) dlm[3].get(secilen-1); // urunler icin id listesi;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,urunid);
ps.setInt(2,miktar);
ps.executeUpdate();
jcb.setSelectedIndex(0);
jtf.setText("");
listedoldur();
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
else
{
Connection conn =baglan();
try {
String sql = "delete from sepet where id=?;";
int secilen = jlist[0].getSelectedIndex();
if(secilen>-1)
{
int secilen_id = (int) dlm[4].get(secilen); // urunler icin id listesi;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,secilen_id);
ps.executeUpdate();
listedoldur();
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
public void combodoldur()
{
try{
Connection conn = baglan();
if(conn!=null)
{
String sql = "select id,ad,fiyat from urunler";
Statement st = conn.createStatement();
ResultSet rs =st.executeQuery(sql);
dlm[3] = new DefaultListModel(); // urunler tablosunun id bilgisini tutacak olan degisken
jcb.addItem("Seçiniz");
while(rs.next())
{
dlm[3].addElement(rs.getInt("id"));
jcb.addItem(rs.getString("ad"));
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public void listedoldur()
{
try{
Connection conn = baglan();
if(conn!=null)
{
String sql = "select u.ad,u.fiyat*s.miktar as tutar,s.id," +
"s.miktar from urunler u inner join sepet s on " +
"s.urunid=u.id";
Statement st = conn.createStatement();
ResultSet rs =st.executeQuery(sql);
dlm[0].clear();
dlm[1].clear();
dlm[2].clear();
dlm[4] = new DefaultListModel(); // sepet id bilgisi tutan degisken silme isleminde kullanilacak
int tutar=0;
while(rs.next())
{
dlm[0].addElement(rs.getString("ad"));
dlm[1].addElement(rs.getInt("miktar"));
dlm[2].addElement(rs.getInt("tutar"));
dlm[4].addElement(rs.getInt("id")); // s.id bilgisi
tutar+=(int)rs.getInt("tutar");
}
jlab.setText("Tutar: "+tutar);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public Connection baglan()
{
Connection conn=null;
try{
Class.forName("org.sqlite.JDBC");
conn=DriverManager.getConnection("jdbc:sqlite:market.db");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Bağlantı yapılamadı!","Hata",0);
}
return conn;
}
/**
* @param args
*/
public static void main(String[] args) {
new market();
}
}
Java-2 2016 Hafta-11 Uygulamaları