This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, i have certain data which i have to write to the database. Its an insert of records. The problem here is that i dont want to insert into all fields of the database but only those which is needed. I get the error saying "Column count doesn't match value count at row 1" This is a sample of my code.
String query = ("select Name, Leave_Entitlement from tb_employee where emp_ID = '" + userName + "'"); rst = stm.executeQuery(query);
String query1 = "SELECT (A.Leave_Entitlement - sum(B.Duration)) AS BALANCE_LEAVE FROM tb_employee A, tb_leavemc B WHERE A.emp_ID=B.emp_ID AND A.emp_ID = '" + userName + "'"; rst1 = stm1.executeQuery(query1);
Originally posted by vanan saravanan: Hi i tried this, but i get the below error.
javax.servlet.ServletException: Unknown column 'LeaveType' in 'field list' [...] String query2 = "INSERT into tb_leavemc (Leave_Type) values (LeaveType)";
Yes. (LeaveType) expresses the name of a column. If LeaveType is a java variable whose value you want to embed in the SQL, then do:
However, in the not-very-long run, you will want to use PreparedStatment, instead of Statement. Some of the many reasons: 1. Cleaner code 2. Faster on some databases (for repeated executions) 3. More secure 4. Don't have to escape embedded quote values 5. Fewer implicit data conversion problems 6. Cleaner code 7. It's what all the cool professional programmers use.
INSERT into tb_leavemc (emp_ID) values ('"+userName+"') In the above query statement you trying to insert only emp_ID. May be (Leave_Type) attibute is defined as not nullin database and there is no default value specified for that in database. so you must supply value for Leave_Type whenever you are inserting record in tb_leavemc table.
so your query should be like:
String MyQuery = "INSERT into tb_leavemc (emp_ID,Leave_Type) values ('"+userName+"','"+LeaveType+"')";