操作mysql数据库源码_图形界面下操作Mysql数据库 (源代码篇)

数据库脚本:

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值