I am able to update table while running this , but not able to save. I am getting the below error.
SQL Error: 1364, SQLState: HY000 - Field 'AccCode' doesn't have a
default value org.hibernate.HibernateException:
org.hibernate.exception.GenericJDBCException: could not insert:
[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;
}
}
Based on your error code, check out this link:
Error: SQLSTATE[HY000]: General error: 1364 Field ‘xyz’ doesn’t have a default value
Otherwise, be sure that you're not updating a column constraint from NULL to NOT NULL if you don't provide a default value for already existing values to fill in.
java.lang.ArrayIndexOutOfBoundsException
how to remove this exception
public static void main(String[] args) throws ClassNotFoundException, SQLException {
FileUpdate obj = new FileUpdate();
obj.run();
}
public void run() throws SQLException, ClassNotFoundException {
String csvFile = "/home/IMRAN/file.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date dateobj = new Date();
String dt = df.format(dateobj);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Rforms", "root", "root12");
Statement st = con.createStatement();
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] emp = line.split(cvsSplitBy);
// if (emp[0] != null && emp[1] != null ) {
// for(int x = 0; x < emp.length; x++) {
String t = (String) emp[0].trim();
String t2 = (String) emp[1].trim();
}
}
Problem is not clear... java.lang.ArrayIndexOutOfBoundsException occours when you try to access to an object (array element) that does not exist. For example, your array is 7 elements long and you try to access element[8]
Are you sure emp[0] is not null?
You should check length of the emp array before accessing its elements.
if(emp!=null && emp.length==2){
String t = (String) emp[0].trim();
String t2 = (String) emp[1].trim();
}
This code below tells that emp should always have 2 or more elements.
else you got java.lang.ArrayIndexOutOfBoundsException.
...
String t = (String) emp[0].trim();
String t2 = (String) emp[1].trim();
...
Hi Let's assume that you have place CSV file and content correctly in that case you can place conditions check per below
String[] emp = line.split(cvsSplitBy);
if (emp.length > N) {
String t = (String) emp[0] !=null ?emp[0].trim():"";
String t2 = (String)emp[1] !=null ?emp[1].trim():"";
}
Note that you are aware of number values fetch from the line .
or you can utilize List. this will completely take you out from this issue.
List lst= Arrays.asList(emp);
Refer java docs for how to utilize List and fetch list from it.
I have problem with this code..I want to extract data from flat file and store it into database. flat file format is like this:-
DT|00000001|TMDWH|UNIFI|00380520160|MAH SIEW YIN|11 |JALAN PP 2/8|TAMAN PUTRA PRIMA|PUCHONG|SELANGOR|47100|MALAYSIA|801110-14-5498||||||VOBB||A||11|JALAN PP 2/8|||TAMAN PUTRA PRIMA
DT|00000002|TMDWH|UNIFI|00322012091|JUNITA BINTI JAMAL|6 10 KONDOMINIUM FAJARIA|JALAN PANTAI BARU|KUALA LUMPUR|KUALA LUMPUR|WILAYAH PERSEKUTUAN|59200|MALAYSIA|800129-09-5078||||||VOBB||A|||JALAN PANTAI BARU|6|KONDOMINIUM FAJARIA|KUALA LUMPUR
Code:
public void massageData(String tmp) {
String RecordType = "";
String RecordNumber = "";
String sourceSystemId = "";
String targetSystemId = "";
String TelNo = "";
String Name = "";
String Addr1 = "";
String Addr2 = "";
String Addr3 = "";
String TownCity = "";
String State = "";
String PostalCd = "";
String Country = "";
String NewICNo = "";
String OldICNo = "";
String PassportNo = "";
String BRN = "";
String Latitude = "";
String Longitude = "";
String ServiceType = "";
String IndicatorType = "";
//add
String CreateDate = "";
String Filler = "";
String CRNL = "";
String HouseNo = "";
String LotNo = "";
String StreetName = "";
String AptNo = "";
String BuildingName = "";
//add
String LowID = "";
String HighID = "";
String SectionName = "";
tmp = tmp.replace("\""," "); // remove " with blank
tmp = tmp.replace("\'","\'\'");
String[] recArray = tmp.split("\\|");
RecordType = recArray[1].trim();
RecordNumber = recArray[2].trim();
sourceSystemId = recArray[3].trim();
targetSystemId = recArray[4].trim();
TelNo = recArray[5].trim();
Name = recArray[6].trim();
Addr1 = recArray[7].trim();
Addr2 = recArray[8].trim();
Addr3 = recArray[9].trim();
TownCity = recArray[10].trim();
State = recArray[11].trim();
PostalCd = recArray[12].trim();
Country = recArray[13].trim();
NewICNo = recArray[14].trim();
OldICNo = recArray[15].trim();
PassportNo = recArray[16].trim();
BRN = recArray[17].trim();
Latitude = recArray[18].trim();
Longitude = recArray[19].trim();
ServiceType = recArray[20].trim();
IndicatorType = recArray[21].trim();
//add
CreateDate = recArray[22].trim();
Filler = recArray[23].trim();
CRNL = recArray[24].trim();
//
HouseNo = recArray[25].trim();
LotNo = recArray[26].trim();
StreetName = recArray[27].trim();
AptNo = recArray[28].trim();
BuildingName = recArray[29].trim();
//add
LowID = recArray[30].trim();
HighID = recArray[31].trim();
//
SectionName = recArray[32].trim();
Connection conn = null;
ResultSet rs = null;
PreparedStatement stmt = null;
logger.info("masuk messageData");
// get actual telephone number
String actualMSISDN = parseMSISDN(TelNo);
String [] aNo = getAreaCode(actualMSISDN).split("\\|");
String iCtr = getiCtr(actualMSISDN);
iCtr = recArray[0].trim();
String stateCode = lookupStateCode(State);
String sQuery = "insert into DATA_999 (ID,RecordType,RecordNumber,SourceSystemApplicationId,TargetApplicationId,TelNo,Name,HouseNo,StreetName,AppartmentSuite,TownCity,State,PostalCode,Country,NewIC,OldIC,PassportNo,BRN,LatitudeDecimal,LongitudeDecimal,ServiceType,IndicatorType,CreateDate,Filler,Cr_Nl,HouseNo_New,LotNo_New,StreetName_New,AptNo_New,BuildingName_New,LowIDRange,HighIDRange,SectionName) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
conn = ds.getConnection();
// insert post process data to data_999 table
logger.info("start Query");
stmt = conn.prepareStatement(sQuery);
stmt.setString(0,iCtr);
stmt.setString(1,RecordType);
stmt.setString(2,RecordNumber);
stmt.setString(3,sourceSystemId);
stmt.setString(4,targetSystemId);
stmt.setString(5,TelNo);
stmt.setString(6,Name);
stmt.setString(7,Addr1);
stmt.setString(8,Addr2);
stmt.setString(9,Addr3);
stmt.setString(10,TownCity);
stmt.setString(11,State);
stmt.setString(12,PostalCd);
stmt.setString(13,Country);
stmt.setString(14,NewICNo);
stmt.setString(15,OldICNo);
stmt.setString(16,PassportNo);
stmt.setString(17,BRN);
stmt.setString(18,Latitude);
stmt.setString(19,Longitude);
stmt.setString(20,ServiceType);
stmt.setString(21,IndicatorType);
//add
stmt.setString(22,CreateDate);
stmt.setString(23,Filler);
stmt.setString(24,CRNL);
//
stmt.setString(25,HouseNo);
stmt.setString(26,LotNo);
stmt.setString(27,StreetName);
stmt.setString(28,AptNo);
stmt.setString(29,BuildingName);
//add
stmt.setString(30,LowID);
stmt.setString(31,HighID);
//
stmt.setString(32,SectionName);
//stmt = conn.prepareStatement(sQuery);
int dbStat = stmt.executeUpdate();
conn.close();
} catch (SQLException s){
logger.error(s.getMessage());
}
finally {
try {if (stmt != null) stmt.close();} catch (SQLException e) {}
try {if (conn != null) conn.close();} catch (SQLException e) {}
}
I really2 hope anyone here can help me.
Current result:
No data store into database, the code was successfully compiled!
Expected result
All the data will store into database DATA_999.
The SQL API, unlike every other java API I can think of which is zero-based, is one-based - meaning it starts counting from one. Your code is trying to set the zeroth field, which should be exploding.
As a side note, because there's hardy any special processing for each field, you could replace all that code with just a few lines by simply iterating over the fields and setting the stmt params - ie don't use variables for each field:
// fyi, the regex of this split trims automatically
String[] fields = tmp.replace("\""," ").replace("\'","\'\'").trim().split("\\s*\\|\\s*");
// Do any special field processing (most need none)
field[0] = getiCtr(parseMSISDN(field[5])); // for example - just do what you need
// Now set all the SQL params
int col = 0;
for (String field : fields) {
stmt.setString(++col, field); // Note: SQL API is 1-based (not zero-based)
}
Indexes for prepared statements are 1-based:
Change stmt.setString(0,iCtr); to stmt.setString(1,iCtr);. (And adjust the following)
And please post the exception you get. It will give us more hints what might went wrong
EDIT:
Are all fields in your table of type varchar? There are values in your lines that might be modeled as ints.
I dont know whether it is possible or not,But here is my question:
I am getting 13 input fields,based on input
Ex:String firstname=request.getParameter("firstname"); ......
I have to prepare sql where clause like if (firstname!=null){ where firstname='test' and ..}
Any advises for this kind of scenario.
Regards,
Raj
If I understand correctly, you would like to generate queries dynamically, depending on the value of input fields. There are frameworks helping to do that, like MyBatis. But you could roll your own solution with prepared statements :
String query = "select * from foo f";
List<String> clauses = new ArrayList<String>();
List<Object> parameters = new ArrayList<Object>();
if (firstName != null) {
clauses.add("f.name = ?");
parameters.add(firstName);
}
// ...
if (!clauses.isEmpty()) {
query += " where " + StringUtils.join(clauses, " and ");
}
PreparedStatement ps = connection.prepareStatement(query);
for (int i = 0; i < parameters.size(); i++) {
ps.setObject(i + 1, paremeters.get(i));
}
You could make it even better by supporting SQL types, by using the builder pattern, etc., but you should get the idea with this simple example.
I assume you are using a JDBC connection to your database. You should use prepared statements, otherwise you are wide open for SQL injection attacks.
The second question is how to prevent a WHERE clause involving a field which the user did not supply. There are many (2^13 == 8192) combinations, so it is not practical to have a different statement for each possible user input. It would be possible to build the prepared statement dynamically in your case:
String statement = "SELECT * FROM " + dbName + "." + tableName;
String condition = " WHERE";
List<String> params = new ArrayList<String>();
if ( firstname != null ){
statement += condition + " firstname = ?";
condition = " AND";
params.add(firstname);
}
if ( familyname != null ){
statement += condition + " familyname = ?";
condition = " AND";
params.add(familyname);
}
connection.prepareStatement(updateString);
Then you will need to add the contents of params when you execute the prepared statement.
You will need to dynamically build the query in Java or use a stored procedure that will not filter on a field if it is null.
I was curious about this as well so I created a new answer. This is what I came up with. It can be optimized but this does what you want using the Builder pattern. You can see from my test I pass in a null and it is omitted from the where string.
public class WhereBuilder {
private final String requestParm1;
private final String requestParm2;
private final String requestParm3;
private final String requestParm4;
private final String requestParm5;
private StringBuilder whereString = new StringBuilder();
public static class Builder {
private String requestParm1 = null;
private String requestParm2 = null;
private String requestParm3 = null;
private String requestParm4 = null;
private String requestParm5 = null;
private StringBuilder whereString = new StringBuilder("WHERE ");
public Builder() {}
public Builder requestParm1(String value) {
if (value != null) {
requestParm1 = value;
whereString.append(" requestParm1 = '" + requestParm1 + "' AND");
}
return this;
}
public Builder requestParm2(String value) {
if (value != null) {
requestParm2 = value;
whereString.append(" requestParm2 = '" + requestParm2 + "' AND");
}
return this;
}
public Builder requestParm3(String value) {
if (value != null) {
requestParm3 = value;
whereString.append(" requestParm3 = '" + requestParm3 + "' AND");
}
return this;
}
public Builder requestParm4(String value) {
if (value != null) {
requestParm4 = value;
whereString.append(" requestParm4 = '" + requestParm4 + "' AND");
}
return this;
}
public Builder requestParm5(String value) {
if (value != null) {
requestParm5 = value;
whereString.append(" requestParm5 = '" + requestParm5 + "' AND");
}
return this;
}
public WhereBuilder build() {
return new WhereBuilder(this);
}
}
private WhereBuilder(Builder builder) {
requestParm1 = builder.requestParm1;
requestParm2 = builder.requestParm2;
requestParm3 = builder.requestParm3;
requestParm4 = builder.requestParm4;
requestParm5 = builder.requestParm5;
whereString = builder.whereString;
}
public String getWhereString() {
whereString.delete(whereString.length()-3, whereString.length());
return whereString.toString();
}
public static void main(String[] args) {
WhereBuilder wb = new WhereBuilder.Builder().requestParm1("hello").requestParm2("how")
.requestParm3("are").requestParm4(null).requestParm5("you").build();
String whereString = wb.getWhereString();
System.out.println(whereString);
}
}
The output of the main method is
WHERE requestParm1 = 'hello' AND requestParm2 = 'how' AND requestParm3 = 'are' AND requestParm5 = 'you'
I have this method in my database class.and I want to get a part of data from a column which is "dateOfBirth" in MySQL table ,but I don't know why the list.size() is "0" but when i use System.out.println() in my code it will show just the first line of sql table ,although I have two rows!!!
my method:
public static int getBirthPercent(String i) throws SQLException {
Statement stmt = conn.createStatement();
List<String> list = null;
if (i.equals("O")) {
ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (rst.wasNull()) {
s1 = null;
}
String s2 = s1.substring(s1.length() - 4);
int s3 = Integer.parseInt(s2);
if (list == null && s3 < 1970) {
list = new ArrayList<String>();
list.add(s2);
} else {
list = new ArrayList<String>(0);
}
}
}
if (i.equals("N")) {
ResultSet rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (rst.wasNull()) {
s1 = null;
}
String s2 = s1.substring(s1.length() - 4);
int s3 = Integer.parseInt(s2);
if (list == null && s3 > 2000) {
list = new ArrayList<String>();
list.add(s2);
System.out.println(list);
} else {
list = new ArrayList<String>(0);
}
}
}
it will return "0" for all "if" situation but the System.out.println() ,shows [2006] which is one of my row's column's year,although I have two rows it must show [2006,2009].but it doesn't!!!
Now try this code, and let us know. Cheers.
public static int getBirthPercent(String i) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rst = null;
List<String> list = new ArrayList<String>();
if (i.equals("O")) {
rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (s1 != null && !s1.isEmpty()) {
String s2 = s1.substring(s1.length() - 4);
int n = Integer.parseInt(s2);
if (n < 1970) {
list.add(s2);
}
}
}
}
if (i.equals("N")) {
rst = stmt.executeQuery("SELECT dateOfBirth from birthtable");
while (rst.next()) {
String s1 = rst.getString(1);
if (s1 != null && !s1.isEmpty()) {
String s2 = s1.substring(s1.length() - 4);
int n = Integer.parseInt(s2);
if (n > 2000) {
list.add(s2);
}
}
}
}
System.out.println(list);
}
Enough refactoring for now. Try to do more for yourself. For example,
look into commons-lang StringUtils to replace null checking,
use Date object to store dates and use rs.getDate() instead,
you can use Calendar object to get the year out. Or even SimpleDateFormat object would work too
etc...