簡體   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