Results 1 to 5 of 5
- 03-13-2012, 07:48 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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.
- 03-13-2012, 08:23 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-13-2012, 11:12 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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:
What is the point of the DataInputStream?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));
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.
- 03-14-2012, 12:49 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
- 03-14-2012, 02:22 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help to replace this pstmt.setString(1, strResult[0]); with for loop
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
String.replace ?!
By HearT.Hunt3r in forum New To JavaReplies: 4Last Post: 08-22-2011, 03:04 AM -
problem in setString method
By gokul1242 in forum Java ServletReplies: 5Last Post: 05-10-2011, 08:12 AM -
How to replace and integer with another
By franzwarning in forum New To JavaReplies: 7Last Post: 11-26-2010, 10:21 PM -
is there anyway to replace java.IO
By nobody58 in forum Advanced JavaReplies: 1Last Post: 03-19-2010, 01:19 PM -
How can i setString when i use radio buttons?
By Tahorn in forum AWT / SwingReplies: 4Last Post: 02-16-2010, 02:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks