繁体   English   中英

Mysql JDBC异常:无法在JSP中创建或访问数据库连接

[英]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>

当我尝试从JSP启动数据库时,出现以下异常:异常com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

但是,当我从Java类启动数据库时,它可以正常工作。 另外,当我尝试从Java类获取连接并在JSP文件中使用它时,会遇到前面提到的相同异常?

任何输入将不胜感激!

如果您使用的是mysql-connector-java兼容性问题。 尝试升级它,看看是否能解决问题?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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