2 wuran beijing wuran_beijing 于 2016.01.26 20:17 提问

jdbc连接mysql出错,求解释

最近在学习mvc,写了一个简单的demo,其中连接MySQL数据库的Java类如下:
但是老报错:
package myjava;
import java.sql.*;

public class myjavabean {
Connection conn;
Statement stmt;
ResultSet rs1;
ResultSet rs2;
public myjavabean(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3308/mvc_user?useUnicode=true&characterEncoding=UTF-8","root","123456");
stmt=conn.createStatement();

}
catch(Exception e)
{

e.printStackTrace();
}
}
public String login(String name, String password){
String message="abc";
try{
String sql1="select count(0) from user_info where user_name='"+name+"'";
rs1=stmt.executeQuery(sql1);
if(rs1.next()){
int userCount=rs1.getInt(1);
if(0==userCount){
message="不存在该用户";
return message;
}
String sql2="select count(0) from user_info where user_name='"+name+"'and password='"+password+"'";
rs2=stmt.executeQuery(sql2);
if(rs2.next()){
int trueUserCount=rs2.getInt(1);
if(0==trueUserCount){
message="密码错误";
return message;
}

          message="成功!";
         }           
        }
       }catch(Exception e)
       {
        e.printStackTrace();
       }finally{
           try{
               if(rs1!=null){
                   rs1.close();
                   System.out.println("rs1 is not null");
               }
               if(rs1==null){
                   System.out.println("rs1 is null");
               }
               if(rs2!=null){
                   rs2.close();
               }
               if(stmt!=null){
                   stmt.close();
               }
               if(conn!=null){
                   conn.close();
               }
           }catch(SQLException e){
               System.out.println(e.toString());
           }
       }
       return message;         
      }

}
eclipse控制台报如下错误:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:276)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2592)
at com.mysql.jdbc.Connection.(Connection.java:1509)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at myjava.myjavabean.(myjavabean.java:10)
at myjava.myservlet.doGet(myservlet.java:42)
at myjava.myservlet.doPost(myservlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

** END NESTED EXCEPTION **

Last packet sent to the server was 9 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2658)
at com.mysql.jdbc.Connection.(Connection.java:1509)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at myjava.myjavabean.(myjavabean.java:10)
at myjava.myservlet.doGet(myservlet.java:42)
at myjava.myservlet.doPost(myservlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at myjava.myjavabean.login(myjavabean.java:22)
at myjava.myservlet.doGet(myservlet.java:43)
at myjava.myservlet.doPost(myservlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

3个回答

oyljerry
oyljerry   Ds   Rxr 2016.01.26 20:32

连接被拒绝了 数据库端口等能否访问

wuran_beijing
wuran_beijing 1.junit测试我的链接数据库方法,没问题,然后发现是页面上的form表单提交方法我写的是post,submit提交写为大写了“Submit”
接近 2 年之前 回复
wuran_beijing
wuran_beijing 问题我找到了,解决思路如下:
接近 2 年之前 回复
wuran_beijing
wuran_beijing 你好,端口可以访问的,
接近 2 年之前 回复
zzlu_suran
zzlu_suran   2016.01.26 20:58

看看MySQL服务打开了吗

zzlu_suran
zzlu_suran 回复wuran_beijing: at myjava.myjavabean.(myjavabean.java:10) 是包的事吗
接近 2 年之前 回复
wuran_beijing
wuran_beijing 问题我找到了:解决思路如下
接近 2 年之前 回复
zzlu_suran
zzlu_suran 回复wuran_beijing: 好吧,不是转义字符的事
接近 2 年之前 回复
wuran_beijing
wuran_beijing 问题我找到了:解决思路如下
接近 2 年之前 回复
zzlu_suran
zzlu_suran 回复wuran_beijing: jdbc:mysql://localhost:3306/XXXX?useUnicode=true&characterEncoding=utf-8 酱呢
接近 2 年之前 回复
wuran_beijing
wuran_beijing 服务也是打开的,是不是我代码问题?
接近 2 年之前 回复
bobyang888
bobyang888   2016.01.27 04:14

conn=DriverManager.getConnection("jdbc:mysql://localhost/mydb")

bobyang888
bobyang888 conn=DriverManager.getConnection("jdbc:mysql://localhost/表名","root",密码)
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!