这个代码是否正确关于外键关系

时间:2014-07-24 13:16:02

标签: mysql sql

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主键,子目录varchar(50)not null,以及一个来自catag表的catagid的外键列。

1 个答案:

答案 0 :(得分: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