qq_37032132 2017-06-27 08:38 采纳率: 80%
浏览 1239
已采纳

Java编程连接数据库Communications link failure,有代码求帮忙啊

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TeachSearchInfo extends JPanel implements ActionListener {
private JLabel []jlArray={new JLabel("学号"),new JLabel("姓名"),
new JLabel("性别"),new JLabel("出生日期"),new JLabel("年"),
new JLabel("月"),new JLabel("日"),new JLabel("籍贯"),
new JLabel("学院"),new JLabel("专业"),new JLabel("班级"),
new JLabel("入学时间"),new JLabel("年"),
new JLabel("月"),new JLabel("日")};
private JLabel jl=new JLabel("请输入要查询学生的学号");
private JTextField jtf=new JTextField();
private JButton jb=new JButton("查询");
//创建存放信息的jl数组
private JLabel []jlArray2=new JLabel[13];

private String host;


public TeachSearchInfo(String host){
    this.host=host;
    initialFrame();
    addActionListener();
}

public void initialFrame(){
    this.setLayout(null);
    jl.setBounds(30, 20, 150, 30);
    this.add(jl);
    jtf.setBounds(200, 20, 100, 30);
    this.add(jtf);
    jb.setBounds(310, 20, 30, 30);
    this.add(jb);

}

public void addActionListener(){
    jb.addActionListener(this);
}



@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(e.getSource()==jb){
        String stu_id=jtf.getText();
        if(stu_id.equals("")){
            JOptionPane.showMessageDialog(this, "请输入学号", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }else{
            String []baseinfo=getStuBaseInfo(stu_id,host);
            for(int i=0;i<13;i++){
                System.out.println(baseinfo[i]);
            }

        }
    }


}
public String[] getStuBaseInfo(String stu_id,String host) {
    // TODO Auto-generated method stub
    String []message=new String[13];
    Connection conn;
    Statement stmt;
    ResultSet rs;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://"+host+"/test");
        stmt=conn.createStatement();
        String sql="select stu_id,stu_name,stu_gender,stu_birth,nativeplace,"+
                "coll_name,dept_name,class_name,cometime from student,dept,college,"+
                "class where stu_id='"+stu_id+"' and student.coll_id=college.coll_id"+
                " and student.dept_id=dept.dept_id and student.class_id=class.class_id";
        rs=stmt.executeQuery(sql);
        if(rs.next()){
            message[0]=rs.getString(1);
            message[1]=new String(rs.getString(2).getBytes("ISO-8859-1"));
            message[2]=new String(rs.getString(3).getBytes("ISO-8859-1"));
            Date stu_birth=rs.getDate(4);
            message[3]=stu_birth.getYear()+1900+"";
            message[4]=stu_birth.getMonth()+1+"";
            message[5]=stu_birth.getDate()+"";
            message[6]=new String(rs.getString(5).getBytes("ISO-8859-1"));
            message[7]=new String(rs.getString(6).getBytes("ISO-8859-1"));
            message[8]=new String(rs.getString(7).getBytes("ISO-8859-1"));
            message[9]=new String(rs.getString(8).getBytes("ISO-8859-1"));
            Date cometime=rs.getDate(9);
            message[10]=cometime.getYear()+1900+"";
            message[11]=cometime.getMonth()+1+"";
            message[12]=cometime.getDate()+"";
        }
        if(rs!=null)
        {
            rs.close();
        }
        if(stmt!=null)
        {
            stmt.close();
        }
        if(conn!=null)
        {
            conn.close();
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return message;

}



//cs
public static void main(String[] args){
    TeachSearchInfo ts=new TeachSearchInfo("127.0.0.1:3036");
    JFrame jf=new JFrame();
    jf.add(ts);
    jf.setBounds(10,10,700,650);
    jf.setVisible(true);

}

}

报错信息如下:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at TeachSearchInfo.getStuBaseInfo(TeachSearchInfo.java:83)
at TeachSearchInfo.actionPerformed(TeachSearchInfo.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4583)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4413)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.(Socket.java:366)
at java.net.Socket.(Socket.java:209)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:280)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
... 38 more
null
null
null
null
null
null
null
null
null
null
null
null
null

  • 写回答

4条回答 默认 最新

  • 南宫文凯 2017-06-27 09:13
    关注

    那按你所述和数据库链接没半毛钱关系,用Bedug打一下断点,看看运行时哪里开始报的错

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。