看“通過讀取 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)");
大家看一下我註釋掉的這行是錯誤的。我的提交便不能全部成功。那就是出現了異常了吧。具體往下我就不知道了。不過將這樣的錯誤行改正就可以把白屏問題解決了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章