数据库脚本:
DROP TABLE employee ;
CREATE TABLE employee(
idINTAUTO_INCREMENT PRIMARY KEY ,
nameVARCHAR(30)NOT NULL ,
ageINTNOT NULL ,
sexVARCHAR(2)DEFAULT '男' ,
birthdayDATE
) ;
Java测试代码:
public class InsertDemo extends JFrame implements ActionListener {
Statement sm = null;
Connection ct = null;
ResultSet rs = null;
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER="root";
public static final String DBPASS="mysqladmin";
JLabel[] bq={new JLabel("序号"),new JLabel("姓名"),new JLabel("年龄"),
new JLabel("性别"),new JLabel("生日")};//
JTextField[] t={new JTextField(""),new JTextField(""),new JTextField(""),
new JTextField(""),new JTextField(""),new JTextField("")};//输入信息框
JLabel lab;
JPanel p1,p2;
JButton an1,an2;//确定员工信息及返回上一页按钮
public InsertDemo(){
p1=new JPanel();
p1.setLayout(null);
lab=new JLabel("欢迎使用添加员工信息界面");
lab.setFont(new Font("宋体", Font.PLAIN, 26));
lab.setBounds(100,0,350,40);
//添加标签及文本框
for(int i=0;i
bq[i].setBounds(120,80+i*70,130,28);
bq[i].setFont(new Font("宋体", Font.PLAIN, 26));
t[i].setBounds(220,80+i*70,130,26);
t[i].setFont(new Font("宋体", Font.PLAIN, 26));
p1.add(t[i]);
p1.add(bq[i]);
}
//添加确定按钮
an1=new JButton("确定");
an1.setBounds(100,420,100,30);
an1.setFont(new Font("宋体", Font.PLAIN, 26));
an1.setForeground(Color.blue);
p1.add(an1);
an1.addActionListener(this);
//添加返回按钮
an2=new JButton("返回");
an2.setBounds(260,420,100,30);
an2.setFont(new Font("宋体", Font.PLAIN, 26));
an2.setForeground(Color.blue);
p1.add(an2);
an2.addActionListener(this);
//提示信息
p1.add(lab);
this.add(p1);
this.setTitle("添加工作人员信息");
this.setBounds(100, 100, 500,600);
this.setResizable(false);
this.setResizable(false);//固定界面大小,不允许最大化
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
Class.forName( DBDRIVER );
}
catch (ClassNotFoundException e)
{
System.out.print( "尊敬的用户:\n温馨提示您:数据库信息异常,请检查!" );
}
try {
//注意连接的数据库、用户名、密码
ct=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
}
catch (SQLException e)
{
System.out.print( "尊敬的用户:\n温馨提示您:数据库连接不成功,请检查!" );
}
try {
sm=ct.createStatement();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//System.out.println(ct);//测试数据库是否连接成功
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==an1)
{
if(t[0].getText().isEmpty()||t[1].getText().isEmpty()||t[2].getText().isEmpty()||t[3].getText().isEmpty()||t[4].getText().isEmpty())
{
JOptionPane.showMessageDialog(this,"文本框不能为空!!!","消息对话框!"
,JOptionPane.INFORMATION_MESSAGE);
return;
}
else {
String u0=t[0].getText();
String u1=t[1].getText();
String u2=t[2].getText();
String u3=t[3].getText();
String u4=t[4].getText();
String sql="insert into employee(id,name,age,sex,birthday)" +
" values ('"+u0+"','"+u1+"','"+u2+"','"+u3+"','"+u4+"')";
try {
sm.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"该信息已录入系统!!!","消息对话框!"
,JOptionPane.INFORMATION_MESSAGE);
sm.close();
}
catch(SQLException ee) {System.out.println("出错,未添加成功!");}
try {
ct.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
InsertDemo indemo=new InsertDemo();
}
}