看“通过读取 web.xml 文件中的配置参数连接数据库”帖子后的想法

/**
* @(#)CreateDBServlet.java
*
*
* @author
* @version 1.00 2009/6/24
*/
package org.sunxin.lesson.jsp.bookstore;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class CreateDBServlet extends HttpServlet
{
private String url;
private String user;
private String password;
public void init()throws ServletException
{
String driverClass=getInitParameter("driverClass");
url=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
throw new UnavailableException("加载数据库驱动失败");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
try{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("use bookstore");
stmt.executeUpdate("create table bookinfo(id INT NOT NULL primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,bookconcern VARCHAR(100) not null,publish_date DATE not null,price FLOAT(4,2) not null,amout SMALLINT,remark VARCHAR(200))ENGINE=InnoDB");
//stmt.addBatch("insert into bookinfo values(1,'java从入门到精通','张三','张三出版社'.'2004-10-1',34.00,35,null)");
stmt.addBatch("insert into bookinfo values(1,'jsp深入详解','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(2,'jsp深入详解','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(3,'J2EE高级编程','王五','王五出版社','2005-3-1',78.00,10,null)");
stmt.addBatch("insert into bookinfo values(4,'jsp深入详解','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(5,'jsp深入详解','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.addBatch("insert into bookinfo values(6,'jsp深入详解','李四','李四出版社','2004-10-1',56.00,20,null)");
stmt.executeBatch();
PrintWriter out=resp.getWriter();
out.println("success!");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
if(stmt!=null)
{
try{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}

//stmt.addBatch("insert into bookinfo values(1,'java从入门到精通','张三','张三出版社'.'2004-10-1',34.00,35,null)");
大家看一下我注释掉的这行是错误的。我的提交便不能全部成功。那就是出现了异常了吧。具体往下我就不知道了。不过将这样的错误行改正就可以把白屏问题解决了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值