<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    march alex's blog
    hello,I am march alex
    posts - 52,comments - 7,trackbacks - 0
    MyFrame類用于實現數據的增刪改查。
    代碼用JFrame進行了演示,其中的每一個按鍵都添加了事件監聽。
        這里假定mysql數據庫的端口號是3306。
        數據庫是my_database。
        表是user。
        表中包含name和score兩個元素。
    使用前注意引入JDBC的jar包作為外部jar包。

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;


    public class MyFrame extends JFrame {
        private static final int Width = 420;
        private static final int Height = 300;
        private static JFrame frame = null;
        //private static Container ctn = null;
        private static FlowLayout flowLayout = null;
        private static JLabel nameLabel = null;
        private static JLabel scoreLabel = null;
        private static JTextField nameText = null;
        private static JTextField scoreText = null;
        private static JButton addButton = null;
        private static JButton deleteButton = null;
        private static JButton modifyButton = null;
        private static JButton searchButton = null;
        private static JLabel resultLabel = null;
        private static JTextField resultText = null;
        private static JButton closeButton = null;
        
        private static String driver = "com.mysql.jdbc.Driver";
        private static String url = "jdbc:mysql://localhost:3306/my_database";
        private static String userName = "root";
        private static String password = "root";
        private static Connection conn = null;
        private static Statement stmt = null;
        
        public MyFrame() {
            frame = new JFrame("JFrame connect MySQL");
            flowLayout = new FlowLayout(FlowLayout.LEFT);
            flowLayout.setHgap(20);
            flowLayout.setVgap(30);
            frame.setLayout(flowLayout);
            nameLabel = new JLabel("name");
            scoreLabel = new JLabel("score");
            nameText = new JTextField(10);
            scoreText = new JTextField(10);
            addButton = new JButton("ADD");
            deleteButton = new JButton("DELETE");
            modifyButton = new JButton("MODIFY");
            searchButton = new JButton("SEARCH");
            resultLabel = new JLabel("result");
            resultText = new JTextField(30);
            closeButton = new JButton("CLOSE");
            
            frame.add(nameLabel);
            frame.add(nameText);
            frame.add(scoreLabel);
            frame.add(scoreText);
            frame.add(addButton);
            frame.add(deleteButton);
            frame.add(modifyButton);
            frame.add(searchButton);
            frame.add(resultLabel);
            frame.add(resultText);
            frame.add(closeButton);
            
            addButton.addActionListener(new ButtonAction());
            deleteButton.addActionListener(new ButtonAction());
            modifyButton.addActionListener(new ButtonAction());
            searchButton.addActionListener(new ButtonAction());
            closeButton.addActionListener(new ButtonAction());
            
            frame.setVisible(true);
            frame.setSize(Width, Height);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
        private class ButtonAction implements ActionListener {
            public void actionPerformed(ActionEvent evt) {
                Object s = evt.getSource();
                String name = nameText.getText();
                String score = scoreText.getText();
                if(s == addButton) {
                    if(name.length()==0 || score.length()==0) {
                        System.out.println("Please enter the name and score!");
                    }
                    else {
                        String sqlInsert = 
                            "INSERT INTO my_table (name,score) VALUES (\"" + name + "\"," + score + ");";
                        try {
                            stmt.executeUpdate(sqlInsert);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Add " + name + " Succeed!");
                    }
                } else if(s == deleteButton) {
                    if(name.length() == 0) {
                        System.out.println("You must enter the name");
                        return;
                    } else if(score.length() == 0) {
                        String sqlDelete = "DELETE FROM my_table WHERE name=\"" + name + "\";";
                        try {
                            stmt.executeUpdate(sqlDelete);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } else {
                        String sqlDelete = "DELETE FROM my_table WHERE name=\"" + name 
                                + "\" and score<=" + score + ";";
                        try {
                            stmt.executeUpdate(sqlDelete);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("Delete succeed!");
                } else if(s == modifyButton) {
                    if(name.length() == 0 || score.length() == 0) {
                        System.out.println("Please enter the name and score you want to modify!");
                    } else {
                        String sqlModify = "UPDATE my_table SET name=\"" + name 
                                + "\" WHERE score=" + score + ";";
                        try {
                            stmt.executeUpdate(sqlModify);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        System.out.println("Modify " + name + " succeed!");
                    }
                } else if(s == searchButton) {
                    String sqlName = " name=\"" + name + "\"";
                    String sqlScore = " score=" + score;
                    String sqlSelect = "SELECT * FROM my_table";
                    if(name.length() == 0 && score.length() == 0) {
                        ;
                    } else if(score.length() == 0) {
                        sqlSelect += " WHERE" + sqlName + ";";
                    } else if(name.length() == 0) {
                        sqlSelect += " WHERE" + sqlScore + ";";
                    } else {
                        sqlSelect += " WHERE" + sqlName + " and" + sqlScore + ";";  
                    }
                    //System.out.println(sqlSelect);
                    try {
                        ResultSet res = stmt.executeQuery(sqlSelect);
                        while(res.next()) {
                            String ansName = res.getString(1);
                            String ansScore = res.getString(2);
                            System.out.println(ansName + " get score: " + ansScore);
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                } else if(s == closeButton) {
                    try {
                        stmt.close();
                        if(conn != null)
                            conn.close();
                    } catch (SQLException e) {
                        System.out.println("SQLException異常"+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }
        
        public static void main(String[] args) {
            try {
                Class.forName(driver);
                System.out.println("數據庫連接成功");
            } catch (ClassNotFoundException e) {
                System.out.println("ClassNotFoundException");
            }
            try {
                conn = DriverManager.getConnection(url, userName, password);
                System.out.println("connect database successful");
                stmt = conn.createStatement();
                new MyFrame();
            } catch (SQLException e) {
                System.out.println("SQLException異常"+e.getMessage());
                e.printStackTrace();
            }
            
        }
    }

    posted on 2015-03-08 16:43 marchalex 閱讀(660) 評論(0)  編輯  收藏 所屬分類: java小程序
    主站蜘蛛池模板: 亚洲大片免费观看| 久久精品国产69国产精品亚洲| 91亚洲一区二区在线观看不卡| 成人无码a级毛片免费| 亚洲无人区午夜福利码高清完整版| 永久免费无码日韩视频| 亚洲精品线路一在线观看| 人妖系列免费网站观看| 国产成人99久久亚洲综合精品 | 亚洲中文无韩国r级电影| 无套内射无矿码免费看黄| 亚洲高清偷拍一区二区三区| 特级毛片在线大全免费播放| 亚洲中文字幕在线第六区| 无人在线观看免费高清| 亚洲无人区视频大全| 麻豆国产精品入口免费观看| 国产精品亚洲专区无码WEB| 免费一级做a爰片性色毛片| 一级毛片在线播放免费| 国产AV无码专区亚洲Av| 久久久久av无码免费网| 亚洲国产欧美国产综合一区| 免费中文字幕一级毛片| 特级做A爰片毛片免费看无码| 亚洲大片在线观看| 毛片在线看免费版| 日本免费精品一区二区三区| 亚洲精品无码国产| 美女网站免费福利视频| 亚洲AV无码国产一区二区三区| 亚洲日本中文字幕一区二区三区| 免费久久人人爽人人爽av| 亚洲乱码一二三四区国产| 免费大黄网站在线观| 日韩精品人妻系列无码专区免费| 67194在线午夜亚洲| 亚洲七七久久精品中文国产| 亚洲免费福利视频| 九九免费观看全部免费视频| 4480yy私人影院亚洲|