现在的位置: 首页 > 综合 > 正文

java servlet 连接数据库

2012年12月08日 ⁄ 综合 ⁄ 共 2608字 ⁄ 字号 评论关闭
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreatDBServlet extends HttpServlet{

	private String url,user,password;
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		String driverClass = this.getInitParameter("driverClass");
		url = this.getInitParameter("url");
		user = this.getInitParameter("user");
		password = this.getInitParameter("password");
		try 
		{
			Class.forName(driverClass);
		}
		catch (ClassNotFoundException ce)
		{
			throw new ServletException(driverClass+"数据库加载失败");
		}
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doGet(req, resp);
		Connection conn=null;
		Statement stmt=null;
		try 
		{
			conn=DriverManager.getConnection(url,user,password);
			stmt=conn.createStatement();
			stmt.executeUpdate("drop database if exists bookstore");
			stmt.executeUpdate("create database bookstore");
			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,amount SMALLINT,remark VARCHAR(200))ENGINE=InnoDB");
			stmt.addBatch("insert into bookinfo values(1,'Java web 开发','author','.....','2006-4-20',99.00,35,null)");
			stmt.executeBatch();
			resp.setContentType("text/html;charset=gbk");
			PrintWriter out=resp.getWriter();
			out.println("success,创建数据库成功");
			out.close();
		}
		catch (SQLException se)
		{
			throw new ServletException(se);
		}
		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;
			}
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(req, resp);
		doGet(req,resp);
	}
}
把mysql的jdbc驱动mysql-connector-java-5.1.18-bin.jar的路径.../mysql-connector-java-5.1.18-bin.jar添加的环境变量的classpath中,并拷贝到tomcat目录中的common\lib内

在web.xml中配置参数
</servlet>
 <servlet>
  <servlet-name>CreatDBServlet</servlet-name>
  <servlet-class>CreatDBServlet</servlet-class>
  <init-param>
   <param-name>driverClass</param-name>
   <param-value>com.mysql.jdbc.Driver</param-value>
  </init-param>
  <init-param>
   <param-name>user</param-name>
   <param-value>root</param-value>
  </init-param>
  <init-param>
   <param-name>password</param-name>
   <param-value>mysql的root密码</param-value>
  </init-param>
  <init-param>
   <param-name>url</param-name>
   <param-value>jdbc:mysql://localhost:3306</param-value>
  </init-param>
 </servlet>

抱歉!评论已关闭.