0

我可以在运行时更新表,但无法保存。我收到以下错误。

SQL 错误:1364,SQLState:HY000 - 字段“AccCode”没有默认值 org.hibernate.HibernateException:org.hibernate.exception.GenericJDBCException:无法插入:[com.anm.more.dao.AccMast]。

public class AccmastAutoUpdate {

 /*
  * Created class for auto update of accmast table from .csv file through ftp
  */
 enter code here
 private static final Logger logger = Logger.getLogger(AccmastAutoUpdate.class);

 static MailTicket mail = new MailTicket();
 static StringBuffer storeMsg = new StringBuffer();
 static int totalcount = 0;
 static int errorRecCount = 0;
 static int successRecCount = 0;
 public static void main(String args[]) {

  MoreUtils moreUtils = new MoreUtils();
  Properties ps = moreUtils.getPropertyLoad("com/anm/more/bundle/Messages.properties");
  String folderLoc = moreUtils.getPropertyValueByLabel(ps, "task_ftp_download");
  folderLoc = folderLoc.replace("\\", "/");

  System.out.println("Entering AccmastAutoUpdate");
  File folder = new File(folderLoc);

  File[] listOfFiles = folder.listFiles();
  for (int i = 0; i < listOfFiles.length; i++) {
   String filename = listOfFiles[i].getName();

   if (filename.startsWith("Accmast") && listOfFiles[i].getName().endsWith(".csv")) {
    logger.info(filename);
    boolean flag = insertToAccmast(folderLoc + "/" + filename);
    if (flag) {
     File file = new File(folderLoc + "/" + filename);
    }
    storeMsg.append(System.getProperty("line.separator"));
    storeMsg.append("Total Records : " + totalcount + " Success Records : " + successRecCount + " Failed Records : " + errorRecCount);
   }
  }
 }

