Java Derby Veritabanı Form Uygulaması

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;

public class GUI extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
private JTextField txtAdi;
private JTextField txtSoyadi;
private JLabel lblAd;
private JLabel lblSoyad;
private JButton btnGonder;
Database d=new Database();

public GUI() {
txtAdi=new JTextField();
txtSoyadi=new JTextField();
lblAd=new JLabel("Adı");
Font font = new Font("Serif", Font.BOLD, 15);
lblAd.setForeground(Color.WHITE);
lblAd.setFont(font);
lblSoyad=new JLabel("Soyadı");
lblSoyad.setFont(font);
lblSoyad.setForeground(Color.WHITE);
btnGonder=new JButton("Kaydet");
pencereGoster();
this.setBackground(Color.BLACK);
}
private void pencereGoster() {
setSize(500, 500);
setLocation(100, 100);
Container con=this.getContentPane();
setTitle("Emrah KAHRAMAN E-Hero");
con.setLayout(null);
lblAd.setBounds(120, 70, 50, 25);
lblSoyad.setBounds(120, 110, 50, 25);
txtAdi.setBounds(170, 70, 120, 25);
txtSoyadi.setBounds(170, 110, 120, 25);
btnGonder.setBounds(170, 150, 100, 30);
con.add(txtAdi);
con.add(txtSoyadi);
con.add(lblAd);
con.add(lblSoyad);
con.add(btnGonder);
btnGonder.addActionListener(this);
d.Baglan();
}
@Override
public void actionPerformed(ActionEvent e) {
d.ekleme(txtAdi.getText(), txtSoyadi.getText());
try {
d.sonuclar();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
GUI g=new GUI();
g.setVisible(true);
}
}

import java.sql.*;
import javax.swing.JOptionPane;

public class Database {

public Connection Baglan(){
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn=DriverManager.getConnection("jdbc:derby:myDatabase");

return conn;}
catch(Exception e4)
{ e4.getMessage();
}
return null;
}

public void Olustur(){
Statement stmt=Baglan().createStatement();
try {
stmt.executeUpdate("CREATE TABLE Uye (Ad VARCHAR(32) NOT NULL, Soyad VARCHAR(50) NOT NULL)");
} catch (SQLException e) {
e.printStackTrace();
}

}

public void sonuclar() throws SQLException{
Statement stmt=Baglan().createStatement();
ResultSet rs;

rs = stmt.executeQuery("select * from Uye");
int num=0;
while(rs.next()){
JOptionPane.showMessageDialog(null, ++num+" – Ad:"+ rs.getString(1)+" Soyadı:" + rs.getString(2));
}
rs.close();
}

public void ekleme(String ad,String soyad){
try {

PreparedStatement psInsert=Baglan().prepareStatement("insert into Uye values (?,?)");
psInsert.setString(1,ad);
psInsert.setString(2, soyad);
psInsert.executeUpdate();
}
catch (Exception e) {

e.printStackTrace();
}
}
}
Derby Jdbc Driver

« »