gpt4 book ai didi

mysql - 无法使用 Servlet 连接到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 13:28:17 26 4
gpt4 key购买 nike

我是新手,我正在尝试在 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

提前致谢。

最佳答案

首先,请仔细检查您是否可以以 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();
}

}

关于mysql - 无法使用 Servlet 连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19839025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com