jsp连接mysql异常,求大神指点

蒲公草 2016-09-07 10:23:41
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.text.*" %>
<%@ page import="javax.sql.*,javax.naming.*,webbook.util.*" %>
<%@ page import="java.sql.*,com.mysql.jdbc.Driver" %>
<% String context=request.getContextPath(); %>
<html>
<head>
<title>guestbook</title>
</head>
<body>
String driverName="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:8080/test?useUnicode=true&characterEncoding=UTF-8";
String username = "root";
String password = "xiaopu123";
String sql="insert into guestbook(id,name,email,phone,title,content,time) values (gb_seq.nextval,?,?,?,?,?,?)";
int result=0;
Connection conn=null;
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String title = request.getParameter("title");
try{

Class.forName(driverName);
conn=DriverManager.getConnection(url,username,password);
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, StringUtil.filterHtml(name));
pstmt.setString(2, request.getParameter(request.getParameter("email")));
pstmt.setString(3, request.getParameter(request.getParameter("phone")));
pstmt.setString(4, StringUtil.filterHtml(title));
pstmt.setString(5, request.getParameter("content"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
pstmt.setString(6, sdf.format(new java.util.Date()));
result = pstmt.executeUpdate();
pstmt.close();
}catch (ClassNotFoundException e){
e.printStackTrace();
out.println("!");
}catch(SQLException e){
e.printStackTrace();
out.println("无法连接数据库,请检查数据库连接是否正确!");
}
if(result==0){
out.println("对不起,添加留言不成功,请您重新输入!<br>");
out.println("<a href=\""+context+"/addMessage.jsp\">添加新的留言内容</a><br>");
}else{
out.println("祝贺您,成功添加留言。<br>");
out.println("<a href=\""+context+"/getMessages.jsp\">查看所有留言内容</a><br>");
}

%>
<br>
<p align="center">
Copyright © 2016 <a href="http://www.baidu.com">baidu.com</a>
</body>
</html>



错误信息:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

PS:mysql-connector-java.jar包已经放在lib目录下,classpath也配置好了,求大神指点啊啊啊啊啊啊啊

...全文
293 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Intboy 2016-09-08
  • 打赏
  • 举报
回复
确定这个IP+端口吗?
wangzhuoyan 2016-09-08
  • 打赏
  • 举报
回复
1.连接是否错误 2.mysql好像有8小时连接保护,是否运行一段时间才出这个。
tangwwip 2016-09-08
  • 打赏
  • 举报
回复
看错误信息好像是连接超时了 还有你为什么要把连接数据库的代码写在jsp里,这样很不安全啊 而且在jsp写java代码是很不好的行为
codingjav 2016-09-08
  • 打赏
  • 举报
回复
端口号吧,不过你把java代码写在这里面感觉很别扭,尽量分离吧
我系仔仔 2016-09-08
  • 打赏
  • 举报
回复
127.0.0.1等于localhost,MySQL的本地端号是3306。
yuliuzhen 2016-09-07
  • 打赏
  • 举报
回复
jdbc:mysql://localhost:3306/test 端口错了
蒲公草 2016-09-07
  • 打赏
  • 举报
回复
端口号写错了
小灯光环 2016-09-07
  • 打赏
  • 举报
回复
先用普通的java项目测试一下,把jar包add buildpath,直接在main方法里测试看看报错不~
我怀疑是你的url写错了
wula0010 2016-09-07
  • 打赏
  • 举报
回复

try {
    Class.forName(driverClassName).newInstance();
    conn=DriverManager.getConnection(url);
    pstmt = conn.prepareStatement(sql);
    Iterator inputParamsIte = params.iterator();
    int index = 1;
    while (inputParamsIte.hasNext()) {
        pstmt.setObject(index, inputParamsIte.next());
        index++;
    }
} catch (SQLException se) {
    throw new Exception(this.getClass().getName() + ":_executeQuery(final String sql, final List params):pstmt.setObject(index, inputParamsIte.next())失败。object:" + inputParamsIte.toString() + "。SQLException错误信息:" + se.getMessage());
}

try {
    rs = pstmt.executeQuery();
    ResultSetMetaData rsmd = rs.getMetaData();
    while (rs.next()) {
    }
}catch (SQLException se) {
    ......
}
anakin_feng 2016-09-07
  • 打赏
  • 举报
回复
String url = "jdbc:mysql://127.0.0.1:8080/test?useUnicode=true&characterEncoding=UTF-8";你确定是8080?不是3306
家里敷泥呀 2016-09-07
  • 打赏
  • 举报
回复
Mysql Communications link failure 问题的解决 - netcorner - 博客园 http://www.cnblogs.com/netcorner/p/4346181.html 希望对你有帮助。
今天晴 2016-09-07
  • 打赏
  • 举报
回复
小伙子你为什么还要在jsp页面写这么多java代码
书生_iit 2016-09-07
  • 打赏
  • 举报
回复
这样把数据库密码写在jsp上很不安全吧.

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