我是新手我试图在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
提前致谢。
答案 0 :(得分: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();
}
}