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

来源:互联网 发布:淘宝流量怎么提上去 编辑:程序博客网 时间:2024/05/13 06:51
<span style="font-size:18px;color:#ff0000;"><strong>数据库脚本:</strong></span>
DROP TABLE employee ;CREATE TABLE employee(idINTAUTO_INCREMENT PRIMARY KEY ,nameVARCHAR(30)NOT NULL ,ageINTNOT NULL ,sexVARCHAR(2)DEFAULT '男' ,birthdayDATE ) ;
<span style="font-size:18px;color:#ff0000;"><strong>Java测试代码:</strong></span>
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.length;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 blocke2.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 blocke1.printStackTrace();} } }  }public static void main(String[] args) {// TODO Auto-generated method stubInsertDemo indemo=new InsertDemo();}}


0 0
原创粉丝点击