jdbc连接sybase时出错

package  grs_dev1;

import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.sql.Statement;

public   class  ConnectTest  {
    Connection con;
    Statement sta;
    ResultSet rs;
    
public void add() throws SQLException{
        
try {
            Class.forName(
"com.sybase.jdbc2.jdbc.SybDriver");
            con
=DriverManager.getConnection("jdbc:sybase:Tds:localhost:5000/grs_dev1","sa","");
            sta
=con.createStatement();
            rs
=sta.executeQuery("select * from hts_audit_status");
            
while(rs.next()){
                System.out.println(rs.getInt(
1));
            }

            rs.close();
            sta.close();
            con.close();
        }
 catch (Exception e) {
            e.printStackTrace();
            con.close();
        }

        
    }

    
public static void main(String[]args){
        ConnectTest data
=new ConnectTest();
        
try {
            data.add();
        }
 catch (SQLException e) ...
    }

}

java.sql.SQLException: JZ006: 捕获到 IO 例外:java.net.ConnectException: Connection refused: connect
 at com.sybase.jdbc2.jdbc.ErrorMessage.raiseError(ErrorMessage.java:556)
 at com.sybase.jdbc2.jdbc.ErrorMessage.raiseErrorCheckDead(ErrorMessage.java:860)
 at com.sybase.jdbc2.tds.Tds.handleIOE(Tds.java:3971)
 at com.sybase.jdbc2.tds.Tds.handleIOE(Tds.java:3916)
 at com.sybase.jdbc2.tds.Tds.login(Tds.java:438)
 at com.sybase.jdbc2.jdbc.SybConnection.tryLogin(SybConnection.java:254)
 at com.sybase.jdbc2.jdbc.SybConnection.regularConnect(SybConnection.java:230)
 at com.sybase.jdbc2.jdbc.SybConnection.(SybConnection.java:200)
 at com.sybase.jdbc2.jdbc.SybConnection.(SybConnection.java:134)
 at com.sybase.jdbc2.jdbc.SybDriver.connect(SybDriver.java:179)
 at java.sql.DriverManager.getConnection(DriverManager.java:525)
 at java.sql.DriverManager.getConnection(DriverManager.java:171)
 at grs_dev1.ConnectTest.add(ConnectTest.java:17)
 at grs_dev1.ConnectTest.main(ConnectTest.java:37)
Exception in thread "main" java.lang.NullPointerException
 at grs_dev1.ConnectTest.add(ConnectTest.java:30)
 at grs_dev1.ConnectTest.main(ConnectTest.java:37)

错误是由于url引起的,把“jdbc:sybase:Tds:localhost:5000/grs_dev1”中的localhost替换成自己的ip就ok了,(最好也不要用127.0.0.1)。

你可能感兴趣的:(jdbc连接sybase时出错)