我正在尝试使用函数添加,更新和删除但未插入值。 请帮帮我。该值不会插入数据库,它会给出错误 保存记录后,不插入数据库值 我试过以下代码
package main;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author abc
*/
public class TenantOperation {
private Connection conn;
public TenantOperation()
{
conn = ConnectionDb.getConnection();
}
public void addTenant(TenantClass tenantclass) {
try {
String sql = "INSERT INTO tenantmaster(flatno,ownername,contactno,flattype,buildingname) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, tenantclass.getFlatno());
ps.setString(2, tenantclass.getOwnername());
ps.setString(3, tenantclass.getMob());
ps.setString(4, tenantclass.getFlattype());
ps.setString(5, tenantclass.getBuildingname());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void removeTenant(int tenantId) {
try {
String sql = "DELETE FROM tenantmaster WHERE tenantid=?";
PreparedStatement ps = conn
.prepareStatement(sql);
ps.setInt(1, tenantId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
Here I have accessed that addtenant(). i have connect that function in this class
package main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author abc
*/
public class TenantHandler extends HttpServlet {
private static String insert= "/tenantadd.jsp";
private static String Edit = "/tenantadd.jsp";
private static String TenantRecords = "/tenantadd.jsp";
private TenantOperation tenantOperation;
public TenantHandler()
{
super();
tenantOperation = new TenantOperation();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String redirect="";
String flatno = request.getParameter("flatno");
String action = request.getParameter("action");
if(action.equalsIgnoreCase("insert"))
{
int no = Integer.parseInt(flatno);
TenantClass tenantclass = new TenantClass();
tenantclass.setFlatno(no);
tenantclass.setOwnername(request.getParameter("ownername"));
tenantclass.setMob(request.getParameter("mob"));
tenantclass.setFlattype(request.getParameter("flattype"));
tenantclass.setBuildingname(request.getParameter("buildingname"));
tenantOperation.addTenant(tenantclass);
redirect = TenantRecords;
// request.setAttribute("tenantList", tenantOperation.getAllTenants());
//System.out.println("Record Added Successfully");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}