Iterating List using DAO Java - java

In this code I declared a variable that contains the values of the List which is the acode. I can see the values in this List, but I don't know how to iterate over the values using the adao.adao.findAllacctDesc(**acode**). How can i iterate over this list, so that the options show the values?
Here is the code:
<%
TblTaxTypeDAO tdao = DAOFactory.getDaoManager(TblTaxType.class);
TblAccountCodesDAO adao = DAOFactory.getDaoManager(TblAccountCodes.class);
List<TblTaxType> acode = null;
String tcode = request.getParameter("taxt");
String bcode = request.getParameter("bfns");
acode = tdao.findAllAcctCode(bcode, tcode);
Debugger.print(acode);
List<TblAccountCodes> acctdesclist = null;
acctdesclist = adao.findAllacctDesc(acode); <= Having error in this line because acode is a list not a string.
String acctdescoptions = "";
if( acctdesclist!=null) {
if( acctdesclist.size()>0 ) {
for(int i=0; i<acctdesclist.size();i++) {
TblAccountCodes acctcode = (TblAccountCodes) acctdesclist.get(i);
acctdescoptions += "<option value='"+acctcode.getAcctCode()+"'>"+acctcode.getAcctDesc()+"</option>";
acctcode = null;
}
}
}
adao = null;
acctdesclist = null;
%>
<%=acctdescoptions%>

change
List<TblAccountCodes> acctdesclist = null;
acctdesclist = adao.findAllacctDesc(acode); <= Having error in this line because acode is a list not a string.
by
List<TblAccountCodes> acctdesclist = null;
for(TblTaxType T:acode){
acctdesclist.add(adao.findAllacctDesc(T.getString))
}
Is this what you want?
where getString is the function in TblTaxType where you return the string you need.

Related

How getSet of Text from row in cassandra

