?java.sql.SQLException: Column count doesn't match value count at row

fuyou001 2008-04-10 04:23:22
建表的SQL:
use info;
create table employee (
id int not null primary key AUTO_INCREMENT,
name char(20) not null,
wkno char(20) not null,
department char(50),
birthday datetime ,
salary decimal(7,2)

)

actionForm
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.fuyou.struts.form;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 04-10-2008
*
* XDoclet definition:
* @struts.form name="regForm"
*/

public class RegForm extends ActionForm {
/*
* Generated Methods
*/

/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private String name;
private String wkno;
private String department;
private String birthday;
private double salary = 0.0;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getWkno() {
return wkno;
}

public void setWkno(String wkno) {
this.wkno = wkno;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public String getBirthday() {
return birthday;
}

public void setBirthday(String birthday) {
this.birthday = birthday;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

return null;
}

/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
}
:
action代码:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.fuyou.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.fuyou.beans.Employee;
import com.fuyou.dao.EmployeeDao;
import com.fuyou.struts.form.RegForm;

/**
* MyEclipse Struts
* Creation date: 04-10-2008
*
* XDoclet definition:
* @struts.action path="/reg" name="regForm" input="/reg.jsp" scope="request" validate="true"
*/
public class RegAction extends Action {
/*
* Generated Methods
*/


/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String name;
String wkno;
String department;
String date;
double salary = 0.0;
RegForm regForm = (RegForm) form;

name = regForm.getName();
wkno = regForm.getWkno();
department = regForm.getDepartment();
date = regForm.getBirthday();
salary = regForm.getSalary();
java.sql.Date birthday = java.sql.Date.valueOf(date);
Employee employee = new Employee(name,wkno,department,birthday,salary);
EmployeeDao employeeDao = new EmployeeDao();
employeeDao.save(employee);
return mapping.findForward("success");
}
}

这样的类型,它报这个错!
信息: Server startup in 6437 ms
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:843)
at com.fuyou.dao.EmployeeDao.save(EmployeeDao.java:126)
at com.fuyou.struts.action.RegAction.execute(RegAction.java:56)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.in
...全文
1944 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2010-09-28
  • 打赏
  • 举报
回复
将EmployeeDao中的
public void save(Employee employee) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "insert into employee values(?,?,?,?,?)";
conn = dbUtils.getConnection();
}
中的
String sql = "insert into employee values(?,?,?,?,?)";
改为
String sql = "insert into employee values(null,?,?,?,?,?)";
c_jinfeng 2010-04-21
  • 打赏
  • 举报
回复
发错了,很抱歉
c_jinfeng 2010-04-21
  • 打赏
  • 举报
回复
vc方法
minlucky 2008-04-10
  • 打赏
  • 举报
回复
你写的SQL语句里列的数目和后面的值的数目不一致,
SQL语句写成:insert into employee (name,wkno ,department,birthday ,salary ) values('a','b','c','d','e') 这样估计就可以了。

awusoft 2008-04-10
  • 打赏
  • 举报
回复
insert into employee values(?,?,?,?,?)";


把字段写上去
全粘架构师 2008-04-10
  • 打赏
  • 举报
回复
你要把EmployeeDao贴出来才行

看上去是越界了。数据库的列的序号是从1开始,不是0
limon758 2008-04-10
  • 打赏
  • 举报
回复
sqlBean没发啊
fuyou001 2008-04-10
  • 打赏
  • 举报
回复
我贴出来:
employee:javabean
/**
* 员工的javabean
*/
package com.fuyou.beans;

import java.sql.Date;

