hi all how are u? i have a problem and i need your help ..i have a code in java that connect to a mysql DB and update the rows of the table ...but i have a problem ...the new updated column called (original_text) contains the updated columnss from the column (content) but the while loop is not right coz the loop keep inserting the text in the new column without stopping ..i don't know what to do..and sorry for my bad english ...thnx alot in advance ...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conversion;
import java.sql.*;
//import javax.management.Query;
/**
 *
 * @author weblover
 */
public class DataBaseConvertor {

    public static void main (String [] args) throws Exception
    {
        //Vector<text> Alltext = new Vector<text>();
        Convertor c = new Convertor();
        String after = "";
        String before = "";
        String id = "";
        int number=0;
        int oldnumber=0;
        int chapter_number =0;
        int length=0;
        Connection conn = null;
        try
        {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/quran", "root", "");
        Statement stm = conn.createStatement();
        Statement stm1 = conn.createStatement();
        String Query = "SELECT * FROM `qtable_ar`";
        ResultSet res = stm.executeQuery(Query);

        while (res.next()){
            before = res.getString("content");
            id = res.getString ("id");
            oldnumber = Integer.parseInt(res.getString("number"));
            chapter_number = Integer.parseInt(res.getString("chapter_number"));
            c.SetBefore(before);
            after = c.Converte();
            length = c.getLength();
            number = c.newNumber(chapter_number, oldnumber);
            //AllAyat.add(new Aya(id, chapter_number, number, before, after, length));
            Query = "UPDATE `qtable`.`qtable_ar` SET `original_text` = '"+after+"', `length` = '"+length+"', `number` = '"+number+"' WHERE `qtable_ar`.`id` ="+id+" LIMIT 1 ;";
           //System.out.println(after);
            stm1.executeUpdate(Query);
            
        }
        }
        catch (Exception ex)
        {
            System.err.println("Exception : " + ex.getMessage());
        }
        finally {
            try {
                if (conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.err.println("SQL Exception : "+ e.getMessage());
            }
        

    }
    }

}

We don't know what the "Convertor" does. The code seems OK, are you sure the loop doesn't stop?
Try printing the data you get from the ResultSet and compare them with what data you have in the database.
Also I would suggest to close the Statements: stm, stm1 as well

hi ...first of all thnx for your reply ...the loop have a problem and i don't know what ...i print the data ...and i found out that he is updating the data in each row (the number of rows time) coz when he finish their is no data in the rows but (?????????null) and the length is 1923 ...but actually the length of each row i want to update is only 23 or more a bit ...thnx alot

Given the information it is difficult to understand why it doesn't work and I believe there isn't anything else you can tell me so I would suggest first:
Remove completely the update from the code.
Do you know how to use the Vector class? How to put objects inside it?
java.util.Vector

In your first loop after you get the data from the DB and convert them, put them in a Vector. Then outside the loop close the ResultSet, the Statement and print the Vector:

class Data {
  public String after = null;
  public int length = 0;
  public int number = 0;
  public String id = null;

  public Data() {
  }

  public Data(String after, int length, int number, String id) {
     // set the arguments to the global variables
    this.after = after;  
    .... 
  }
Vector v = new Vector();
while (res.next()){
            before = res.getString("content");
            id = res.getString ("id");
            oldnumber = Integer.parseInt(res.getString("number"));
            chapter_number = Integer.parseInt(res.getString("chapter_number"));
            c.SetBefore(before);
            after = c.Converte();
            length = c.getLength();
            number = c.newNumber(chapter_number, oldnumber);

            Data data = new Data(after, length, number, id);
            v.add(data);
        }
rs.close();
stm.close();

// print the Vector using a for loop
Data d = (Data)v.get(i);
System.out.println(d.after+","+d.length+","+.....);

Once you are satisfied by the results of the print from the Vector use another for loop to run the update

hi ...thnx again for your reply ....i tried the code that you told me ..and i got this Exception :

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2882)
        at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
        at java.lang.StringBuilder.append(StringBuilder.java:119)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at Conversion.Convertor.Converte(Convertor.java:55)
        at Conversion.DataBaseConvertor.main(DataBaseConvertor.java:43)

what to di now?

thnx for your reply ... thats my code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conversion;
import java.sql.*;
//import javax.management.Query;
/**
 *
 * @author Wassim
 */import java.util.Vector;
public class DataBaseConvertor {

