0

我是新手,我正在尝试在 Servlet 的帮助下连接 MySQL 数据库,但是我无法连接它。当我尝试使用 JSP 页面运行此 Servlet 时,它工作正常(无连接)

这是我的代码。

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


@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {


/**
     * 
     */
    private static final long serialVersionUID = 1L;

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
     // String text = request.getParameter("name");
      String toolfirst=request.getParameter("tname1");
      String toolsecond=request.getParameter("tname2");
      String toolvalue=request.getParameter("tvalue");
      if (toolfirst == null)
          toolfirst = "World";
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<body>");
      out.println("<h1>Hello " + toolfirst + "</h1>");
      out.println("</body>");
      out.println("</html>");


      //Connection conn = null;


      try {
       Class.forName("com.mysql.jdbc.Driver").newInstance();
       Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tool","root","");
       System.out.println("Connected!");

      } catch(Exception e) {

       System.out.println("not connected");


      }


     }
   }

安慰 :

Nov 07, 2013 9:57:11 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\UI Development\Mukesh\software\Eclipse EE developer\eclipse;;.
Nov 07, 2013 9:57:12 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:test1' did not find a matching property.
Nov 07, 2013 9:57:13 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 07, 2013 9:57:13 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2013 9:57:13 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2465 ms
Nov 07, 2013 9:57:13 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 07, 2013 9:57:13 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.29
Nov 07, 2013 9:57:14 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [133] milliseconds.
Nov 07, 2013 9:57:14 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 07, 2013 9:57:14 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 07, 2013 9:57:14 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1329 ms

提前致谢。

4

1 回答 1

1

首先仔细检查您是否可以以root无密码身份登录数据库。然后试一试:

然后试试这个:

Connection conn = null;

try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tool", "root", "");
            System.out.println("Connected!");

        } catch (Exception e) {

            System.out.println("not connected");


        } finally {
            if (conn != null) {
                conn.close();
            }

        }
于 2013-11-07T15:08:39.510 回答