public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String content = request.getParameter("content");
String categoryId = request.getParameter("category");

DataSource ds = null;

try{
Context context = new InitialContext(); 
ds = (DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
}catch(Exception e){
System.out.println("获取数据源时出错");
}
try{
Connection conn = ds.getConnection();

String sql = "insert into blog (title,content,category_id,createdtime) values (?,?,?,now())";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setInt(3, Integer.parseInt(categoryId));
int result = pstmt.executeUpdate();
System.out.println(result);
}catch(SQLException e){
e.printStackTrace();
}
}
}
我的数据库
/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2009-8-20 17:20:46                           */
/*==============================================================*/
drop table if exists blog;drop table if exists category;drop table if exists comment;drop table if exists user;/*==============================================================*/
/* Table: blog                                                  */
/*==============================================================*/
create table blog
(
   id                   int not null,
   category_id          int,
   title                varchar(400),
   content              varchar(4000),
   created_time         datetime,
   primary key (id)
);/*==============================================================*/
/* Table: category                                              */
/*==============================================================*/
create table category
(
   id                   int not null,
   name                 varchar(200),
   level                int,
   primary key (id)
);/*==============================================================*/
/* Table: comment                                               */
/*==============================================================*/
create table comment
(
   id                   int not null,
   blog_id              int,
   username             varchar(200),
   content              varchar(1000),
   primary key (id)
);/*==============================================================*/
/* Table: user                                                  */
/*==============================================================*/
create table user
(
   id                   int not null,
   username             varchar(200),
   password             varchar(200),
   primary key (id)
);alter table blog add constraint FK_Relationship_1 foreign key (category_id)
      references category (id) on delete restrict on update restrict;alter table comment add constraint FK_Relationship_2 foreign key (blog_id)
      references blog (id) on delete restrict on update restrict;出现问题,老是插不进去:
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`blog`, CONSTRAINT `FK_Relationship_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`))
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1011)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at cn.com.jobedu.blog.BlogServlet.doPost(BlogServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'createdtime' in 'field list'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at cn.com.jobedu.blog.BlogServlet.doPost(BlogServlet.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)

解决方案 »

  1.   

    com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`blog`, CONSTRAINT `FK_Relationship_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`))从这句,感觉是有外键冲突,插入了不存在的主表ID。
      

  2.   


    a foreign key constraint fails (`test`.`blog`, CONSTRAINT `FK_Relationship_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`))
    你数据库表blog的外键category_id加了约束条件的原因,你可以到数据库中删了这个约束就OK了
      

  3.   

    com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`blog`, CONSTRAINT `FK_Relationship_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`)) 
    数据库表字段category_id加了外键约束,可能你插入的category_id在category表中不存在
      

  4.   

    String sql = "insert into blog (title,content,category_id,createdtime) values (?,?,?,now())"; 
    把sql打印出来 
      执行下看看·~
      

  5.   

    和3楼的意见一样,可能你的category表中没有你插入的数据,你应该先把category表中插入数据,另一张表才可以引用
      

  6.   

    a foreign key constraint fails (`test`.`blog`, CONSTRAINT `FK_Relationship_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`))
    子表的外键列没有赋值。比如你的blog里面的 catagory_id 就没有数值!OVER
      

  7.   

    Unknown column 'createdtime' in 'field list
      

  8.   

    那是因为外键所对应的那个表:category 里没有先插入数据