2014-07-24 52 views
0
String str="insert into subcatag(subcatagid,subcatagory,catagid) values (?,?,select  
catagid from catag where catagory=?)"; 
PreparedStatement st=con.prepareStatement(str); 
status=st.executeUpdate(); 
st.setString(1,v.getSubcatagid()); 
st.setString(2,v.getSubCatagory()); 
st.setString(3,v.getCatagid()); 

其中 v.getSubcatagid() v.getSubCatagory() v.getCatagid()都是POJO類方法..我不能插入值到從JSP輸入數據庫表。在哪裏我錯了提前謝謝。 我的表包含subcatagid int auto_inc主鍵,subcatagory varchar(50)不爲null,以及一個從catag表catagid的外鍵列。這是正確的代碼關於外鍵關係

回答

1

嘗試在Java中使用批處理語句

示例代碼:

import java.sql.Connection; 
import java.sql.Statement; 

//... 

Connection connection = new getConnection(); 
Statement statement = connection.createStatement(); 

for (Employee employee: employees) { 
String query = "insert into employee (name, city) values('" 
     + employee.getName() + "','" + employee.getCity + "')"; 
statement.addBatch(query); 
} 
statement.executeBatch(); 
statement.close(); 
connection.close(); 

可能是這個鏈接將幫助

Batch statement