求帮忙找sql语句错

来源:2-5 JDBC实战---搭建模型层 Ⅲ

随便学学

2015-07-12 15:33

public void addUser(User u) throws Exception{
 Connection conn=GetConnection.getConnection();
 String sql="insert into user_table (name,sex,keshi,jituanhao,group) values (?,?,?,?,?)";  
 PreparedStatement pdst=conn.prepareStatement(sql);
 pdst.setString(1, u.getName());
 pdst.setString(2, u.getSex());
 pdst.setString(3, u.getKeshi());
 pdst.setInt(4, u.getJituanhao());
 pdst.setString(5, u.getGroup());
 pdst.execute(); 
 }
 public void delUser(Integer id) throws SQLException{
  Connection conn=GetConnection.getConnection();
  String sql="" + "delete from user_table" +
    "where id=? ";

  PreparedStatement pdst=conn.prepareStatement(sql);
  pdst.setInt(1, id);
  pdst.execute();
 }
 public void updateUser(User u) throws SQLException{
  Connection conn=GetConnection.getConnection();
  String sql="" + " update user_table"+" set name=?,sex=?,keshi=?,jituanhao=?,group=? " +
    "where id=? ";

  PreparedStatement pdst=conn.prepareStatement(sql);
  pdst.setString(1, u.getName());
  pdst.setString(2, u.getSex());
  pdst.setString(3, u.getKeshi());
  pdst.setInt(4, u.getJituanhao());
  pdst.setString(5, u.getGroup());
  pdst.setInt(6, u.getId());
  pdst.execute();
 }
 public List<User> query() throws Exception{
  Connection conn=GetConnection.getConnection();
  Statement stmt=conn.createStatement();
  ResultSet res=stmt.executeQuery("select name,group  from user_table");
  List<User> user=new ArrayList<User>();
  User u=null;
  int i=0;
  while(res.next()){
   System.out.println(++i);
   u=new User();
   u.setName(res.getString("name"));
   u.setGroup(res.getString("group"));
   user.add(u);
   
  }
  return user;
 }
 public User get(Integer id) throws SQLException{
  User u=null;
  Connection conn=GetConnection.getConnection();
  String sql="" + "select * from user_table " +
    "where id=? ";  

 pdst=conn.prepareStatement(sql);   
  pdst.setInt(1, id);
  ResultSet rs=pdst.executeQuery();
  while(rs.next()){
   u=new User();
   u.setId(rs.getInt("id"));
   u.setGroup(rs.getString("group"));
   u.setJituanhao(rs.getInt("jituanhao"));
   u.setKeshi(rs.getString("keshi"));
   u.setName(rs.getString("name"));
   u.setSex(rs.getString("sex"));
  }
   
  return u;
 }

增加人员时报错信息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group) values ('??','?','????',6922,'???')' at line 1

删除人员时报错信息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id=23' at line 1

更新人员时报错信息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group='???' where id=24' at line 1

查询多个人员时报错信息:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group from user_table' at line 1

查询单个人员时报错信息:/*You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=3' at line 1  PreparedStatement

实在找不出sql语句错在哪,在MySQL数据库直接用这些sql语句操作,提示错误信息类似。

写回答 关注

3回答

  • bupang
    2015-08-04 09:04:51

    字段中间加空格   连在一起是会报错的

  • 随便学学
    2015-07-13 16:29:25

    表是用heidiSQL建的

    字段是一一对应的

    关键是直接调用数据库,输入sql语句,也是提示有误

  • 康振宁
    2015-07-13 15:03:22

    你表是如何创建的?你看看字段什么的一一对应吗?

JDBC之 “ 对岸的女孩看过来”

一起领略JDBC的奥秘,为进一步学习集成框架打下良好的基础

99324 学习 · 856 问题

查看课程

相似问题