Sybase jdbc4没有从sql查询返回任何结果

时间:2016-10-31 18:24:04

标签: java sybase

我正在尝试对Sybase数据库运行查询。似乎连接成功,但当它到达rs.next()时,它无法转到结果的第一行。它跳转到rs.Close()。

我知道以下查询有效。我在sql工具中运行它并返回一条记录。

请有人能告诉我我做错了吗?

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

import java.sql.*;
import com.sybase.jdbc4.jdbc.SybDataSource;
import com.sybase.jdbc4.jdbc.SybDriver;


public class test { 

    public static void main(String[] args) throws SQLException {
       SybDriver sybDriver = null;
       Connection conn = null;
       Statement stmt = null;
       String host = "123.12.34.56";
       String url = "jdbc:sybase:Tds:"+host+":1300";
       String username = "testuser";
       String password ="password";

       try{
           sybDriver=(SybDriver)Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
           conn = DriverManager.getConnection(url, username, password);
           stmt = conn.createStatement();
           ResultSet rs = stmt.executeQuery("select top 1 brand, model,color,serialnum from myTable order by newid()");

           while(rs.next())
           {
              String one = rs.getString(1);
              String two = rs.getString(2);
              String three = rs.getString(3);
              String four = rs.getString(4);
           }

        rs.close();
        stmt.close();
        conn.close();
    } catch(SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {

    }
}

0 个答案:

没有答案