1

我正在学习 JSP,我想使用 JSP 在 mysql 中创建一个表。我有以下代码。

    <%@ page contentType="text/html;charset=UTF-8" %>
    <%@ page errorPage="error.jsp" %>
    <%@ page import="java.sql.*" %>

    <html>
    <head>
    <title>MySQL Database creation</title>
    <style>
      { font-size: 12px; font-family: Verdana }
    </style>
    </head>
    <body>

    <h2>Creation of a books database</h2>

    <jsp:declaration>

    Statement stmt;
    Connection con;
    String url = "jdbc:mysql://localhost:3306/";

    </jsp:declaration>

    <jsp:scriptlet><![CDATA[

    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(url, "root", ""); 

    stmt = con.createStatement();
    stmt.executeUpdate("CREATE DATABASE books");
    con.close();

    ]]></jsp:scriptlet>

    </body>
    </html>

我的error.jsp页面是

   <%@ page contentType="text/html" pageEncoding="UTF-8"%>
   <%@ page isErrorPage="true" %>

   <html>
   <head>
   <title>Error page</title>
   <style>
     { font-size: 12px; font-family: Verdana }
   </style>
   </head>
   <body>
   <h2>Error occured!</h2>
   <p>Message 
   <jsp:expression> 
        exception.getMessage() 
   </jsp:expression>
   </p>

   </body>
   </html>

当我运行它时,它被重定向到错误页面并获得如下输出:

发生了错误!消息 com.mysql.jdbc.Driver

我哪里错了?

4

3 回答 3

1

用于前端的 Jsp,因此请避免在此处使用数据库代码

为您解决

添加jar文件下载

mysqlconnecter.jar
于 2013-06-27T09:19:48.867 回答
0

用这个

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page errorPage="error.jsp" %>
<%@ page import="java.sql.*" %>

<html>
<head>
<title>MySQL Database creation</title>
<style>
  { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>

<h2>Creation of a books database</h2>

<jsp:declaration>

Statement stmt;
Connection con;
String url = "jdbc:mysql://localhost:3306/your database name";

</jsp:declaration>

<jsp:scriptlet><![CDATA[

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, "root", ""); 

stmt = con.createStatement();
stmt.executeUpdate("CREATE TABLE `test`.`sample` (`id` TINYINT UNSIGNED), `name` VARCHAR (50)");
con.close();

]]></jsp:scriptlet>
于 2013-06-27T09:58:45.210 回答
0

朋友下载你的mysqlconnector后,你应该提供jdk的mysqlconnector路径。

于 2014-01-25T18:05:42.763 回答