Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Am trying to connect jsp to mysql and it is not working , if i connect from php to mysql its working good , is am connecting from jsp then am getting error
SQLException caught: Communications link failure Last packet sent to the server was 0 ms ago.


What I have tried:

<%@ page import="java.sql.*"%>
<html>
<head>
<title>JDBC Connection example</title>
</head>

<body>
<h1>JDBC Connection example</h1>

<%
  String db = request.getParameter("dbname");
  String user = "username"; // assumes database name is the same as username
  try {
    java.sql.Connection con;
    Class.forName("org.gjt.mm.mysql.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost/"+db, user, "password");
    out.println (db+ "database successfully opened.");
  }
  catch(SQLException ex) {
    out.println("SQLException caught: " +ex.getMessage());
  }
%>

</body>
</html>
Posted
Updated 21-Dec-21 22:18pm
Comments
Richard MacCutchan 21-Dec-21 4:07am    
Some part of the address does not map to the server, but there is not enough information to guess what the system is failing on. Check the physical location of the database; check that you can connect directly to the server outside of JSP; check that the JSP default port number is the same as the one that the database is listening on.
Abhijith moody 22-Dec-21 4:17am    
i checked with php to mysql connection and it was working and now i found a solution thank you :)

1 solution

%@page
   import="java.sql.*"
   import="javax.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Connection Test</title>
    </head>
    <body>

        <!--<a href="index1.jsp">Local Host</a> | -->
        <h1 style='color:green'>Connection Test</h1>
        <%
            //===========Database Connection Starts===========//
            String driver = "com.mysql.jdbc.Driver";
            String user     = "<username>";
            String password = "<password>";
            String dbname   = "<dbname>";
            String url      = "jdbc:mysql://0.0.0.0/"+dbname;
        //    String url      = "jdbc:mysql://localhost:3306/"+dbname;
            //===========Database Connection Ends===========//            



            //===========Don't Touch===========//  
try
            {
                Class.forName(driver).newInstance();
                Connection con = DriverManager.getConnection(url,user,password);
                if(con!=null)
                {
                    out.print("<h3 style='color:green'>Database connected successfully.</h3>");
                    out.print("<hr>");
                    String sql ="SELECT * FROM admin_details s";
                    PreparedStatement pst = con.prepareStatement(sql);
                    ResultSet rs = pst.executeQuery();
                    out.print("<table border='1'>");
                    out.print("<thead><tr><th>Script Code</th><th>Script Name</th></tr></thead>");
                    out.print("<tbody>");
                    while(rs.next())
                    {
                        out.print("<tr><td>"+rs.getInt(1)+"</td><td>"+rs.getString(2)+"</td></tr>");
                    }
                    out.print("</tbody>");
                    out.print("</table>");
                }else{
                    out.print("<h3 style='color:yellow'>Unable to connect to database.</h3>");
                }
            }catch(Exception e){
               out.print("<h3 style='color:red'>Unable to connect to database.</h3>");
                out.print("<h3 style='color:red'>My ERROR : "+e+"</h3>");
                System.out.println("Unable to connect : "+e);
            }
        %>
    </body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900