/**
* @author fuyubao
*/
public class Employee {

private int id ;
private String name;
private String wkno;
private String department;
private Date birthday;
private double salary = 0.0;

public Employee() {

}

public Employee( String name, String wkno, String department,
Date birthday, double salary) {


this.name = name;
this.wkno = wkno;
this.department = department;
this.birthday = birthday;
this.salary = salary;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getWkno() {
return wkno;
}

public void setWkno(String wkno) {
this.wkno = wkno;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

}

dao:
/**
*
*/
package com.fuyou.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fuyou.beans.Employee;
import com.fuyou.utis.DBUtils;

/**
* @author fuyubao 操作员工信息的DAO类
*/
public class EmployeeDao {

private List<Employee> list = null;
private DBUtils dbUtils = null;

public EmployeeDao() {
dbUtils = new DBUtils();
list = new ArrayList<Employee>();

}

/*
* @return返回所有的员工信息
*/
public List<Employee> getAll() {
Connection conn = null;
Statement stmt = null;
String sql = "select * from employee";
// System.out.println(sql);
conn = dbUtils.getConnection();
ResultSet rs = null;
Employee employee = null;

try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

while (rs != null && rs.next()) {
employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setWkno(rs.getString("wkno"));
employee.setDepartment(rs.getString("Department"));

employee.setBirthday(rs.getDate("birthday"));
employee.setSalary(rs.getDouble("salary"));
list.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closeResult(rs);
this.dbUtils.closeStatemnet(stmt);
this.dbUtils.closeConnection(conn);
}
return list;
}

/*
* @return返回与ID关联的员工信息 @param id根据ID回
*/
public Employee getOne(int id) {
Connection conn = null;
Statement stmt = null;
String sql = "select * from employee where id = '" + id+"'";
// System.out.println(sql);
conn = dbUtils.getConnection();
ResultSet rs = null;
Employee employee = null;

try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

while (rs != null && rs.next()) {

employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setWkno(rs.getString("wkno"));
employee.setDepartment(rs.getString("Department"));

employee.setBirthday(rs.getDate("birthday"));
employee.setSalary(rs.getDouble("salary"));
System.out.println("getOne");
return employee;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closeResult(rs);
this.dbUtils.closeStatemnet(stmt);
this.dbUtils.closeConnection(conn);
}
return null;
}

/*
* @param employee参数是员工信息 增加新一个名员工
*/
public void save(Employee employee) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "insert into employee values(?,?,?,?,?)";
conn = dbUtils.getConnection();

try {
preStmt = conn.prepareStatement(sql);

preStmt.setString(1, employee.getName());
preStmt.setString(2, employee.getWkno());
preStmt.setString(3, employee.getDepartment());
preStmt.setDate(4, (Date) employee.getBirthday());
preStmt.setDouble(5, employee.getSalary());

preStmt.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
}

/*
* @param id根据ID主键更新数据 ,@return 更新成功,返回true,否则返回false
*/
public boolean update(Employee employee, int id) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "update employee set name=?,wkno=?,department=?,birthday=?,salary=? where id='"
+ id + "'";
conn = dbUtils.getConnection();

try {
preStmt = conn.prepareStatement(sql);

preStmt.setString(1, employee.getName());
preStmt.setString(2, employee.getWkno());
preStmt.setString(3, employee.getDepartment());
preStmt.setDate(4, (Date) employee.getBirthday());
preStmt.setDouble(5, employee.getSalary());

preStmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
return false;
}

/*
* @param id根据ID主键删除数据, @return 删除成功,返回true,否则返回false
*/
public boolean delete(int id) {
Connection conn = null;
PreparedStatement preStmt = null;
String sql = "delete from employee where id = '" + id + " '";
conn = dbUtils.getConnection();

try {
preStmt = conn.prepareStatement(sql);
preStmt.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.dbUtils.closePreStmt(preStmt);
this.dbUtils.closeConnection(conn);
}
return false;
}
}
kokobox 2008-04-10
  • 打赏
  • 举报
回复
可怜的小宝,好多的代码
zr_dixuexiongying 2008-04-10
  • 打赏
  • 举报
回复
sql 语句插入错误
nihuajie05 2008-04-10
  • 打赏
  • 举报
回复
有时间...能力有限..只能捧个人场了

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