JSP与MySQL的连接失败

时间:2012-01-30 06:49:29

标签: java mysql database jsp jdbc

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

使用NetBeans 6.8,赢取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,
  • 完成管理工具连接。

1 个答案:

答案 0 :(得分:0)

Ok这是在给定程序上连接MYSQL和JSP的解决方案。我问过我的老板,他是一位真正的专家...

首先打开 Netbeans

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

在第39行添加以下行我认为 GlobalNamingResources 标记

<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>

并保存

现在选择项目

然后右键单击您正在使用的项目,然后选择属性

现在在库中在“编译”选项卡下,单击添加JAR 然后找到你的“mysql-connector-java-5.1.18-bin.jar”

然后点击“确定”

然后重启服务器

然后完成!