dbStatement = con.createStatement();
dbResult = dbStatement.executeQuery("SELECT Vendor_Code FROM temp ORDER BY status ASC ");
while (dbResult.next())
{
VendorCode=dbResult.getString("Vendor_Code");
System.out.println(VendorCode);
dbStatement.executeUpdate("INSERT INTO temp1 VALUES ('"+VendorCode+"')");
}
In the above code I am selecting a list of vendor code in the ascending order of their status,it works properly if I don’t add:
dbStatement.executeUpdate("INSERT INTO temp1 VALUES ('"+VendorCode+"')");
into the while loop. If I add this I’m getting result as only the first value which is in the sorted list and also it’s getting inserted into the temp1 table…
I am using Java swing and MySQL in NetBeans. Any idea please why this is happening?
If I do the above code in other way as,
dbStatement = con.createStatement();
dbInsert = con.createStatement();
dbResult = dbStatement.executeQuery("SELECT Vendor_Code FROM temp ORDER BY status ASC ");
while (dbResult.next())
{
VendorCode=dbResult.getString("Vendor_Code");
dbResult = dbInsert.executeQuery("SELECT Bid_No,Vendor_Name,Vendor_Address,Amount,Tax_Percentage,Amount_Aftertax,Expected_Deliverydate,Vendor_Code FROM purchase_bid where PE_Number='"+penumber+"' AND Vendor_Code='"+VendorCode+"' ");
while(dbResult.next())
{
Bid_Number=dbResult.getString("Bid_No");
vendor_name=dbResult.getString("Vendor_Name");
vendor_address=dbResult.getString("Vendor_Address");
Amount=dbResult.getString("Amount");
tax=dbResult.getString("Tax_Percentage");
date2=dbResult.getString("Expected_Deliverydate");
Amount_Aftertax=dbResult.getString("Amount_Aftertax");
venCode=dbResult.getString("Vendor_Code");
date3=date2.split("-");
String day="";
String month="";
String year="";
day=date3[2];
month=date3[1];
year=date3[0];
date=day+"-"+month+"-"+year;
addtoCart();//for displaying it in jTable
}
}
It does not take VendorCode in ascending order for retrieving values from purchase_bid table..Then it again takes first sorted value and nothing is displayed in jTable.
You should only use the statement instance for one operation. So calling the executeUpdate-method erases the ResultSet obtained by the executeQuery-method.
Create a second statement, so that:
If you’re not just testing and you actually want to bulk insert the Vendor_Code from temp to temp1, try using INSERT … SELECT
See the official MySQL documentation for further details on this type of INSERT:
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html