Java数据映射器创建MySQL用户时报‘Unknown column 'phone'’字段错误求助
Hey there! Let's break down what's going on here and fix that error quickly.
The Root Cause
Your error message says MySQL can't find a column named phone, but looking at your SQL statement:
String SQL = "INSERT INTO Users (email, password, phonenumber, post, adress, role) VALUES (?, ?, ?, ?, ?, ?)";
You're referencing a column called phonenumber instead of phone. This means your actual Users table in MySQL has a column named phone for the user's phone number, not phonenumber—the mismatch between your code and the database schema is causing the error.
Step-by-Step Fixes
Verify your table structure first
Run this MySQL command to confirm the exact column names in yourUserstable:DESCRIBE Users;This will list all columns, and you'll see that the phone number column is indeed named
phone(notphonenumber).Update your SQL statement
Changephonenumbertophonein your Java code's SQL string:String SQL = "INSERT INTO Users (email, password, phone, post, adress, role) VALUES (?, ?, ?, ?, ?, ?)";Bonus: Check for other typos
I noticed you wroteadressinstead ofaddress(missing a 'd'). This might not cause an error now if your table uses that misspelled column name, but it's worth correcting for consistency and to avoid future issues.
Quick Double-Checks
- Make sure the order of parameters in your
PreparedStatementmatches the order of columns in the SQL statement (so the phone number value is being passed to the correct column). - Confirm you're connecting to the right MySQL database instance—sometimes people accidentally connect to a test database with a different schema.
内容的提问来源于stack exchange,提问作者kristheman