 private static boolean insertToAccmast(String fileloc) {
  // TODO Auto-generated method stub

  boolean flag = false;

  String AccCode = null;
  String Account = null;
  String Site = null;
  String Phone = null;
  // String Disp_Deno = null;
  String Address = null;
  String AccType = null;
  // String Shortage_Deno = null;
  String Fax = null;
  String AccTeam = null;
  String Status = null;
  String City = null;
  String State = null;
  String Territory = null;
  String URL = null;
  String Pincode = null; // int
  String Country = null;
  String Industry = null;
  String conttype = null;
  String SiteCode = null;
  String acccode1 = null;
  String SiteID = null;
  String ContactName = null;
  String ServiceArea = null;
  String BillId = null;
  String ResponseHours = null;
  String ResolutionHours = null;
  String comments = null;
  String AccBranchCode = null;
  String AccBranchName = null;
  String ZonalCode = null;
  String acc_createdby = null;
  String NSBID = null;
  String Bank = null;
  String AccessibilityType; // double
  String AccessibilityTimeFrom; // double
  String AccessibilityTimeTo = null;
  String HubLocation = null;
  String SubLocation = null;
  String GLNumber = null;
  String NumberOfATM = null; // double
  String acc_createddate = null; // Date
  DateFormat getDate = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");

  try {
   CsvReader csvReader = new CsvReader(fileloc, ',');
   boolean firstline = true;
   while (csvReader.readRecord()) {
    if (firstline) {
     firstline = false;
     continue;
    }
    try {
     totalcount++;

     csvReader.get(0);

     AccCode = csvReader.get(0);
     Account = csvReader.get(1);
     Site = csvReader.get(2);
     Phone = csvReader.get(4);
     Address = csvReader.get(5);
     AccType = csvReader.get(6);
     Fax = csvReader.get(7);
     AccTeam = csvReader.get(8);
     Status = csvReader.get(9);
     City = csvReader.get(10);
     State = csvReader.get(11);
     Territory = csvReader.get(12);
     URL = csvReader.get(13);
     Pincode = csvReader.get(14);
     Country = csvReader.get(15);
     Industry = csvReader.get(16);
     conttype = csvReader.get(17);
     SiteCode = csvReader.get(18);
     acccode1 = csvReader.get(19);
     SiteID = csvReader.get(20);
     ContactName = csvReader.get(21);
     ServiceArea = csvReader.get(22);
     BillId = csvReader.get(23);
     ResponseHours = csvReader.get(24);
     ResolutionHours = csvReader.get(25);
     comments = csvReader.get(26);
     AccBranchCode = csvReader.get(27);
     AccBranchName = csvReader.get(28);
     ZonalCode = csvReader.get(29);
     acc_createdby = csvReader.get(30);
     NSBID = csvReader.get(32);
     Bank = csvReader.get(33);
     AccessibilityType = csvReader.get(34);
     AccessibilityTimeFrom = csvReader.get(35);
     AccessibilityTimeTo = csvReader.get(36);
     HubLocation = csvReader.get(37);
     SubLocation = csvReader.get(38);
     GLNumber = csvReader.get(39);
     NumberOfATM = csvReader.get(40);
     acc_createddate = csvReader.get(31);

     AccMast accmast = new AccMast();

     if (Pincode != null && Pincode != "" && Pincode.trim().length() <= 0) {
      int PincodeNo = Integer.parseInt(Pincode);
      accmast.setPincode(PincodeNo);
     } else {
      accmast.setPincode(123456);
     }

     if (NumberOfATM != null && NumberOfATM != "" && NumberOfATM.trim().length() <= 0) {
      long NumberOfATMNo = Long.parseLong(NumberOfATM);
      accmast.setNumberOfAtm(NumberOfATMNo);
     } else {
      accmast.setNumberOfAtm(0 L);
     }

     if (AccessibilityTimeFrom != null && AccessibilityTimeFrom != "" && AccessibilityTimeFrom.trim().length() <= 0) {
      long AccessibilityTimeFromNo = Long.parseLong(AccessibilityTimeFrom);
      accmast.setAccessibilityTimeFrom(AccessibilityTimeFromNo);
     } else {
      accmast.setAccessibilityTimeFrom(0 L);
     }

     if (AccessibilityTimeTo != null && AccessibilityTimeTo != "" && AccessibilityTimeTo.trim().length() <= 0) {
      long AccessibilityTimeToNo = Long.parseLong(AccessibilityTimeTo);
      accmast.setAccessibilityTimeTo(AccessibilityTimeToNo);
     } else {
      accmast.setAccessibilityTimeTo(0 L);
     }

     accmast.setAccCode(AccCode);
     accmast.setAccount(Account);
     accmast.setSite(Site);
     accmast.setPhone(Phone);
     accmast.setAddress(Address);
     accmast.setAccType(AccType);
     accmast.setFax(Fax);
     accmast.setAccTeam(AccTeam);
     accmast.setStatus(Status);
     accmast.setCity(City);
     accmast.setState(State);
     accmast.setTerritory(Territory);
     accmast.setUrl(URL);
     accmast.setCountry(Country);
     accmast.setIndustry(Industry);
     accmast.setConttype(conttype);
     accmast.setSiteCode(SiteCode);
     accmast.setAcccode1(acccode1);
     accmast.setSiteId(SiteID);
     accmast.setContactName(ContactName);
     accmast.setServiceArea(ServiceArea);
     accmast.setBillId(BillId);
     accmast.setResponseHours(ResponseHours);
     accmast.setResolutionHours(ResolutionHours);
     accmast.setComments(comments);
     accmast.setAccBranchCode(AccBranchCode);
     accmast.setAccBranchName(AccBranchName);
     accmast.setZonalCode(ZonalCode);
     accmast.setAccCreatedby(acc_createdby);
     accmast.setNsbid(NSBID);
     accmast.setBank(Bank);
     //value has been sets                      accmast.setAccessibilityType(AccessibilityType);            
     accmast.setHubLocation(HubLocation);
     accmast.setSubLocation(SubLocation);
     accmast.setGlnumber(GLNumber);
     //Date has been set                        accmast.setAccCreateddate(getDate.parse(acc_createddate));
     AccMastDAO amd = new AccMastDAO();

     //obj accmast able to view the content while debuggin.
     //primary key is varchar in database.

     boolean flag1 = amd.load(AccMast.class, AccCode);
     if (flag1) {
      amd.update(accmast);
     } else {
      amd.save(accmast);
     }

     successRecCount++;
     System.out.println("Properties saved in accmast table object");

    } catch (Exception ex) {
     errorRecCount++;
     storeMsg.append("AccMast : " + AccCode + "& Account : " + Account + " Failed");
     storeMsg.append(System.getProperty("line.separator"));
     ex.printStackTrace();
    }
   }

  } catch (FileNotFoundException e) {
   storeMsg.append(e.getMessage());
   return false;
  } catch (Exception e) {
   errorRecCount++;
   storeMsg.append(e.getMessage());
   logger.info(e.getMessage());
   return false;
  }
  return true;
 }
}

//boolean method is working fine.

public boolean load(Class < ? > classType, Object id) {
 Session session = super.getSession();
 Object ob1 = (Object) session.get(classType, (Serializable) id);
 if (ob1 != null) {
  return true;
 } else {
  return false;
 }
}    
4

1 回答 1

0

根据您的错误代码,请查看此链接:

错误:SQLSTATE[HY000]:一般错误:1364 字段“xyz”没有默认值

否则,如果您没有为已经存在的值提供默认值来填充,请确保您没有将列约束从NULLto更新。NOT NULL

于 2016-07-20T13:16:41.927 回答