java在数据库插入bean
创建数据库表:
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 易做图;
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) {
易做图 = "m";
} else {
易做图 = "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, 易做图);
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;
}
}
www.zzzyk.com 电脑知识网 打造全国最大的电脑知识学习基地