Rrugemalira
asked on
JDBC; mySQL; ErrorMessage "Column count doesn't match value count at row 1" ; Cannot enter data into table
Entering data into database table fails with following error message:
SQLException: General error, message from server: "Column count doesn't match value count at row 1"
SQLState: S1000
VendorError: 1136
Shouldn't address_id increment automatically? Here is the description of the table:
mysql> DESCRIBE address;
+--------------+---------- ---------- -+------+- ----+----- ----+----- ---------- -+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------- ---------- -+------+- ----+----- ----+----- ---------- -+
| address_id| bigint(20) unsigned | | PRI | NULL | auto_increment |
| country | varchar(20) | | | | |
| district | varchar(20) | YES | | NULL | |
| constituency | varchar(20) | YES | | NULL | |
| location | varchar(20) | YES | | NULL | |
| sublocation | varchar(20) | YES | | NULL | |
+--------------+---------- ---------- -+------+- ----+----- ----+----- ---------- -+
6 rows in set (0.00 sec)
And here is my DBHandler:
public class DBHandlerAddress extends HttpServlet {
public void doPost (HttpServletRequest request,
HttpServletResponse response) {
try { // OBTAIN A DATABASE CONNECTION
// Step 1: Load the JDBC driver
Class.forName("com.mysql.j dbc.Driver ");
// Step 2: Establish a connection to the database
Connection conn = DriverManager.getConnectio n(
"jdbc:mysql://localhost/rt pmKenyaDB" ,"rmwijage ", "nahumCh2");
String sql = "INSERT INTO address( country, district," +
"constituency, location, sublocation) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql) ;
// Retreave the bean properties to be inserted into table rtpm_user
AddressFormBean f = (AddressFormBean) request.getAttribute("form Handler");
String cntry = f.getCountry();
String dstrct = f.getDistrict();
String cnstncy = f.getConstituency();
String lcn = f.getLocation();
String sblcn = f.getSublocation();
// Set the parameters in the prepared statement
ps.setString(1, id);
ps.setString(2, cntry);
ps.setString(3, dstrct);
ps.setString(4, cnstncy);
ps.setString(5, lcn);
ps.setString(6, sblcn);
// Insert the row in the address table
ps.executeUpdate();
// Close the connection
ps.close();
conn.close();
}
catch (SQLException e) {
// handle any errors
System.out.println("SQLExc eption: " + e.getMessage());
System.out.println("SQLSta te: " + e.getSQLState());
System.out.println("Vendor Error: " + e.getErrorCode());
}
catch (Exception e) {
e.printStackTrace();
}
// Send the request back to success_registration.jsp
try {
getServletConfig().getServ letContext ().
getRequestDispatcher("/suc cess_addre ss.jsp").
forward(request, response);
}
catch (Exception e){}
}
}
// I do appreciate your kind help.
SQLException: General error, message from server: "Column count doesn't match value count at row 1"
SQLState: S1000
VendorError: 1136
Shouldn't address_id increment automatically? Here is the description of the table:
mysql> DESCRIBE address;
+--------------+----------
| Field | Type | Null | Key | Default | Extra |
+--------------+----------
| address_id| bigint(20) unsigned | | PRI | NULL | auto_increment |
| country | varchar(20) | | | | |
| district | varchar(20) | YES | | NULL | |
| constituency | varchar(20) | YES | | NULL | |
| location | varchar(20) | YES | | NULL | |
| sublocation | varchar(20) | YES | | NULL | |
+--------------+----------
6 rows in set (0.00 sec)
And here is my DBHandler:
public class DBHandlerAddress extends HttpServlet {
public void doPost (HttpServletRequest request,
HttpServletResponse response) {
try { // OBTAIN A DATABASE CONNECTION
// Step 1: Load the JDBC driver
Class.forName("com.mysql.j
// Step 2: Establish a connection to the database
Connection conn = DriverManager.getConnectio
"jdbc:mysql://localhost/rt
String sql = "INSERT INTO address( country, district," +
"constituency, location, sublocation) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql)
// Retreave the bean properties to be inserted into table rtpm_user
AddressFormBean f = (AddressFormBean) request.getAttribute("form
String cntry = f.getCountry();
String dstrct = f.getDistrict();
String cnstncy = f.getConstituency();
String lcn = f.getLocation();
String sblcn = f.getSublocation();
// Set the parameters in the prepared statement
ps.setString(1, id);
ps.setString(2, cntry);
ps.setString(3, dstrct);
ps.setString(4, cnstncy);
ps.setString(5, lcn);
ps.setString(6, sblcn);
// Insert the row in the address table
ps.executeUpdate();
// Close the connection
ps.close();
conn.close();
}
catch (SQLException e) {
// handle any errors
System.out.println("SQLExc
System.out.println("SQLSta
System.out.println("Vendor
}
catch (Exception e) {
e.printStackTrace();
}
// Send the request back to success_registration.jsp
try {
getServletConfig().getServ
getRequestDispatcher("/suc
forward(request, response);
}
catch (Exception e){}
}
}
// I do appreciate your kind help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.