[java]代码库
package www.baidu.com;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class revise extends JDialog implements ActionListener {
private JTextField setName,setNumber,setSex,setAge,setPost,setWage,setJop;
private JLabel name,number,age,sex,wage,post,jop;
private JButton Addyes,Addno;
private Connection con=null;
private Statement sql=null;
private ResultSet rs=null;
private String SQLname,SQLsex,SQLpost,SQLjop;
private int SQLnumber,SQLage,SQLwage;
//chaxun xx;
revise(Frame fck,String ckm,Boolean msck,chaxun xx,int j){
super(fck, ckm,msck);
if(j==-1){
JOptionPane.showMessageDialog(null,"请选中要修改的行");
return;
}
//xx=new chaxun();
setNumber=new JTextField(5);
setNumber.setText((String)xx.getValueAt(j,0).toString());
setNumber.setEditable(false);//设置为不能修改,防止出错
setName=new JTextField(5);
setName.setText((String)xx.getValueAt(j,1));
setAge=new JTextField(5);
setAge.setText((String)xx.getValueAt(j,3).toString());
setSex=new JTextField(5);
setSex.setText((String)xx.getValueAt(j,2));
setPost=new JTextField(5);
setPost.setText((String)xx.getValueAt(j,4));
setWage=new JTextField(5);
setWage.setText((String)xx.getValueAt(j,5).toString());
setJop=new JTextField(5);
setJop.setText((String)xx.getValueAt(j,6));
name=new JLabel("姓名");
number=new JLabel("学号");
age=new JLabel("年龄");
sex=new JLabel("性别");
post=new JLabel("职位");
wage=new JLabel("工资");
jop=new JLabel("地点");
Addyes=new JButton("确定");
Addno=new JButton("取消");
this.add(number);this.add(setNumber);
this.add(name);this.add(setName);
this.add(sex);this.add(setSex);
this.add(age);this.add(setAge);
this.add(post);this.add(setPost);
this.add(wage);this.add(setWage);
this.add(jop);this.add(setJop);
this.add(Addyes); this.add(Addno);
this.Addyes.addActionListener(this);
this.Addno.addActionListener(this);
this.setSize(350,400);
this.setLocation(100, 260);
this.setLayout(new GridLayout(8,2));
this.setVisible(true);
this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==Addyes){
try{
// 获得连接
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/nature","root","root");
// 建立查询条件
String recode = " update nature set name=?,sex=?,age=?,post=?,wage=?,jop=? where number=?";
PreparedStatement input=con.prepareStatement(recode);
SQLnumber=Integer.parseInt(setNumber.getText());
SQLage= Integer.parseInt(setAge.getText());
SQLwage= Integer.parseInt(setWage.getText());
// input.setInt(1, SQLnumber);
input.setString(1, setName.getText());
input.setString(2,setSex.getText());
input.setInt(3,SQLage);
input.setString(4,setPost.getText());
input.setInt(5,SQLwage);
input.setString(6,setJop.getText());
input.setInt(7, SQLnumber); //修改的条件
input.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功");
this.dispose();
}
catch(NumberFormatException nu){
JOptionPane.showMessageDialog(null,"你还没有输入哦","错误",JOptionPane.ERROR_MESSAGE);
}
catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}
catch(SQLException sqle){
sqle.printStackTrace();
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
finally
{
try {
if(rs!=null)
{
rs.close();
}
if(sql!=null)
{
sql.close();
}
if(con!=null)
{
con.close();
}
} catch (Exception e1){}
}
}
if(e.getSource()==Addno){
this.dispose();
}
}
}
[源代码打包下载]
初级程序员
by: 云代码会员 发表于:2016-06-09 23:03:04 顶(0) | 踩(0) 回复
我要积分啊!
回复评论