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 閱讀(661) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 竹北市| 哈巴河县| 简阳市| 正安县| 龙海市| 翼城县| 蚌埠市| 长岛县| 德庆县| 日照市| 揭西县| 民县| 万载县| 天津市| 宜川县| 平湖市| 灵武市| 威海市| 拜城县| 班戈县| 吕梁市| 安乡县| 革吉县| 邮箱| 舒城县| 湘阴县| 井研县| 宕昌县| 绩溪县| 湖北省| 泸州市| 富宁县| 师宗县| 罗平县| 唐河县| 怀远县| 孟村| 南丹县| 泰来县| 鄂托克前旗| 天台县|