MySQL插入数据显示unknown column ‘x' in 'field list'

问题遇到的现象和发生背景

数据库插入中英文数据时报错Unknown column 'x' in 'field list',但是插入数字就不会这样

用代码块功能插入代码,请勿粘贴截图

public ResultSet select(String id) throws SQLException {
String sql = "select * from a where 工号 = "+id+" ";
rs = stmt.executeQuery(sql);
return rs;
}

//添加
public void insert(String id,String name,String sex,String age) throws SQLException {
    select(id);
    if(rs.next()) {
            JOptionPane.showMessageDialog(null, "已存在该工号");
    }
    else {
        String sql = "insert into a values('"+id+"' ,"+name+","+sex+","+age+")";
        int a = stmt.executeUpdate(sql);
        if(a==1){
            JOptionPane.showMessageDialog(null, "添加成功");
        }
    }
}

应该是数据库编码问题,看下你数据库的编码,调成统一的,数据库URL上指定编码utf-8

检查
是否有触发器
字段编码是否一致

"select * from a where 工号 = "+id+" ";

确定下数据库schema,“工号”是表“a”一个列名?

你表的每个字段 是个 VARCHAR 类型

SQL 拼接 要注意 单引号问题
比如 这个 查询sql

String sql = "select * from a where 工号 = "+id+" ";

img

img

如果id是非数字类型的,要改成如下,不然 id 这个值 就会被当作 一个列名,而你的表是没有这个列名的,所以报错了【就比如 你上面的 x】

String sql = "select * from a where 工号 = '"+id+"'";

img


如有帮助,欢迎采纳哈!

在这里插入图片描述

本人的开源项目,欢迎star支持下!!!