日期:2014-05-18 浏览次数:20930 次
package com.fox_ice.servlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class CreateDBServlet2 extends HttpServlet
{
String url1;
String user;
String password;
String driverClass;
public void init() throws ServletException
{
driverClass=getInitParameter("driverClass");
url1=getInitParameter("url");
user=getInitParameter("user");
password=getInitParameter("password");
try
{
Class.forName(driverClass);
}
catch(ClassNotFoundException ce)
{
ce.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
Connection conn=null;
Statement stmt=null;
request.setCharacterEncoding("gbk");
try
{
conn=DriverManager.getConnection(url1,user,password);
stmt=conn.createStatement();
stmt.executeUpdate("drop database if exists guest");
stmt.executeUpdate("create database guest");
stmt.executeUpdate("use guest");
stmt.executeUpdate("create table guestbook(id INT not null primary key,title VARCHAR(50) not null,author VARCHAR(50) not null,contents VARCHAR(100) not null");
stmt.addBatch("insert into guestbook values(1,'我的留言','fox_ice','我的留言我的留言我的留言我的留言我的留言')");
stmt.addBatch("insert into guestbook values(2,'我的留言','fox_ice','我的留言我的留言我的留言我的留言我的留言')");
stmt.addBatch("insert into guestbook values(3,'我的留言','fox_ice','我的留言我的留言我的留言我的留言我的留言')");
stmt.executeBatch();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("数据库建表成功");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
if(null!=conn)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
if(null!=stmt)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
}
}
}