简体   繁体   中英

Mysql JDBC Exception: Unable to create or access database connection in JSP

<%@page import="java.util.List"%>
<%@page import="com.istudentlabs.dao.CollegeNamesDao"%>
<%@page import="com.istudentlabs.model.CollegeNames"%>
<%@page import="com.istudentlabs.util.DBConnection"%>
<%@ page import="java.sql.*"%>
<div>
<table>
<%  
String name=request.getParameter("val");  

    CollegeNames categories=null;
try{  
    Class.forName("com.mysql.jdbc.Driver");
    Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","mysql");
    PreparedStatement ps = c.prepareStatement("select id from college_names where id=1");
    ResultSet rs = ps.executeQuery();

    while (rs.next()) {
        CollegeNames collegeNames = new CollegeNames();
        collegeNames.setId(rs.getInt("id"));
        collegeNames.setName(rs.getString("college_name"));
        collegeNames.setCity(rs.getString("district"));
        out.print("<tr>"+collegeNames.getName()+"</tr>");
        //categories.add(collegeNames);

    }
}
catch(Exception e){out.print(e);}  


%>
</table>
</div>

When I try to initiate Database from JSP, I get the following exception: exception com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

However, when I initiate database from a Java class, it works properly. Also, when I try to get connection from the Java class and use it in a JSP file, I get the same previously mentioned exception ?

Any input would be appreciated !

If you are using mysql-connector-java compatibility issue. Try to upgrade it and see if that solves the issue ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM