Results 1 to 5 of 5
  1. #1
    lucida is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Thumbs up Need help to replace this pstmt.setString(1, strResult[0]); with for loop

    import java.io.*;
    import java.sql.*;



    public class dds {

    /**
    * @param args
    * @throws Exception
    */
    public static void main(String[] args) throws Exception{

    try
    {

    FileInputStream fstream = new FileInputStream("C:/eclipse-jee-helios-SR1-win32/eclipse/InsertFiles/src/dds.txt");
    //Reading the file from the Notepad
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strInputLine = null;
    while ((strInputLine = br.readLine()) != null) {
    int iIdx;
    //Using Regular expression to ignore Comma when it is sandwiched between Double quotes
    String[] strResult = strInputLine.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

    for(iIdx = 0; iIdx < strResult.length; iIdx++) {
    //Replacing all double Quotes with empty
    strResult[iIdx] = strResult[iIdx].replaceAll("\"", "");}
    //System.out.println(strResult[iIdx]);


    Connection connection = null;
    PreparedStatement pstmt=null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    connection = DriverManager.getConnection("jdbc:oracle:thin:@ETC HNDKT003:1521:xe","system","tiger");
    //Connecting with the Oracle Database

    String query = "INSERT INTO dds1(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f1 4,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,f25,f26, f27,f28,f29,f30,f31,f32,f33,f34,f35) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    //Inserting into an Oracle Database
    pstmt = connection.prepareStatement(query);
    // for(String : strResult[iIdx]) {
    pstmt.setString(1, strResult[0]);
    pstmt.setString(2, strResult[1]);
    pstmt.setString(3, strResult[2]);
    pstmt.setString(4, strResult[3]);
    pstmt.setString(5, strResult[4]);
    pstmt.setString(6, strResult[5]);
    pstmt.setString(7, strResult[6]);
    pstmt.setString(8, strResult[7]);
    pstmt.setString(9, strResult[8]);
    pstmt.setString(10, strResult[9]);
    pstmt.setString(11, strResult[10]);
    pstmt.setString(12, strResult[11]);
    pstmt.setString(13, strResult[12]);
    pstmt.setString(14, strResult[13]);
    pstmt.setString(15, strResult[14]);
    pstmt.setString(16, strResult[15]);
    pstmt.setString(17, strResult[16]);
    pstmt.setString(18, strResult[17]);
    pstmt.setString(19, strResult[18]);
    pstmt.setString(20, strResult[19]);
    pstmt.setString(21, strResult[20]);
    pstmt.setString(22, strResult[21]);
    pstmt.setString(23, strResult[22]);
    pstmt.setString(24, strResult[23]);
    pstmt.setString(25, strResult[24]);
    pstmt.setString(26, strResult[25]);
    pstmt.setString(27, strResult[26]);
    pstmt.setString(28, strResult[27]);
    pstmt.setString(29, strResult[28]);
    pstmt.setString(30, strResult[29]);
    pstmt.setString(31, strResult[30]);
    pstmt.setString(32, strResult[31]);
    pstmt.setString(33, strResult[32]);
    pstmt.setString(34, strResult[33]);
    pstmt.setString(35, strResult[34]);


    pstmt.executeUpdate();
    connection.commit();
    System.out.println("Records inserted into database...");

    pstmt.close();
    connection.close();
    //Closing the Oracle Database
    }}

    catch (ClassNotFoundException e) {
    System.out.println("ClassNotFound");// Could not find the database driver
    } catch (SQLException e) {
    e.printStackTrace() ;// Could not connect to the database
    }

    }
    }


    Hi All,Iam very new to JAVA ...I want some of your help to finish off my project ..The above coding is to read the file from the notepad and it has to split by comma and have to store into oracle database..This works very fine for me I can able to successfully insert into database but to improve the efficiency I want pstmt.setString(1, strResult[0]);
    pstmt.setString(2, strResult[1]);
    pstmt.setString(3, strResult[2]); to replace with for loop ..Can anyone help on this please its very urgent.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help to replace this pstmt.setString(1, strResult[0]); with for loop

    If it's so urgent for you, why didn't you come for help here earlier? w.r.t. your question: you want to set query field #i to the value of array element #i-1; alternatively, you want to set array element #i to query field #i+1.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Need help to replace this pstmt.setString(1, strResult[0]); with for loop

    Please use [code] tags [/code] when posting code.

    Jos has answered your immediate question, but:
    Java Code:
    FileInputStream fstream = new FileInputStream("C:/eclipse-jee-helios-SR1-win32/eclipse/InsertFiles/src/dds.txt");
    //Reading the file from the Notepad
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    What is the point of the DataInputStream?
    I see a lot of people do this, and it adds nothing.

    You also should close your resources (connection etc) in a finally block.
    If that code above threw an exception then you'd have a connection left open to your db, which is not a Good Thing.
    Please do not ask for code as refusal often offends.

  4. #4
    lucida is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help to replace this pstmt.setString(1, strResult[0]); with for loop

    Quote Originally Posted by JosAH View Post
    If it's so urgent for you, why didn't you come for help here earlier? w.r.t. your question: you want to set query field #i to the value of array element #i-1; alternatively, you want to set array element #i to query field #i+1.

    kind regards,

    Jos
    Thanks Jos for ur immediate reply but I can't get you ...Can you tell me this in brief with code

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help to replace this pstmt.setString(1, strResult[0]); with for loop

    Quote Originally Posted by lucida View Post
    Thanks Jos for ur immediate reply but I can't get you ...Can you tell me this in brief with code
    Sure, you have to do either this:

    Java Code:
    pstmt.setString(i, strResult[i-1]);
    or this:

    Java Code:
    pstmt.setString(i+1, strResult[i]);
    You pick your choice and craft a little for-loop around it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. String.replace ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 03:04 AM
  2. problem in setString method
    By gokul1242 in forum Java Servlet
    Replies: 5
    Last Post: 05-10-2011, 08:12 AM
  3. How to replace and integer with another
    By franzwarning in forum New To Java
    Replies: 7
    Last Post: 11-26-2010, 10:21 PM
  4. is there anyway to replace java.IO
    By nobody58 in forum Advanced Java
    Replies: 1
    Last Post: 03-19-2010, 01:19 PM
  5. How can i setString when i use radio buttons?
    By Tahorn in forum AWT / Swing
    Replies: 4
    Last Post: 02-16-2010, 02:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •