叶风 2011-03-11 23:51
浏览 310
已采纳

MySQL数据库连接问题。

[code="java"][/code]我是个Java开发的初学者,最近开始自学Servlet/JSP编程,参考的是孙鑫老师编的《Servlet/Jsp深入详解——基于Tomcat的Web开发》。
在学习“第4章数据库访问”时遇到了问题,我按照书上的代码编写了一个CreateDBServlet.java用于连接数据库,编译是没问题的。进行了部署和配置后,访问时出错了,浏览器显示的错误信息如下:
HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
org.sunxin.ch04.servlet.CreateDBServlet.doGet(CreateDBServlet.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:718)
com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
java.sql.DriverManager.getConnection(DriverManager.java:525)
java.sql.DriverManager.getConnection(DriverManager.java:171)
org.sunxin.ch04.servlet.CreateDBServlet.doGet(CreateDBServlet.java:44)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.


Apache Tomcat/6.0.18

CreateDBServlet代码如下:
[code="java"]package org.sunxin.ch04.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateDBServlet extends HttpServlet
{
private String userUrl;
private String userName;
private String password;

public void init() throws ServletException
{
    String driverClass=getInitParameter("driverClass");
    userUrl=getInitParameter("userUrl");
    userName=getInitParameter("userName");
    password=getInitParameter("password");
    try
    {
        Class.forName(driverClass);
    }
    catch (ClassNotFoundException ce)
    {
        throw new ServletException("加载数据库驱动失败!");
    }
}

public void doGet(HttpServletRequest req,HttpServletResponse resp)
        throws ServletException,IOException
{
    Connection conn=null;
    Statement stmt=null;
    try
    {
        conn=DriverManager.getConnection(userUrl,userName,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(50) 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 详解','孙鑫','电子工业出版社','2006-4-20',99.00,35,null)");
        stmt.addBatch("insert into bookinfo values(2,'Struts 2深入详解','孙鑫','电子工业出版社','2008-6-15',79.00,20,null)");
        stmt.addBatch("insert into bookinfo values(3,'Servlet/JSP详解','孙鑫','电子工业出版社','2008-7-1',79.00,10,null)");
        stmt.executeBatch();

        resp.setContentType("text/html;charset=GBK");
        PrintWriter out=resp.getWriter();
        out.println("数据库创建成功!");
        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;
        }
    }
}

}[/code]

web.xml文件如下:
[code="java"]<?xml version="1.0" encoding="gb2312"?>

xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>createDB</servlet-name>
    <servlet-class>org.sunxin.ch04.servlet.CreateDBServlet</servlet-class>
    <init-param>
        <param-name>driverClass</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </init-param>
    <init-param>
        <param-name>userUrl</param-name>
        <param-value>jdbc:mysql://localhost:3306</param-value>
    </init-param>
    <init-param>
        <param-name>userName</param-name>
        <param-value>root</param-value>
    </init-param>
    <init-param>
        <param-name>passord</param-name>
        <param-value>12345678</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>createDB</servlet-name>
    <url-pattern>/createdb</url-pattern>
</servlet-mapping>


[/code]

  • 写回答

7条回答 默认 最新

  • 勤劳的小猿 2011-03-12 10:27
    关注

    [quote]exception
    javax.servlet.ServletException: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exits bookstore' at line 1 [/quote]

    这个是在你写的sql语句中有语法错误。
    逻辑是先判断有删除 你的exits用法不对。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池