فيديو يشرح كيفية استخدام قاعدة بيانات اباتشي ديربي في برنامج الجافا الخاص بك و تم عمل البرنامج بواسطة برنامج netbeans
رابط ملف derby.jar : http://www.mediafire.com/?kl8oz6aiinse69w
كود برنامج انشاء قاعدة البيانات
كود البرنامج
رابط ملف derby.jar : http://www.mediafire.com/?kl8oz6aiinse69w
كود برنامج انشاء قاعدة البيانات
import java.io.File; import java.sql.*; /** * * @author shalkam */ public class Main { private String absPath; /** * the constructor sets the abspPath value */ public Main() { String path = new File(getClass().getResource("").getPath()).getParentFile().getParent(); absPath = path.substring(5) + System.getProperty("file.separator"); } /** * @param args the command line arguments */ public static void main(String[] args) { Main funcs = new Main(); funcs.functions(); } private void functions() { try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); } catch (ClassNotFoundException e) { System.out.println(e); } try { Statement stat; Connection m_connexion = DriverManager.getConnection("jdbc:derby:" + absPath + "data;create=true"); stat = m_connexion.createStatement(); stat.execute("CREATE TABLE APP.MYTABLE ( Age int, Name varchar(255) )"); } catch (SQLException e) { System.err.println(e); } }
كود البرنامج
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; import javax.swing.table.DefaultTableModel; /** * * @author shalkam */ public class Main { private String absPath; /** * the constructor sets the abspPath value */ public Main() { String path = new File(getClass().getResource("").getPath()).getParentFile().getParent(); absPath = path.substring(5) + System.getProperty("file.separator"); } /** * @param args the command line arguments */ public static void main(String[] args) { Main funcs = new Main(); funcs.functions(); } private void functions() { JButton button = new JButton("add row"); ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent e) { try { DatabaseMetaData metas; ResultSet tables; final Statement stat; Connection m_connexion = DriverManager.getConnection("jdbc:derby:" + absPath + "data;"); metas = m_connexion.getMetaData(); stat = m_connexion.createStatement(); stat.execute("insert into APP.MYTABLE values (25, 'SHALKAM')"); } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }; button.addActionListener(action); JButton button2 = new JButton("reset"); ActionListener action2 = new ActionListener() { public void actionPerformed(ActionEvent e) { try { DatabaseMetaData metas; ResultSet tables; final Statement stat; Connection m_connexion = DriverManager.getConnection("jdbc:derby:" + absPath + "data;"); metas = m_connexion.getMetaData(); stat = m_connexion.createStatement(); stat.execute("DROP TABLE APP.MYTABLE"); stat.execute("CREATE TABLE APP.MYTABLE ( Age int, Name varchar(255) )"); } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }; button2.addActionListener(action2); try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); } catch (ClassNotFoundException e) { System.out.println(e); } String data[][] = {}; String col[] = {"Name", "code"}; DefaultTableModel model = new DefaultTableModel(data, col); try { Statement stat; Connection m_connexion = DriverManager.getConnection("jdbc:derby:" + absPath + "data;"); stat = m_connexion.createStatement(); //stat.execute("CREATE TABLE APP.MYTABLE ( Age int, Name varchar(255) )"); ResultSet rs = stat.executeQuery("SELECT * FROM APP.MYTABLE"); JFrame frame = new JFrame("Inserting rows in the table!"); JPanel panel = new JPanel(); JTable table = new JTable(model); panel.add(table); panel.add(button); panel.add(button2); frame.add(panel); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); model.insertRow(table.getRowCount(), col); while (rs.next()) { String s = rs.getString("Name"); float n = rs.getFloat("Age"); model.insertRow(table.getRowCount(), new Object[]{s, n}); } } catch (SQLException e) { System.err.println(e); } } }
0 التعليقات:
إرسال تعليق