I assume that your select statement is use to find out if the data is already in the database before you insert?
Main Topics
Browse All TopicsHi Expert,
I'm doing the project that need to download data from web page as a string array and use these data insert to database. Any help will be great.
Here is my example code
-----------
try
{
Class.forName("sun.jdbc.od
Connection dbConn = DriverManager.getConnectio
if (dbConn == null)
{
System.out.println("Connec
}
else
{
try
{
InetTemplate obj = new Inet () ;
String strContent = obj.getURL("http://my_webp
String strData = obj.getData(strContent);
BufferedReader reader = new BufferedReader(new StringReader(strData));
String line;
int count = 0;
array = new String[120];
while ((line = reader.readLine()) != null)
{
array[count] = new String((line)+ "\r\n ");
count++;
}
}
catch (Exception e)
{
System.out.println(e.getMe
System.exit(0);
}
Statement stmt = dbConn.createStatement();
String dbSQL = "SELECT * FROM table WHERE col_Name=''";
ResultSet rs = stmt.executeQuery(dbSQL);
if (!rs.next())
{
dbSQL = "INSERT INTO table (col_Name) VALUES (";
for (int i=0;i<array.length;i++)
{
dbSQL += "' " + array[i] + " ',";
}
dbSQL += ")";
stmt.executeUpdate(dbSQL);
}
rs.close();
}
dbConn.close();
Statement stmt = null;
}
catch (Exception e)
{
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If you are.. try this script (provided you know how many fields the table have and the field name). Just replace the 'DATA1', 'DATA2', 'DATA3'.... with the value in the array, and field1, field2... with the field name in the table.
insert into table_1
select top 1
'DATA1', 'DATA2', 'DATA3'
from table_1
where
not exists (
select * from table_1
where field1 = 'DATA1'
and field2 = 'DATA2'
and field3 = 'DATA3')
using PreparedStatement:
String query =
"insert into table_1 " +
"select top 1 " +
"?,?,? " +
"from table_1 " +
"where " +
"not exists ( " +
"select * from table_1 " +
"where field1 = ? " +
"and field2 = ? " +
"and field3 = ?)";
PreparedStatement stmt = conn.prepareStatement(quer
stmt.setString(1, array1[0]);
stmt.setString(2, array1[1]);
//and so on
stmt.executeUpdate();
--------------------------
hope this helps.... if not, just ignore me ;-)
Doesn't sound like you need to do an insert then.
batch update may be the way to go
http://javaalmanac.com/egs
Business Accounts
Answer for Membership
by: objectsPosted on 2006-05-25 at 17:09:50ID: 16765572
> ResultSet rs = stmt.executeQuery(dbSQL);
why do you do a select if you want to *insert* the strings?
sounds like u should be instead looping thru your array inserting a row for each string
for (int i=0; i<array.length; i++) {
// insert array[i]
}
A PreparedSTatement could be used for this