I have a table with < frozen < set < text > > > , column. I can't understand how I may read a column value from a row.Trying below but not able to get pass through.
else if (key.getType() == DataType.frozenSet(text))
{
str = row.getSet( , )
}
I tried the below , but i see nothing is printing out from that frozen set column.
public static void main(String[] args) throws FileNotFoundException {
long startTime = System.currentTimeMillis();
PrintWriter pw = new PrintWriter(new File("test.csv"));
String keyspace = "xxxxx";
String table = "xxxxxx";
String username = "xxxx";
String password = "xxxx";
String host = "xxxxxxx";
double count = 0;
Cluster.Builder clusterBuilder = Cluster.builder()
.addContactPoints(host)
.withCredentials(username, password);
Cluster cluster = clusterBuilder.build();
Session session = cluster.connect(keyspace);
Statement stmt = new SimpleStatement("SELECT * FROM " + table);
stmt.setFetchSize(2000);
ResultSet rs = session.execute(stmt);
Iterator<Row> iter = rs.iterator();
while ( !rs.isFullyFetched()) {
if (rs.getAvailableWithoutFetching() == 120 )
rs.fetchMoreResults();
Row row = iter.next();
if ( rs != null )
{
StringBuilder line = new StringBuilder();
for (Definition key : row.getColumnDefinitions().asList())
{
String val = myGetValue(key, row);
line.append("\"");
line.append(val);
line.append("\"");
line.append(',');
}
line.deleteCharAt(line.length()-1);
line.append('\n');
pw.write(line.toString());
System.out.println(line.toString());
++count;
}
}
pw.close();
session.close();
cluster.close();
System.out.println(count + "\t rows copied into csv");
long endTime = System.currentTimeMillis();
System.out.println("Took "+(endTime - startTime) + " ms");
}
public static String myGetValue(Definition key, Row row)
{
String str = "";
if (key != null)
{
String col = key.getName();
try
{
if (key.getType() == DataType.cdouble())
{
str = new Double(row.getDouble(col)).toString();
}
else if (key.getType() == DataType.cint())
{
str = new Integer(row.getInt(col)).toString();
}
else if (key.getType() == DataType.uuid())
{
str = row.getUUID(col).toString();
}
else if (key.getType() == DataType.cfloat())
{
str = new Float(row.getFloat(col)).toString();
}
else if (key.getType() == DataType.timestamp())
{
str = row.getDate(col).toString();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
str = fmt.format(row.getDate(col));
}
else if (key.getType().equals(DataType.set(DataType.text()) ))
{
Set<String> st1 = row.getSet(i, String.class);
str = st1.toString();
++i;
}
else
{
str = row.getString(col);
}
} catch (Exception e)
{
str = "";
}
}
return str;
}
}
schema:
create table view_page_details(
clickid uuid,
view text,
clickin int,
names frozen <set<text>>,
msg_view text,
number_of_times int,
primary key ((clickid, view, clickin), names);
cqlsh output for that specific 'names' column in that table:
| {').'{'2', 'STOP', 'williams', 'The', 'appts:', 'at', 'family''s', 'first', 'of', 'one', 'reminder', 'starts', 'to', 'your'}
| {'2'{'2', 'STOP', 'shane', 'The', 'appts:', 'at', 'family''s', 'first', 'of'}
| {'1', '2/1/15.'{'2', 'STOP', 'brete', 'The', 'appts:', 'at', 'family''s', 'first', 'of', 'one', 'reminder', 'starts', 'to', 'your'}
My program output for that specific 'names'column in that table:
,,
,,
,,
,,
From what i observed by debugging line by line , String val = myGetValue(key, row); gets the entire row value correctly from cluster, when returns value from that function its not giving out the specified column value. Not sure if something wrong inside the function logic for that specific column.
The first argument is index of the column in the ResultSet.
By accessing this way you need to get get columns by their index not by name or key.
Set<String> mySet = row.getSet(index, String.class);
If you don't have the index, then you will need keep a counter as you iterate the keys.
You can find out more from the javadoc
https://docs.datastax.com/en/latest-java-driver-api/com/datastax/driver/core/GettableByIndexData.html#getSet-int-java.lang.Class-
longer answer:
I'm really guessing what most of your code does, but with the given schema I can output the set using this code:
Row row = rs.one();
String str = "";
int i = 0;
Iterator<ColumnDefinitions.Definition> it = rs.getColumnDefinitions().iterator();
while (it.hasNext()) {
ColumnDefinitions.Definition key = it.next();
if (key.getType().equals(DataType.set(DataType.varchar()))) {
Set<String> st1 = row.getSet(i, String.class);
str = st1.toString();
System.out.println(str);
}
i++;
}

java:get different parts of two similar strings

Get different parts of two similar strings
example:
1.String1="bjsqzctjjzxyxgs" String2="bjqzctjjxxzxyxgs" result:String[] s3 = {"s","xx"}
2.String1="bjssdwxxjsyxgs" String2="bjsdwxxjsyxgs" result:String[] s3 = {"s"}
3.String1="bjydcrwljskjyxgs" String2="bjydcrjswlkjyxgs" result:String[] s3 = {"wljs","jswl"}
for the example1,i make the string to the char array ,then i can get the String "sqzctjj" and "qzctjjxx",but i cant get the result like the example result {"s","xx"}.
I hope some friends can guide me out.
thanks
public static Map<String,String> getPreviousStrAndLastStr(String shortCompanyName, String shortSelectCompanyName){
char[] shortCompanyNameCharArray = (shortCompanyName).toCharArray();
char[] shortSelectCompanyNameCharArray = shortSelectCompanyName.toCharArray();
Map<String,String> map = new HashMap<String,String>();
int cirNum = 0;
int previousDifIndex = 0;
int lastDifIndex = 0;
String longSplitStr = "";
String shortSplitStr = "";
if(shortCompanyNameCharArray.length>shortSelectCompanyNameCharArray.length){
cirNum = shortSelectCompanyNameCharArray.length;
longSplitStr = shortCompanyName;
shortSplitStr = shortSelectCompanyName;
}else{
cirNum = shortCompanyNameCharArray.length;
longSplitStr = shortSelectCompanyName;
shortSplitStr = shortCompanyName;
}
for(int i=0;i<cirNum;i++){
if (shortCompanyNameCharArray[i]!=shortSelectCompanyNameCharArray[i]){
System.out.println(shortCompanyNameCharArray[i]+"--------"+shortSelectCompanyNameCharArray[i]);
previousDifIndex = i;
break;
}
}
if(previousDifIndex != 0){
for(int i=0;i<cirNum;i++){
if (shortCompanyNameCharArray[shortCompanyNameCharArray.length-i-1]!=shortSelectCompanyNameCharArray[shortSelectCompanyNameCharArray.length-i-1]){
System.out.println(shortCompanyNameCharArray[shortCompanyNameCharArray.length-i-1]+"--------"+shortSelectCompanyNameCharArray[shortSelectCompanyNameCharArray.length-i-1]);
lastDifIndex =shortSplitStr.length() - i;
break;
}
if(previousDifIndex==(cirNum-i)){
lastDifIndex = shortSplitStr.length() - i;;
break;
}
}
}
String previousStr = shortSplitStr.substring(0,previousDifIndex);
String lastStr = shortSplitStr.substring(lastDifIndex,shortSplitStr.length());
String diffStr1 = longSplitStr.replace(previousStr,"").replace(lastStr,"");
String diffStr2 = shortSplitStr.replace(previousStr,"").replace(lastStr,"");
map.put("previousStr",previousStr);
map.put("lastStr",lastStr);
map.put("diffStr1",diffStr1);
map.put("diffStr2",diffStr2);
map.put("previousDifIndex",previousDifIndex+"");
map.put("lastDifIndex",lastDifIndex+"");
return map;
}
input "bjydcrwljskjyxgs" and "bjqzctjjxxzxyxgs"
output {diffStr2=sqzctjj, previousStr=bj, diffStr1=qzctjjxx, lastDifIndex=9, lastStr=zxyxgs, previousDifIndex=2}
so i want to make the diffStr1="qzctjjxx"and the diffStr2="sqzctjj" how to be
the String[] s3 = {"s","xx"}

Need help in creating a regular expression in Java

I am trying to apply regex on a string to obtain the output in the below format.
Input string:
params=Param1{index}~[Value1|Value2|....]^Param2~[Value1|Value2|....]
Output should be in the form of Map<String,List<String>>
Eg.
Key1: Param1
Value : Value1,Value2 upto ValueN
Key2 :Param2
Value: Value1,Value2 uptoValueN
Code:
Her is the code that I am trying to do.
String textPatt = "params";
String key = textPatt.replace("/[\\[]/", "\\[").replace("/[\\]]/", "\\]");
String pattern = new String("[\\?&]" + key + "=([^&#]*)");
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(masterUrl);
List<String> paramList = new ArrayList<String>();
if (m.find()) {
String param = m.group();
if(param != null){
String[] multiParamArr = null;
if(param.indexOf("^") != -1){
multiParamArr = param.split("\\^");
}else{
multiParamArr = new String[1];
multiParamArr[0] = param;
}
if(multiParamArr != null && multiParamArr .length > 0){
Pattern refinePattern = null;
Matcher refineMatcher = null;
String paramKeyy = null;
String paramVal = null;
HashMap<String,List<String>> ParamMap = new HashMap<String,List<String>>();
List<String list = new ArrayList<String>();
for(int i=0;i<multiParamArr.length;i++){
String[] multiParam = null;
String patternKey = "^[^\\~]*";
refinePattern = Pattern.compile(patternKey);
refineMatcher = refinePattern.matcher(multiRefineArr[i]);
if(refineMatcher.find()){
paramKey = refineMatcher.group();
if(paramKey.indexOf("=") !=-1){
patternKey = patternKey.substring(patternKey.indexOf("=")+1);
}
if(paramKey.indexOf("{") !=-1){
patternKey = patternKey.substring(patternKey.indexOf("=")+1,patternKey.indexOf("{"));
}
}
if(multiParamArr[i].indexOf("|") != -1){
multiParam = multiParamArr[i].split("\\|");
int startIndex;
int endIndex;
for(int j=0;j<multiParam.length;j++){
startIndex = 0;
endIndex = 0;
ifmultiParamArr[j].indexOf("[") > -1){
startIndex = multiParamArr[j].indexOf("[")+1;
}
ifmultiParamArr[j].indexOf("]") > -1){
endIndex = multiParamArr[j].indexOf("]");
}else{
endIndex = multiParamArr[j].length();
}
paramVal = multiParamArr[j].substring(startIndex,endIndex);
}
}else{
paramVal = multiParamArr[i].substring(multiParamArr[i].indexOfmultiParamArr[i].indexOf("]"));
}
list.add(paramVal);
}
paramMap.put((paramKey,list);
}
}
}
how to apply a single regex and achieve the output
Instead of trying to create a complex regex and maintain it, you can do it simpler and in a more readable way, in a few steps:
Split on ^
Now you have an array of Strings, each of which contains one of the parameters with its values
Split each string on ~ - you'll get an array of two strings, extract from the first the Param1 (Param2 etc)
From the second you can extract the list of values by splitting on
| (pipe).

Process Large Files in Java

I have a requirement like, need to process records from a text file and insert/update in to a table. Following is the code that I have written. But when the records in the file are 50,000 its taking more than 30 minutes to process the records, and if the records are close to 80k, out of memory error is thrown. Can anyone please suggest a way to optimize the code that I have written to improve performance ?
public static String insertIntoCHG_PNT_Table(String FILE_NAME) throws NumberFormatException, IOException
{
Date DATE_INSERTED = new Date();
String strLine = "";
FileReader fr = new FileReader(FILE_NAME);
BufferedReader br = new BufferedReader(fr);
long SEQ = 0;
double consumption = 1;
String returnString = "";
CHG_PNT insertObj = null;
long KY_PREM_NO = 0;
long KY_SPT = 0;
String COD_COT_TYP = "";
String DT_EFF = "";
String TS_KY_TOT = "";
String COD_COT = "";
String ACL_VLE = "";
String ACL_QTY = "";
String WTR_VLE = "";
String WTR_QTY = "";
String SWG_VLE = "";
String SWG_QTY = "";
String CD_TYPE_ACT = "";
String DT_TERM = "";
String CD_STAT = "";
String DT_STAT = "";
String VLN_PPE_SIZ_COD = "";
String WTR_PPE_SIZ_MTD = "";
String SWG_PPE_SIZ_MTD = "";
while( (strLine = br.readLine()) != null){
/*
* Meter Serial No, Property No, Current Meter Index, Previous meter index, Consumption needs to be added
*
*
*/
String[] split = strLine.split("\\;");
KY_PREM_NO = Long.parseLong(split[0].trim());
KY_SPT = Long.parseLong(split[1].trim());
COD_COT_TYP = split[2].trim();
DT_EFF = split[3].trim();
TS_KY_TOT = split[4].trim();
COD_COT = split[5].trim();
ACL_VLE = split[6].trim();
ACL_QTY = split[7].trim();
WTR_VLE = split[8].trim();
WTR_QTY = split[9].trim();
SWG_VLE = split[10].trim();
SWG_QTY = split[11].trim();
CD_TYPE_ACT = split[12].trim();
DT_TERM = split[13].trim();
CD_STAT = split[14].trim();
DT_STAT = split[15].trim();
VLN_PPE_SIZ_COD = split[16].trim();
WTR_PPE_SIZ_MTD = split[17].trim();
SWG_PPE_SIZ_MTD = split[18].trim();
long counter = 0;
long newCounter = 0;
CHG_PNT checkRecordCount = null;
checkRecordCount = checkAndUpdateRecord(KY_PREM_NO,KY_SPT,COD_COT_TYP,TS_KY_TOT);
try {
if(checkRecordCount == null)
insertObj = new CHG_PNT();
else
insertObj = checkRecordCount;
insertObj.setKY_PREM_NO(KY_PREM_NO);
//insertObj.setSEQ_NO(SEQ);
insertObj.setKY_SPT(KY_SPT);
insertObj.setCOD_COT_TYP(COD_COT_TYP);
insertObj.setDT_EFF(DT_EFF);
insertObj.setTS_KY_TOT(TS_KY_TOT);
insertObj.setCOD_COT(COD_COT);
insertObj.setACL_VLE(Double.parseDouble(ACL_VLE));
insertObj.setACL_QTY(Double.parseDouble(ACL_QTY));
insertObj.setWTR_VLE(Double.parseDouble(WTR_VLE));
insertObj.setWTR_QTY(Double.parseDouble(WTR_QTY));
insertObj.setSWG_VLE(Double.parseDouble(SWG_VLE));
insertObj.setSWG_QTY(Double.parseDouble(SWG_QTY));
insertObj.setCD_TYPE_ACT(CD_TYPE_ACT);
insertObj.setDT_TERM(DT_TERM);
insertObj.setCD_STAT(Double.parseDouble(CD_STAT));
insertObj.setDT_STAT(DT_STAT);
insertObj.setVLN_PPE_SIZ_COD(VLN_PPE_SIZ_COD);
insertObj.setWTR_PPE_SIZ_MTD(WTR_PPE_SIZ_MTD);
insertObj.setSWG_PPE_SIZ_MTD(SWG_PPE_SIZ_MTD);
insertObj.setDATE_INSERTED(DATE_INSERTED);
if(checkRecordCount == null)
{
insertObj.setDATE_INSERTED(DATE_INSERTED);
insertObj.insert();
}
else
{
insertObj.setDATE_MODIFIED(DATE_INSERTED);
insertObj.update();
}
BSF.getObjectManager()._commitTransactionDirect(true);
}catch(Exception e)
{
String abc = e.getMessage();
}
}
fr.close();
br.close();
String localPath = FILE_NAME;
File f = new File(FILE_NAME);
String fullPath = f.getParent();
String fileName = f.getName();
String SubStr1 = new String("Processing");
int index = fullPath.lastIndexOf(SubStr1);
String path = fullPath.substring(0, index);
String destPath = path+"\\Archive\\"+fileName;
PMP_PROPERTIES.copyFile(new File(localPath),new File(destPath));
File file = new File(FILE_NAME);
file.delete();
return null;
}
There are two main problems. The first one is a performance problem - and, contrary to your intuition, the problem is the database insertion speed.
You are inserting each item in a separate transaction. You should not do that if you want your inserts to be quick. Introduce a counter variable and perform a commint only each N inserts and at the end.
int commitStep = 100;
int modCount = 0;
while() {
//... your code
modCount++;
if ( modCount % commitStep == 0 ) {
BSF.getObjectManager()._commitTransactionDirect(true);
}
}
You can read more about sql insert speed-up here: Sql insert speed up
The second problem is, possibly, file reading scalability. It will work for smaller files, but not for larger ones. This question Read large files in Java has some good answers to your problem.

Problem with ArrayLists and reading a file

I am having difficulty with the following method. I can't figure out if my problem is, but I have narrowed it down to not populating the array list from the file. Any help is greatly appreciated.
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
//create arraylists
ArrayList<String> model = new ArrayList<String>();
ArrayList<String> length = new ArrayList<String>();
ArrayList<String> width = new ArrayList<String>();
ArrayList<String> radius = new ArrayList<String>();
ArrayList<String> depth = new ArrayList<String>();
ArrayList<String> volume = new ArrayList<String>();
ArrayList<String> shape = new ArrayList<String>();
//fill arraylists from file
try {
String outputline = "";
BufferedReader fin = new BufferedReader(new FileReader("stock.dat"));
while((outputline = fin.readLine()) != null) {
// for(int i = 0; i < outputline.length(); i++) {
int i = 0;
//model
boolean flag = false;
String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
i++;
}
model.add(pass);
//length
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
length.add(pass);
//width
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
width.add(pass);
//radius
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
radius.add(pass);
//depth
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
depth.add(pass);
//volume
flag = false;
pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
else
flag = true;
}
volume.add(pass);
//shape
pass = "";
for(int j = i; j < outputline.length(); j++)
pass.concat(Character.toString(outputline.charAt(i)));
shape.add(pass);
}
fin.close();
}
catch(IOException e) {
System.err.print("Unable to read from file");
System.exit(-1);
}
int at = -1;
for(int i = 0; i < model.size(); i++) {
if(model.get(i).equals(searchIn.getText())) {
at = i;
i = model.size();
}
}
Component frame = null;
if(at != -1) {
searchDepthOut.setText(depth.get(at));
searchLengthOut.setText(length.get(at));
searchRadiusOut.setText(radius.get(at));
searchVolumeOut.setText(volume.get(at));
searchWidthOut.setText(width.get(at));
}
else
JOptionPane.showMessageDialog(null, "Your search did not return any results", "ERORR", JOptionPane.ERROR_MESSAGE);
}
Split the readline by a comma and be done with it. I'd also create an object for the model, length,width,etc... and then have 1 arraylist of that object.
while((outputline = fin.readLine()) != null) {
String[] tokens = outputline.split(",");
if(tokens.length == 7){
SObj o = new SObj; //Some Object
o.model = tokens[0];
o.length = tokens[1];
//and so on
oList.add(o);
}
}
Aside from all the other problems people have listed...
String pass = "";
while(flag = false) {
if(outputline.charAt(i) != ',')
pass.concat(Character.toString(outputline.charAt(i)));
pass is a String. Strings are immutable. You want
pass = pass.concat(.....)
while(flag = false) will never be run - it always evaluates to false. Try while (!flag)
I would suggest you restructure the code. The issue is not just that there's some sort of parser error, but that it's sort of hard to tell what's going on - the code is clearly making assumptions about the structure of the input line, but you have to kind of read through and trace the method to restructure it in your mind.
Something like
/** Expect a line of the form model, length, ...,
return a list of ...
*/
private String[] parse (String inputLine)
{
//check input line charachteristics-not null, length, ...
String out= inputLine.split(",");
if (out.length()!= ...
//whatever sanity checking...
}
private List<String[]> extract(BufferedReader fin)
{
while((outputline = fin.readLine()) != null)
{
//do something with parse(outputline);
}
}
The helpful thing will be to separate out the file reading and the line parsing, so that instead of doing it all in a long sequence you can see what's breaking, most likely an assumption about the line structure that's buried in the code. Does it need 4 comma separated integers? 5? how about if they're padded with spaces? prefixed with an empty line?

Categories

Resources