    public static void main (String [] args) throws Exception
    {
               Convertor c = new Convertor();
        String after = "";
        String before = "";
        String id = "";
        int number=0;
        int oldnumber=0;
        int chapter_number =0;
        int length=0;
        Connection conn = null;
        try
        {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/q", "root", "");
        Statement stm = conn.createStatement();
        Statement stm1 = conn.createStatement();
        String Query = "SELECT * FROM `qtable_ar`";
        ResultSet res = stm.executeQuery(Query);

        Vector v = new Vector();
        while (res.next()){
            before = res.getString("content");
            id = res.getString ("id");
            oldnumber = Integer.parseInt(res.getString("number"));
            chapter_number = Integer.parseInt(res.getString("chapter_number"));
            c.SetBefore(before);
            after = c.Converte();
            length = c.getLength();
            number = c.newNumber(chapter_number, oldnumber);
           Data data = new Data(after, length, number, id);
            v.add(data);
                                 
        }
        res.close();
        stm.close();
        for(int i=0;i<v.size();i++){
        Data d = (Data)v.get(i);
        System.out.println(d.after+","+d.length);
        }
        }
        catch (Exception ex)
        {
            System.err.println("Exception : " + ex.getMessage());
        }
        finally {
            try {
                if (conn!=null) {
                    conn.close();
                }
            } catch (SQLException e) {
                System.err.println("SQL Exception : "+ e.getMessage());
            }
        

    }
    }

}

hi ...thnx again for your reply ....i tried the code that you told me ..and i got this Exception :

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at java.util.Arrays.copyOf(Arrays.java:2882)
        at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
        at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
        at java.lang.StringBuilder.append(StringBuilder.java:119)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        [B]at Conversion.Convertor.Converte(Convertor.java:55)
        at Conversion.DataBaseConvertor.main(DataBaseConvertor.java:43)[/B]

what to di now?

At line 43 DataBaseConvertor.java you call a method of Convertor.java
At line 55 Convertor.java that method gives you the error.
It usually happens when you repeatedly call something.
Example: an endless loop: while (true) {...} or keep running a recursive method without returning from it:

pubic int method() {
  return method();
}

In either way we would like to see the code of the method called,
and YOU need to better test that method in a different main method on its own.

hi ...i tried the methode(Converte) in the convertor class in a different main and it works correctly ..i don't know why this is happening ...thx for help.

Edited 7 Years Ago by weblover: n/a

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conversion;

import java.util.Vector;

/**
 *
 * @author Wassim
 */
public class Convertor {
    String before;
    String after;
    int length;
    int chapter_number;
    int number;
    
    Vector<Character> toDelete = new Vector<Character>();
    String unwanted = "ًٌٍَُِ~ّْ";


    public Convertor ()
    {
        
    }
    public Convertor(String before) {
        this.before = before;
        this.after = "";
        this.length = 0;
    }
    public void SetBefore (String before)
    {
        this.before=before;
    }
    public String Converte ()
    {
        char [] BeforeTable = before.toCharArray();
        Vector<Character> BeforeVector = new Vector<Character>();
        for (int i=0; i<unwanted.length(); i++) {
            toDelete.add(unwanted.charAt(i));
        }
        
        for (int i=0; i<BeforeTable.length; i++) {
            BeforeVector.add(BeforeTable[i]);
        }

            //BeforeVector.re
            BeforeVector.removeAll(toDelete);
        

        for (Character ca : BeforeVector) {
            after+=ca;
        }
        return after;
    }
    public int getLength ()
    {
        length = 0;
        for (int i=0; i<after.length(); i++)
        {
            if (after.charAt(i)!=' ')
                length++;
        }
        return length;
    }
    public int newNumber (int chapter_number, int number)
    {
        return (number+chapter_number);
    }

}

I will tell you in the morning what I think error is.
In the mean time why don't you try to create every time a new Converter in the loop

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conversion;

import java.util.Vector;

/**
 *
 */
public class Convertor {
    String before;
    String after;
    int length;
    int chapter_number;
    int number;
    
