Le langage java et les bases de données

ofta Messages postés 1 Date d'inscription lundi 13 mars 2006 Statut Membre Dernière intervention 21 mai 2006 - 21 mai 2006 à 09:51
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 - 21 mai 2006 à 13:07
salut , je veux tracer des courbes dans des interfaces telque les données concernant cette courbe proviennent d'une base de données (c'est a dire les coordonnées de deux axes)
                           et merci

1 réponse

uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
21 mai 2006 à 13:07
/*
 * DrawFromDatabase.java
 */
package javadb;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Properties;
import javax.swing.*;
/**
 * You need derby.jar to run DrawFromDatabase. You can download derby.jar here:
 * http://apache.root.lu/db/derby/db-derby-10.1.2.1/db-derby-10.1.2.1-lib.zip
 */
public class DrawFromDatabase extends JFrame {
    public DrawFromDatabase(){
        super("DrawFromDatabase");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400,350);
        setLocationRelativeTo(null);
        panel = new DrawingPanel();
        toolbar = new JToolBar();
        btCreateDB = new JButton("Create Database");
        btDraw = new JButton("Draw from Database");
        toolbar.add(btCreateDB);
        toolbar.add(btDraw);
        add(toolbar, BorderLayout.NORTH);
        add(panel, BorderLayout.CENTER);
        setVisible(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        btCreateDB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btCreateDBActionPerformed(evt);
            }
        });
        btDraw.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btDrawActionPerformed(evt);
            }
        });
    }
    private JToolBar toolbar;
    private JButton btCreateDB, btDraw;
    private DrawingPanel panel;
    private int x1, y1, x2, y2;
    private Connection conn;
    private boolean init;
    /* the default framework is embedded*/
    private String framework = "embedded";
    private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    private String protocol = "jdbc:derby:";
    public static void main(String[] args){new DrawFromDatabase();}
    private void btDrawActionPerformed(ActionEvent evt) {
        try {
            initDB();
            Statement s = conn.createStatement();
            selectAll(s);
            /*
               We release the statement resources.
             */
            s.close();
        } catch (Throwable e) {
            System.out.println("exception thrown:\n");
            if (e instanceof SQLException) {
                printSQLError((SQLException) e);
            } else {
                e.printStackTrace();
            }
        }
    }
    private void btCreateDBActionPerformed(ActionEvent evt) {
        try {
            initDB();
            Statement s = conn.createStatement();
            /*
               We create a table, add a few rows, and update one.
             */
            try{s.execute("drop table drawingtable");}catch(Exception e){}
            s.execute("create table drawingtable(x1 int, y1 int, x2 int, y2 int)");
            s.execute("insert into drawingtable values (10, 100, 100, 130)");
            s.execute("insert into drawingtable values (200, 90, 100, 130)");
            s.execute("insert into drawingtable values (100, 130, 300, 230)");
            /*
               We release the statement resources.
             */
            s.close();
            System.out.println("Database sucessfully created");
        } catch (Throwable e) {
            System.out.println("exception thrown:\n");
            if (e instanceof SQLException) {
                printSQLError((SQLException) e);
            } else {
                e.printStackTrace();
            }
        }
    }
    private void initDB() throws ClassNotFoundException, InstantiationException,
            IllegalAccessException, SQLException{
        if (init) return;
        init = true;
        Class.forName(driver).newInstance();
        conn = null;
        Properties props = new Properties();
        props.put("user", "user1");
        props.put("password", "user1");
        conn = DriverManager.getConnection(protocol +
                "drawingtable;create=true", props);
        conn.setAutoCommit(false);
    }
    private void selectAll(Statement s) throws SQLException{
        /*
          We select the rows and save the results.
         */
        ResultSet rs = s.executeQuery(
                "SELECT * FROM drawingtable");
        while (rs.next()) {
            x1 = rs.getInt(1);
            y1 = rs.getInt(2);
            x2 = rs.getInt(3);
            y2 = rs.getInt(4);
            panel.g2d.drawLine(x1, y1, x2, y2);
        }
        panel.repaint();
        /*
           We release the ResultSet resources.
         */
        rs.close();
    }
    private void formWindowClosing(java.awt.event.WindowEvent evt) {
        /*
          We end the transaction and the connection.
         */
        try {
            conn.commit();
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        /*
         Shutdown database:
         */
        boolean gotSQLExc = false;
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch (SQLException se) {
            gotSQLExc = true;
        }
        if (!gotSQLExc) {
            System.out.println("Database did not shut down normally\n");
        } else {
            System.out.println("Database shut down normally\n");
        }
    }
    void printSQLError(SQLException e) {
        while (e != null) {
            System.out.println(e.toString());
            e = e.getNextException();
        }
    }
    class DrawingPanel extends JPanel{
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image == null) {
                image = createImage(getWidth(), getHeight());
                g2d = (Graphics2D)image.getGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.white);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.setColor(Color.black);
            }
            Rectangle r = g.getClipBounds();
            g.drawImage(image, r.x, r.y, r.width+r.x, r.height+r.y,
                    r.x, r.y, r.width+r.x, r.height+r.y, null);
        }
        private Image image;
        public Graphics2D g2d;
    }
}
0
Rejoignez-nous