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

时间:2018-10-10 16:54:46

标签: java mysql jdbc

<%@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文件中使用它时,会遇到前面提到的相同异常?

任何输入将不胜感激!

1 个答案:

答案 0 :(得分:0)

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