    Vector<Character> toDelete = new Vector<Character>();
    String unwanted = "ًٌٍَُِ~ّْ";


    public Convertor ()
    {
        
    }
    public Convertor(String before) {
        this.before = before;
        this.after = "";
        this.length = 0;
    }
    public void SetBefore (String before)
    {
        this.before=before;
    }
    public String Converte ()
    {
        char [] BeforeTable = before.toCharArray();
        Vector<Character> BeforeVector = new Vector<Character>();
        for (int i=0; i<unwanted.length(); i++) {
            toDelete.add(unwanted.charAt(i));
        }
        
        for (int i=0; i<BeforeTable.length; i++) {
            BeforeVector.add(BeforeTable[i]);
        }

            //BeforeVector.re
            BeforeVector.removeAll(toDelete);
        

        for (Character ca : BeforeVector) {
            after+=ca;
        }
        return after;
    }
    public int getLength ()
    {
        length = 0;
        for (int i=0; i<after.length(); i++)
        {
            if (after.charAt(i)!=' ')
                length++;
        }
        return length;
    }
    public int newNumber (int chapter_number, int number)
    {
        return (number+chapter_number);
    }

}

Edited 7 Years Ago by weblover: n/a

In your Converte method you keep adding elements into the vector todelete.
But in the main method you have only one instance of the Conversion class.
Meaning with each loop instead of calculating the new data from scratch you add every time you call the method more elements.
Since the unwanted is declared in the class and it is unchanged, why don't you add the elements to the todelete vector in the constructor.
From what I see you don't do anything else with the vector. You don't add any more items than the ones of the unwanted.

Have you tried to create a new Conversion object inside the while loop for each row read?

hi ..thnx alot for help ..i tried the convertor class on 1 row ....and it works perfectly without any errors .....i don't know what's happening when i run it inside the while loop ?:S

hi ..thnx alot for help ..i tried the convertor class on 1 row ....and it works perfectly without any errors .....i don't know what's happening when i run it inside the while loop ?:S

You have one instance, and you keep calling the Converte() which executes this code:

for (int i=0; i<unwanted.length(); i++) {
toDelete.add(unwanted.charAt(i));
}

Meaning that for each different row you add to the same Vector the unwanted characters. Since it is one instance you add to the Vector the characters and at the next loop you add more characters and with every loop you add more and more.

Try put the code the way you had it and do this:

public String Converte ()
    {
        char [] BeforeTable = before.toCharArray();
        Vector<Character> BeforeVector = new Vector<Character>();
        for (int i=0; i<unwanted.length(); i++) {
            toDelete.add(unwanted.charAt(i));
        }
        
       [B] System.out.println("-------------------------");
        System.out.println(toDelete);[/B]

        for (int i=0; i<BeforeTable.length; i++) {
            BeforeVector.add(BeforeTable[i]);
        }

            //BeforeVector.re
            BeforeVector.removeAll(toDelete);
        

        for (Character ca : BeforeVector) {
            after+=ca;
        }
        return after;
    }

You will see the elements of the Vector keep increasing.
It would be better to put that part of the code at the constructor if you want to have one instance, or have the method to be static that takes as argument the 'before' and define the Vector inside the method.

Won't the statement:

c.SetBefore(before);

keep moving the cursor back one from where it is when rs.next() was called?

Won't the statement:

c.SetBefore(before);

keep moving the cursor back one from where it is when rs.next() was called?

Of course not. Calling methods with the results you get from rs don't affect it

hi all ..sorry because i wasn't answering ....my internet connection was :S ..i wil be back soon ..and i still need your help...sorry

This article has been dead for over six months. Start a new discussion instead.