日期:2014-05-17  浏览次数:20723 次

真心被JDBC搞死了 MySQL弄了一个星期还是连接报错
win7 + jdk 1.7 + mySQL 5.5 + Tomcat 6

Mysql 安装没问题,命令行进去 show datebases 新建表 加数据乱七八糟都没问题
Tomcat没问题,叮叮当当调了好多servlet 正常
然后 调了这个噩梦般的程序:

Java code

package org.sunxin.lesson.jsp.bookstore;

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;

public class CreateDBServlet extends HttpServlet
{
    private String url;
    private String user;
    private String password;
        public String errortext;
        
    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)
        {
            errortext="加载数据库驱动失败!";
        }    
    }
    
    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("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从入门到精通','张三','张三出版社','2004-6-1',34.00,35,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.executeBatch();
            
            PrintWriter out=resp.getWriter();
            out.println(errortext);
            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;
            }
        }
    }
}



Tomcat6的 web.xml 里配置如下:
Java code

  <context-param>
        <param-name>driverClass</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>