Nesne Tabanlı Programlama-2 (Bahar-2016) Hafta-7/2

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;


public class otomasyon extends JFrame implements ActionListener, ListSelectionListener {

	JList<String>[] jlist;
	DefaultListModel<String>[] dlm;
	JTextField[] jtf;
	
	Connection conn = null;
	
	public otomasyon()
	{
		this.setTitle("Ogrenci Otomasyonu");
		this.setSize(500,400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		
		JPanel jpn_main = new JPanel();
		jpn_main.setPreferredSize(new Dimension(490, 390));
		jpn_main.setLayout(new GridLayout(1,5));
		
		String[] labels={"İsim","Vize","Final","Ortalama","Harf"};
		String[] buttons={"Ekle","Sil","Güncel."};
		String[] buttons_action={"ekle","sil","guncelle"};
		
		jlist = new JList[5];
		dlm = new DefaultListModel[6]; // id icin 1 adet fazla yazdik
		jtf = new JTextField[3];
		
		dlm[5] = new DefaultListModel<String>(); // id icin
		
		for(int i=0;i<labels.length;i++)
		{
			JPanel jpn = new JPanel();
			jpn_main.add(jpn);
			JLabel jlab = new JLabel(labels[i]);
			jpn.add(jlab);
			
			dlm[i]= new DefaultListModel<String>();
			jlist[i] = new JList<String>(dlm[i]);
			JScrollPane jscp = new JScrollPane(jlist[i]);
			jscp.setPreferredSize(new Dimension(90, 290));
			
			jpn.add(jscp);
			
			jlist[i].addListSelectionListener(this);
			
			if(i<3)
			{
				jtf[i] = new JTextField();
				jtf[i].setPreferredSize(new Dimension(90, 30));
				
				JButton jbtn = new JButton(buttons[i]);
				jbtn.setPreferredSize(new Dimension(90, 30));
				jbtn.addActionListener(this);
				jbtn.setActionCommand(buttons_action[i]);
				jpn.add(jtf[i]);
				jpn.add(jbtn);
				
			}
			
		}
		this.add(jpn_main);
		
		baglan();
		verigetir();
		
		this.setVisible(true);
	}
	
	
	public void verigetir()
	{
		if(conn==null)
			return;
		
		try{
			
			for(int i=0;i<dlm.length;i++)
				dlm[i].clear();
			
			String sql = "select id,isim,vize,final from ogrenciler order by isim;";
			Statement st = conn.createStatement();
			ResultSet rs =st.executeQuery(sql);
			
			while(rs.next())
			{
				dlm[0].addElement(rs.getString("isim"));
				dlm[1].addElement(rs.getString("vize"));
				dlm[2].addElement(rs.getString("final"));
				dlm[5].addElement(rs.getString("id"));
				int vizenot = rs.getInt("vize");
				int finalnot = rs.getInt("final");
				int ortalama =(int) Math.round(vizenot*0.4+finalnot*0.6);

				dlm[3].addElement(ortalama+"");
				if(finalnot<30)
					dlm[4].addElement("FF");
				else
					dlm[4].addElement(harf(ortalama));
			}
				
		}catch(Exception ex){
			
		}
		
	}
	public String harf(int ortalama)
	{
		String[] harfler={"AA","BA","BB","CB","CC","DC","DD","FD"};
		int[] harf_sinir={90,80,70,60,50,40,35,30};
		
		for(int i =0;i<harfler.length;i++)
			if(ortalama>=harf_sinir[i])
				return harfler[i];
		
		return "FF";
	}

	public void baglan()
	{
		
		try{
			
			Class.forName("org.sqlite.JDBC");
			conn=DriverManager.getConnection("jdbc:sqlite:orgun2h7_2.db"); 
		}
		catch(Exception ex)
		{
			JOptionPane.showMessageDialog(this, "Driver dosyası bulunamadı!");		
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		switch (arg0.getActionCommand()) {
			case "ekle": veriekle(); break;
			case "sil": verisil(); break;
			case "guncelle": veriguncelle(); break;

		}
		
		verigetir();
	}
	
	
	public void veriguncelle() {
		if(conn==null || !kontrolet())
			return;
		
		try{
			
			int secilen = jlist[0].getSelectedIndex();
			
			if(secilen>-1)
			{
				String id = dlm[5].get(secilen);
				
				String sql ="update ogrenciler set isim=?, vize=?, final=? where id=?;";
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1, jtf[0].getText());
				ps.setString(2, jtf[1].getText());
				ps.setString(3, jtf[2].getText());
				ps.setString(4, id);
				ps.executeUpdate();
			}
			
		}
		catch(Exception ex)
		{
			
		}
		
	}

	public void verisil() {
		
		if(conn==null)
			return;
		
		try{
			
			int secilen = jlist[0].getSelectedIndex();
			
			if(secilen>-1)
			{
				String id= dlm[5].get(secilen);
				String sql = "delete from ogrenciler where id=?;";
				PreparedStatement ps = conn.prepareStatement(sql);
				ps.setString(1,id);
				ps.executeUpdate();
			}	
			
		}catch(Exception ex)
		{}
	}

	public void veriekle()
	{
		if(conn==null || !kontrolet())
			return;
		
		try{
			
			String sql = "insert into ogrenciler(isim,vize,final) values(?,?,?);";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, jtf[0].getText());
			ps.setString(2, jtf[1].getText());
			ps.setString(3, jtf[2].getText());
			ps.executeUpdate();
			
			jtf[0].setText("");
			jtf[1].setText("");
			jtf[2].setText("");
			
		}catch(Exception ex)
		{
			
		}
		
	}
	
	public boolean kontrolet()
	{
		if(!(jtf[0].getText().length()>0 && jtf[1].getText().length()>0 && jtf[2].getText().length()>0))
			return false;
		
		try {
			
			int vizenot = Integer.parseInt(jtf[1].getText());
			int finalnot = Integer.parseInt(jtf[2].getText());
			
			if(vizenot>=0 && vizenot<=100 && finalnot>=0 && finalnot<=100)
				return true;
		
		}
		catch(Exception ex)
		{}
		
		
		return false;
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		JList jl= (JList) e.getSource();
		
		int secilen = jl.getSelectedIndex();
		
		for(int i=0;i<jlist.length;i++)
			jlist[i].setSelectedIndex(secilen);
		
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new otomasyon();

	}
}

 

NTP2 (Bahar-2016) Hafta-7/2
sqlite-jdbc

Share