Java-2(2015) 6. Hafta Swing Uygulamaları

java2_2015_hafta6_ornek1

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

public class ornek1 implements ActionListener
{
DefaultListModel lm;
ornek1()
{
JFrame jfrm = new JFrame("SQLite Veritabanı");
jfrm.setSize(150,290);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLayout(new FlowLayout());

lm= new DefaultListModel();
JList jls = new JList(lm);
JScrollPane jscp = new JScrollPane(jls);
jscp.setPreferredSize(new Dimension(115,190));

jfrm.add(jscp);

JButton jbtn = new JButton("Getir");
jbtn.addActionListener(this);
jfrm.add(jbtn);

jfrm.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
	Connection conn=null;	
	try{

	Class.forName("org.sqlite.JDBC"); // veritabani sinifimiz
	conn=DriverManager.getConnection("jdbc:sqlite:hafta6.db"); // veritabanimiz
	Statement st = conn.createStatement();
	st.setQueryTimeout(30);

	ResultSet rs = st.executeQuery("select adsoyad from musteri;"); // executeUpdate(); 
	
	lm.clear();
	while(rs.next())
	{
		lm.addElement(rs.getString("adsoyad"));
	}

	}
	catch(Exception ex){
		JOptionPane.showMessageDialog(null,ex,"Hata!",0);
	}
	
	try{
		conn.close();
	}
	catch(Exception ex)
	{
	
	}	
}

public static void main(String[] asdasd)
{
new ornek1();
}

}

 

 

java2_2015_hafta6_ornek2

 

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

public class ornek2 implements ActionListener
{
DefaultListModel lmadsoyad,lmtelno,lmadres;
JTextField jtfadsoyad,jtftelno,jtfadres;

ornek2()
{
JFrame jfrm = new JFrame("SQLite Veritabanı-2");
jfrm.setSize(450,325);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setLayout(new FlowLayout());

JPanel jpdis1 = new JPanel();
jpdis1.setLayout(new GridLayout(1,3)); // 1 satir 3 sutun
JPanel jpdis2 = new JPanel();

jfrm.add(jpdis1);
jfrm.add(jpdis2);

JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(125,220));
JPanel jp2 = new JPanel();
jp2.setPreferredSize(new Dimension(125,220));
JPanel jp3 = new JPanel();
jp3.setPreferredSize(new Dimension(125,220));

jpdis1.add(jp1);
jpdis1.add(jp2);
jpdis1.add(jp3);

lmadsoyad= new DefaultListModel();
lmtelno= new DefaultListModel();
lmadres= new DefaultListModel();

JList jls1 = new JList(lmadsoyad);
JScrollPane jscp1 = new JScrollPane(jls1);
jscp1.setPreferredSize(new Dimension(115,190));
jtfadsoyad= new JTextField(10);
jp1.add(jscp1);
jp1.add(jtfadsoyad);


JList jls2 = new JList(lmtelno);
JScrollPane jscp2 = new JScrollPane(jls2);
jscp2.setPreferredSize(new Dimension(115,190));
jtftelno= new JTextField(10);
jp2.add(jscp2);
jp2.add(jtftelno);

JList jls3 = new JList(lmadres);
JScrollPane jscp3 = new JScrollPane(jls3);
jscp3.setPreferredSize(new Dimension(115,190));
jtfadres= new JTextField(10);
jp3.add(jscp3);
jp3.add(jtfadres);


JButton jbtn = new JButton("Ekle");
jbtn.addActionListener(this);
jpdis2.add(jbtn);

//jfrm.pack();
verigetir();
jfrm.setVisible(true);
}

public Connection baglan()
{
	Connection conn=null;
	try{
	Class.forName("org.sqlite.JDBC"); // veritabani sinifimiz
	conn=DriverManager.getConnection("jdbc:sqlite:hafta6.db"); // veritabanimiz
	}
	catch(Exception ex)
	{
		JOptionPane.showMessageDialog(null,ex,"Hata-1!",0);
	}	
	return conn;

}

public void verigetir()
{

Connection conn=null;	
	try{
	conn=baglan();
	Statement st = conn.createStatement();
	st.setQueryTimeout(30);

	ResultSet rs = st.executeQuery("select adsoyad,telno,adres from musteri;"); 
	
	lmadsoyad.clear();
	lmtelno.clear();
	lmadres.clear();	
	while(rs.next())
	{
		lmadsoyad.addElement(rs.getString("adsoyad"));
		lmtelno.addElement(rs.getString("telno"));
		lmadres.addElement(rs.getString("adres"));
	}

	}
	catch(Exception ex){
		JOptionPane.showMessageDialog(null,ex,"Hata!",0);
	}
	
	try{
		conn.close();
	}
	catch(Exception ex)
	{
	
	}	


}

public void actionPerformed(ActionEvent ae)
{
	Connection conn=null;	
	try{
	conn=baglan();
	Statement st = conn.createStatement();
	st.setQueryTimeout(30);

	if(jtfadsoyad.getText().length()>0 && jtftelno.getText().length()>0 && jtfadres.getText().length()>0)
	{
	st.executeUpdate("insert into musteri(adsoyad,telno,adres) values('"+jtfadsoyad.getText()+"','"+jtftelno.getText()+"','"+jtfadres.getText()+"');"); 
	
	jtfadsoyad.setText("");
	jtftelno.setText("");
	jtfadres.setText("");
	
	}
	
	}
	catch(Exception ex){
		JOptionPane.showMessageDialog(null,ex,"Hata!",0);
	}
	
	try{
		conn.close();
	}
	catch(Exception ex)
	{
	
	}
	verigetir();
}

public static void main(String[] asdasd)
{
new ornek2();
}

}

Java2 2015 Hafta-6 Eklentiler

Share