【问题标题】:Connection of JSP with MySQL failedJSP 与 MySQL 的连接失败
【发布时间】:2012-02-22 00:53:45
【问题描述】:

好的,我无法将我的 JSP 程序与 MYSQL 表连接以创建、检索或对程序执行任何操作

使用 NetBeans 6.8、win XP sp 2、TOMCAT 6 等...

在显示已连接表的 NetBeans 中,已在 C:\apache-tomcat-5.5.33\apache-tomcat-5.5.33\common\lib\mysql-connector-java-5.1.18-bin.jar 中应用了 MYSQL 连接器

我用来连接的代码是

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
</head>
<body>
    <h1>Creating Table in DB using JSP</h1>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<html>
    <head>
        <title>display data from the table using jsp</title>
    </head>
    <body>
        <%
                    /* Create string of connection url within specified
                    format with machine name, port number
                    and database name. Here machine name id localhost
                    and database name is usermaster. */
                    String connectionURL = "jdbc:mysql://localhost:3306/ipsj";
                    // declare a connection by using Connection interface
                    Connection connection = null;
                    // declare object of Statement interface that is used for
                    // executing sql statements.
                    Statement statement = null;
                    try {
                        // Load JBBC driver "com.mysql.jdbc.Driver".
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        /* Create a connection by using getConnection() method
                        that takes parameters of string type connection url, user
                        name and password to connect to database. */
                        connection = DriverManager.getConnection(connectionURL, "root", "");

                        /* createStatement() is used for create statement object
                        that is used for sending
                        sql statements to the specified database. */
                        statement = connection.createStatement();
                        // sql query to retrieve values from the secified table.
                        String QueryString = "create table user_master(id int not null auto_increment,name " + "varchar(25),city varchar(20), primary key(id));";
                        // execyteUpdate() mothod execute specified sql query.
                        statement.executeUpdate(QueryString);
                        out.print("QueryString");
        %>
        <TABLE>
            <TR>
                <TD align="center" >
                    <font size="+3" color="green">Congratulations !</font>
                </TD>
            </TR>
            <tr>
                <TD align="center" >
                    <font size="+2">Table of specified name is created successfully.</font>
                </TD>
            </tr>

        <%      } catch (Exception ex) {%>
        </TABLE>
        <TABLE>
            <TR>
                <TD align="center" >
                    <font size="+3" color="red">Some problems to create table.</font>
                </TD>
            </TR>
            <%
            }

            %>
            <tr>
                <td align="center" >
                    <A HREF="welcome_to_database_query.jsp">
                        <font size="5" color="blue">back to home page</font>
                    </A>
                </td>
            </tr>
        </TABLE>
</body>

我不知道出了什么问题,但它无法连接。我已经做了什么:

  • 已安装 MYSQL 服务器,
  • 重新启动 TOMCAT,
  • 完成了管理工具连接的事情。

【问题讨论】:

  • Class.forName("com.mysql.jdbc.Driver").newInstance();中不需要newInstance
  • @JigarJoshi 执行的程序中有 catch 块....
  • @RanRag 会尝试告诉请稍等.....
  • 尝试在那个 catch 块中发送 exceptionStacktrace 并告诉我们
  • 谢谢大家,但我找到了答案:)

标签: java mysql database jsp jdbc


【解决方案1】:

好的,上面给出的程序连接MYSQL和JSP的解决方案。我问过我的老板,他是个真正的专家......

首先打开Netbeans

点击服务 然后右键单击服务器,就像我的“Apache Tomcat”一样,然后选择Edit Server.XML

在我认为 GlobalNamingResources 标记之间的第 39 行添加下面

<Resource name="jdbc/DATABASENAME" type="javax.sql.DataSource" maxActive="4" maxIdle="2" username="root" password="" maxWait="23000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/DATABASENAME"></Resource>

并保存

现在选择项目

然后右键单击您正在工作的项目并选择Properties

现在在 Libraries 在 Compile 选项卡下单击 Add JAR 然后找到你的“mysql-connector-java-5.1.18-bin.jar”

然后点击确定

然后重启服务器

然后就完成了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 2016-03-19
    相关资源
    最近更新 更多