急急急!!高手们,请帮帮小妹...关于修改数据库
我用ECLIPSE和MYSQL做一个人事管理系统,功能包括有员工资本信息管理,员工考勤管理,员工工资生成。
在做员工资本信息管理时,我的界面是最上面有一个ToolBar,里面有几个ToolItem。中间有一些Lable和Text,最下面是一个TableViewer。
我建了个员工基本信息数据表名为“Employee”,表内各内容对应各个Label。
我已实现的是选中TableViewer的某一行,就能在上方的TEXT中对应显示内容,以及清除每个TEXT的内容。
我要实现的是在中间各TEXT里填写内容,按“新增式保存”后就能把填写的内容插入到数据库中,并能在最下方的TableViewer中显示出来;
选中TableViewer的某一行,按“删除”,就能把对应的这一行从数据库中删除,并在TableViewer中删除(即不显示);
选中TableViewer的某一行,上方的TEXT中对应显示内容,修改某些内容,按“修改式保存”,就能把修改后的内容在数据库中更新过来。
按“查询”后只显示员工编号和员工姓名,填写员工编号或员工姓名,对数据库进行查询,查询结果在TableViewer中显示出来。
我弄了好久都弄不出来,请问各位高手们,怎样填写语句呢?
我不想用ACTION,我只想在个对应的ToolItem里写事件语句,类似这样:
toolitem2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
}
请问如何实现新增,修改,删除与查询的功能,请帮帮我写写语句好吗??
PS:我是一个极度编程菜鸟...
问题点数:20、回复次数:9Top
1 楼queeniewen(queenie)回复于 2006-05-03 18:05:55 得分 0
代码如下(也许有点乱,请见谅):
//程序最前面先定义好几个变量
private static Connection con = null;
private static Statement sm = null;
private static ResultSet rs = null;
private Table table;
private StackLayout stackLayout = new StackLayout();
private TableViewer tv;
List list = new ArrayList();
Text noText,nameText,gradeText,sexText,birText,idText,xueliText,courseText, poText,deptText,postText,marriedText,joinText,addrText,phoneText,salaryText,memoText;
String no,name,grade,sex,bir,id,xueli,course,po,dept,post,married,join,addr,phone,salary,memo;
comp = new Composite(parent, SWT.NONE);//放ToolBar的容器
comp.setLayout(new GridLayout());
comp.setLayoutData(new GridData(GridData.FILL_BOTH));Top
2 楼queeniewen(queenie)回复于 2006-05-03 18:06:54 得分 0
final ToolBar toolBar = new ToolBar(comp, SWT.FLAT);//创建一个ToolBar
final ToolItem toolitem1 = new ToolItem(toolBar, SWT.PUSH);
toolitem1.setText("刷新数据");
toolitem1.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
tv.refresh();
}
});
final ToolItem toolitem2 = new ToolItem(toolBar, SWT.PUSH);
toolitem2.setText("清空");
toolitem2.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e){
noText.setText("");
nameText.setText("");
gradeText.setText("");
sexText.setText("");
birText.setText("");
idText.setText("");
xueliText.setText("");
courseText.setText("");
poText.setText("");
deptText.setText("");
postText.setText("");
marriedText.setText("");
joinText.setText("");
addrText.setText("");
phoneText.setText("");
salaryText.setText("");
memoText.setText("");
}
});
final ToolItem toolitem3 = new ToolItem(toolBar, SWT.PUSH);
toolitem3.setText("删除员工记录");
final ToolItem toolitem4 = new ToolItem(toolBar, SWT.PUSH);
toolitem4.setText("修改式保存");
final ToolItem toolitem5 = new ToolItem(toolBar, SWT.PUSH);
toolitem5.setText("新增式保存");
final ToolItem toolitem6 = new ToolItem(toolBar, SWT.PUSH);
toolitem6.setText("查询");Top
3 楼queeniewen(queenie)回复于 2006-05-03 18:07:29 得分 0
final Composite composite = new Composite(comp, SWT.NONE);
composite.setLayout(new GridLayout(6,true));//分为6列
final GridData gridData_1 = new GridData(GridData.FILL, GridData.FILL, false, false);
composite.setLayoutData(gridData_1);
{
new Label(composite, SWT.NONE).setText("员工编号:");
noText = new Text(composite, SWT.BORDER);
noText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("员工姓名:");
nameText = new Text(composite,SWT.BORDER);
nameText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.WRAP).setText("员工权限级别:");
gradeText = new Text(composite,SWT.BORDER);
gradeText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("性 别:");
sexText = new Text(composite,SWT.BORDER);
sexText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("出生日期:");
birText = new Text(composite,SWT.BORDER);
birText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.WRAP).setText("身份证号码:");
idText = new Text(composite,SWT.BORDER);
idText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("学 历:");
xueliText = new Text(composite,SWT.BORDER);
xueliText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("专 业:");
courseText = new Text(composite,SWT.BORDER);
courseText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("职位编号:");
poText = new Text(composite,SWT.BORDER);
poText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("所属部门:");
deptText = new Text(composite,SWT.BORDER);
deptText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("职位:");
postText = new Text(composite,SWT.BORDER);
postText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("婚姻状况:");
marriedText = new Text(composite,SWT.BORDER);
marriedText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.WRAP).setText("进入本公司时间:");
joinText = new Text(composite,SWT.BORDER);
joinText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("住 址:");
addrText = new Text(composite,SWT.BORDER);//住址拦占3个位
addrText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,3));
}
{
new Label(composite,SWT.NONE).setText("联系电话:");
phoneText = new Text(composite,SWT.BORDER);
phoneText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
}
{
new Label(composite,SWT.NONE).setText("基本工资:");
GridData gridData = new GridData();
gridData.horizontalSpan = 3;
salaryText = new Text(composite,SWT.BORDER);
salaryText.setLayoutData(gridData);
}
{
new Label(composite,SWT.NONE).setText("备 注:");
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
memoText = new Text(composite,SWT.BORDER);//备注拦占5个位
memoText.setLayoutData(createGridData(GridData.HORIZONTAL_ALIGN_FILL,5));
}
final Composite composite1 = new Composite(comp, SWT.NONE);
composite1.setLayout(new GridLayout());
composite1.setLayoutData(new GridData(GridData.FILL_BOTH));
createEmp_infoTBViewer(composite1);
tv.setContentProvider(new TableViewerContentProvider());
//设定标签器
tv.setLabelProvider(new Emp_infoTVLP());
list = DBOperate.getEmployee();
//用setInput输入数据
tv.setInput(list);
addListener();
return comp;
}Top
4 楼live41(风中涟漪)回复于 2006-05-03 18:10:35 得分 20
你留甘多边有人有心机睇啊?好心你拣D关键部分来问啦Top
5 楼wenqiuyan()回复于 2006-05-03 18:12:11 得分 0
dingTop
6 楼queeniewen(queenie)回复于 2006-05-03 18:13:37 得分 0
//创建员工信息表格
private void createEmp_infoTBViewer(Composite parent){
tv = new TableViewer(parent,SWT.MULTI|SWT.H_SCROLL|SWT.V_SCROLL|SWT.BORDER|SWT.FULL_SELECTION);
Table table = tv.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
table.setLayoutData(new GridData(GridData.FILL_BOTH));
table.setLayout(new TableLayout());
TableLayout tLayout = new TableLayout();
table.setLayout(tLayout);
//创建表格的列
tLayout.addColumnData(new ColumnWeightData(70));//设置列宽
new TableColumn(table, SWT.NONE).setText("员工编号");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("员工姓名");
tLayout.addColumnData(new ColumnWeightData(80));
new TableColumn(table, SWT.NONE).setText("员工权限级别");
tLayout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("性别");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("出生年月");
tLayout.addColumnData(new ColumnWeightData(80));
new TableColumn(table, SWT.NONE).setText("身份证号码");
tLayout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("学历");
tLayout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("专业");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("职位编号");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table,SWT.NONE).setText("所属部门");
tLayout.addColumnData(new ColumnWeightData(60));
new TableColumn(table,SWT.NONE).setText("职位");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("婚姻状况");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("进入本公司时间");
tLayout.addColumnData(new ColumnWeightData(100));
new TableColumn(table, SWT.NONE).setText("住址");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("联系电话");
tLayout.addColumnData(new ColumnWeightData(70));
new TableColumn(table, SWT.NONE).setText("基本工资");
tLayout.addColumnData(new ColumnWeightData(60));
new TableColumn(table, SWT.NONE).setText("备注");
}
private GridData createGridData(int style,int horizontalSpan){
GridData gridData = new GridData(style);
gridData.horizontalSpan=horizontalSpan;
return gridData;
}
private void addListener(){
tv.addSelectionChangedListener(new ISelectionChangedListener(){
public void selectionChanged(SelectionChangedEvent event){
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Employee emp = (Employee) selection.getFirstElement();
noText.setText(emp.getEmp_no());
nameText.setText(emp.getEmp_name());
gradeText.setText(emp.getEmp_grade().toString());
sexText.setText(emp.getEmp_sex());
birText.setText(emp.getBirthday().toString());
idText.setText(emp.getIdcard_no());
xueliText.setText(emp.getEducation());
courseText.setText(emp.getCourse());
poText.setText(emp.getPost_no());
deptText.setText(emp.getDept_name());
postText.setText(emp.getPost_name());
marriedText.setText(emp.getMarried());
joinText.setText(emp.getJoin_date().toString());
addrText.setText(emp.getAddr());
phoneText.setText(emp.getTel_no());
salaryText.setText(emp.getBasic_salary().toString());
memoText.setText(emp.getRemark());
}
});
}
static void close(ResultSet rs){
if(rs != null){
try{
rs.close();
}catch(SQLException e){
rs = null;
}
}
}
/**
* 关闭Statement对象
*/
static void close(Statement sm){
if(sm != null){
try{
sm.close();
}catch(SQLException e){
sm = null;
}
}
}
/**
* 关闭Connection对象
*/
static void close(Connection con){}
}Top
7 楼queeniewen(queenie)回复于 2006-05-03 18:15:41 得分 0
另附加ConnectManager.java
package cn.com.queenie.personnel.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author queeniewen
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class ConnectManager {
private static Connection con;
private ConnectManager(){}
public static Connection getCon() throws SQLException {
if(con !=null&&!con.isClosed())//不为空且没关闭
return con;
String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/erp_personnel?useUnicode=true&characterEncoding=gbk";
String username = "root";
String password = "123456";
//生成一个数据库连接
try{
Class.forName(className);
con = DriverManager.getConnection(url,username,password);
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
/**
* @return 返回 con。
*/
return con;
}
/**
* 提供一个真正关闭Connection的方法
*/
/**
* @throws java.sql.SQLException
*/
public static void closeConnection(){
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
con = null;
}
}
Top
8 楼queeniewen(queenie)回复于 2006-05-03 18:16:41 得分 0
附加DBOperate.java
package cn.com.queenie.personnel.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cn.com.queenie.personnel.model.Employee;
/**
* @author queeniewen
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class DBOperate {
public static List getEmployee() throws SQLException{
List list = new ArrayList();
Connection con = null;
Statement sm = null;
ResultSet rs = null;
// ResultSet rs1 = null;
//ResultSet rs2 = null;
try{
con = ConnectManager.getCon();
sm = con.createStatement();
rs = sm.executeQuery("select * from Employee");
//rs1 = sm.executeQuery("select * from Department");
//rs2 = sm.executeQuery("select * from Position");
while(rs.next()){
Employee emp = new Employee();
//Department dept = new Department();
//Position post = new Position();
emp.setEmp_no(rs.getString("emp_no"));
emp.setEmp_name(rs.getString("emp_name"));
emp.setEmp_grade(new Integer(rs.getString("emp_grade")));
emp.setEmp_sex(rs.getString("emp_sex"));
//emp.setBirthday(new Date());
emp.setBirthday(rs.getDate("birthday"));
emp.setIdcard_no(rs.getString("idcard_no"));
emp.setEducation(rs.getString("education"));
emp.setCourse(rs.getString("course"));
emp.setPost_no(rs.getString("post_no"));
emp.setDept_name(rs.getString("dept_name"));
emp.setPost_name(rs.getString("post_name"));
//dept.setDept_name(rs1.getString("dept_name"));
//post.setPost_name(rs2.getString("post_name"));
emp.setMarried(rs.getString("married"));
//emp.setJoin_date(new Date());
emp.setJoin_date(rs.getDate("join_date"));
emp.setAddr(rs.getString("addr"));
emp.setTel_no(rs.getString("tel_no"));
emp.setBasic_salary(new Float(rs.getString("basic_salary")));
emp.setRemark(rs.getString("remark"));
list.add(emp);
//list.add(dept);
//list.add(post);
}
return list;
}catch(SQLException e){
e.printStackTrace();
con.rollback();
}finally{
close(rs);
close(sm);
close(con);
}
return null;
}
static void close(ResultSet rs){
if(rs != null){
try{
rs.close();
}catch(SQLException e){
rs = null;
}
}
}Top
9 楼live41(风中涟漪)回复于 2006-05-03 19:30:46 得分 0
新开一帖,写关键部分,然后把错误的提示贴上来Top




