程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java在數據庫插入bean

java在數據庫插入bean

編輯:關於JAVA

創建數據庫表:

create table test.Student(
    ID VARCHAR(10),
    Name VARCHAR(20),
    Bedroom VARCHAR(20),
    Zhuanye VARCHAR(40),
    Sex VARCHAR(1)
  )
/* bean 代碼 */

package student;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class stuBean extends JPanel {
  private static final long serialVersionUID = 9166303709249041951L;
  private JComboBox combBed;
  private JComboBox combDanyuan;
  private JComboBox combFool;
  private JTextField textName;
  private JTextField textID;
  private JComboBox combSex;
  private JComboBox combSpecial;
  private String bedroom;
  private String special;
  private String sex;
  private String userName="root";
  private String password="";
  private String url;
  private String strDriver;
  private Connection conn;
  private java.sql.PreparedStatement stat;
  /**
   * Create the panel
   */
  public stuBean() {
    super();
    setLayout(null);
    final JLa
bel label = new JLabel();
    label.setText("學號");
    label.setBounds(37, 29, 66, 18);
    add(label);
    textID = new JTextField();
    textID.setBounds(109, 27, 119, 22);
    add(textID);
    final JLabel label_1 = new JLabel();
    label_1.setText("姓名");
    label_1.setBounds(37, 79, 66, 18);
    add(label_1);
    textName = new JTextField();
    textName.setBounds(109, 77, 119, 22);
    add(textName);
    final JLabel label_2 = new JLabel();
    label_2.setText("宿捨");
    label_2.setBounds(37, 129, 66, 18);
    add(label_2);
    combFool = new JComboBox();
    combFool.setBounds(109, 125, 43, 27);
    combFool.addItem(new String("1"));
    combFool.addItem(new String("2"));
    combFool.addItem(new String("3"));
    combFool.addItem(new String("4"));
    combFool.addItem(new String("5"));
    combFool.addItem(new String("6"));
    combFool.addItem(new String("7"));
    combFool.addItem(new String("8"));
    combFool.addItem(new style="COLOR: #ff0000">String("9"));
    combFool.addItem(new String("10"));
    add(combFool);
    final JLabel label_3 = new JLabel();
    label_3.setText("樓");
    label_3.setBounds(158, 129, 26, 18);
    add(label_3);
    combDanyuan = new JComboBox();
    combDanyuan.setBounds(200, 125, 43, 27);
    combDanyuan.addItem(new String("1"));
    combDanyuan.addItem(new String("2"));
    combDanyuan.addItem(new String("3"));
    add(combDanyuan);
    final JLabel label_4 = new JLabel();
    label_4.setText("單元");
    label_4.setBounds(253, 129, 31, 18);
    add(label_4);
    combBed = new JComboBox();
    combBed.setBounds(290, 125, 43, 27);
    combBed.addItem(new String("1"));
    combBed.addItem(new String("2"));
    combBed.addItem(new String("3"));
    combBed.addItem(new String("4"));
    combBed.addItem(new String("5"));
    combBed.addItem(new String("6"));
    combBed.addItem(new String("7"));
    combBed.addItem(new String("8"));
    add(co
mbBed);
    final JLabel label_5 = new JLabel();
    label_5.setText("床");
    label_5.setBounds(339, 129, 53, 18);
    add(label_5);
    final JLabel label_6 = new JLabel();
    label_6.setText("專業");
    label_6.setBounds(37, 184, 66, 18);
    add(label_6);
    combSpecial = new JComboBox();
    combSpecial.setBounds(109, 180, 147, 27);
    combSpecial.addItem(new String("linux 軟件開發方向"));
    combSpecial.addItem(new String("java 軟件開發方向"));
    combSpecial.addItem(new String("嵌入軟件開發方向"));
    combSpecial.addItem(new String("對日軟件開發方向"));
    combSpecial.addItem(new String(".net 軟件開發方向"));
    add(combSpecial);
    combSex = new JComboBox();
    combSex.setBounds(109, 232, 53, 27);
    combSex.addItem(new String("男"));
    combSex.addItem(new String("女"));
    add(combSex);
    final JLabel label_7 = new JLabel();
    label_7.setText("性別");
    label_7.setBounds(37, 236, 66, 18);
    add(label_7);
    final JButton button = new JButton();
    button.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent e0000cc">) {
        bedroom = combFool.getSelectedItem().toString()
            + combDanyuan.getSelectedItem().toString()
            + combBed.getSelectedItem().toString();
        special = combSpecial.getSelectedItem().toString();
        if (combSex.getSelectedIndex() == 0) {
          sex = "m";
        } else {
          sex = "f";
        }
        dbConnection();
        dbUpdate();
        dbClose();
      }
    });
    button.setText("提交");
    button.setBounds(121, 303, 100, 28);
    add(button);
    //
  }
  public void dbConnection() {
    try {
      Class.forName(strDriver);
      conn = DriverManager.getConnection(url, userName, password);
    } catch (Exception e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "連接數據庫出錯", "error!",
          JOptionPane.ERROR_MESSAGE);
    }
  }
  public void dbUpdate() {
    try {
      stat = conn.prepareStatement("insert into test.Student(Id,Name,BedRoom,Zhuanye,Sex) values(?,?,?,?,?)");
      stat.setString(1, textID.getText());
      stat.setString(2, textName.getText()
stat.setString(3, bedroom);
      stat.setString(4, special);
      stat.setString(5, sex);
      int i = stat.executeUpdate();
      if (i == 1) {
        JOptionPane.showMessageDialog(this, "成功插入一行數據", "sucess",
            JOptionPane.PLAIN_MESSAGE);
      } else {
        JOptionPane.showMessageDialog(this, "提交出錯", "error",
            JOptionPane.ERROR_MESSAGE);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  public void dbClose() {
    try {
      stat.close();
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(null, "關閉數據庫出錯", "error!",
          JOptionPane.ERROR_MESSAGE);
    }
  }
  public String getPassword() {
    return password;
  }
  public String getStrDriver() {
    return strDriver;
  }
  public String getUrl() {
    return url;
  }
  public String getUserName() {
    return userName;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  public void setStrDriver(String strDriver) {
    this.strDriver = strDriver;
  }
  public void setUrl(String url) {
    this.url = url;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved