通过JoinRowSet加入数据时收到错误

时间:2015-09-30 19:21:56

标签: java sql-server jdbc

我尝试使用JoinRowSet通过Java加入,但没有成功,我收到错误:

Exception in thread "main" java.sql.SQLException: Match Column not set for join
at com.sun.rowset.JoinRowSetImpl.addRowSet(JoinRowSetImpl.java:219)
at joinrowset.JoinRowSet.databaseCheck(JoinRowSet.java:33)
at joinrowset.JoinRowSet.main(JoinRowSet.java:49)

这个问题的解决方案是什么?我无法在互联网上找到任何东西,所以我被困住了。如果你有一个解决方案,它会很棒。感谢

package joinrowset;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;



public class JoinRowSet {


    void databaseCheck() throws SQLException, ClassNotFoundException {


        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url="jdbc:sqlserver://localhost:1433;databaseName=Movies;integratedSecurity=true";
        Connection c = DriverManager.getConnection(url);
        Statement s = c.createStatement();

        ResultSet rs = s.executeQuery("select FilmName from tblFilm");

        CachedRowSet director = RowSetProvider.newFactory().createCachedRowSet();
        CachedRowSet film = RowSetProvider.newFactory().createCachedRowSet();

        director.populate(s.executeQuery("select DirectorName from tblDirector"));
        film.populate(s.executeQuery("select FilmName from tblFilm"));

        javax.sql.rowset.JoinRowSet jrs = RowSetProvider.newFactory().createJoinRowSet();
        jrs.addRowSet(director);
        jrs.addRowSet(film);

        while(jrs.next()) {
            for(int i=1; i<=jrs.getMetaData().getColumnCount(); i++) {

                System.out.println(jrs.getString(i) + "\t");
            }
        }

    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException {

        JoinRowSet instantiate = new JoinRowSet();
        instantiate.databaseCheck();


    }
}

1 个答案:

答案 0 :(得分:0)

JoinRowSet想知道如何加入你的数据。您需要设置匹配列。 在查询select DirectorName from tblDirectorselect FilmName from tblFilm中,没有可用作匹配列的列。 我想每部电影都必须有一个导演,可能在你的表tblDirector中你可以找到导演的参考资料。在这种情况下,您可以将代码重写为

director.populate(s.executeQuery("select DirectorName from tblDirector"));
film.populate(s.executeQuery("select FilmName, DirectorName from tblFilm"));
javax.sql.rowset.JoinRowSet jrs = RowSetProvider.newFactory().createJoinRowSet();

director.setMatchColumn(1);   //position of the column "DirectorName" 
film.setMatchColumn(2);       //position of the column "DirectorName" 

jrs.addRowSet(director);
jrs.addRowSet(film);

不要忘记,它只是基于我对您的数据结构的假设的一个例子。