Java 通过JoinRowSet连接数据时收到错误
我尝试使用JoinRowSet通过Java进行连接,但没有成功,我收到一个错误:Java 通过JoinRowSet连接数据时收到错误,java,sql-server,jdbc,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
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();
}
}
包连接行集;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入javax.sql.rowset.CachedRowSet;
导入javax.sql.rowset.RowSetProvider;
公共类JoinRowSet{
void databaseCheck()引发SQLException,ClassNotFoundException{
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
String url=“jdbc:sqlserver://localhost:1433;databaseName=Movies;integratedSecurity=true”;
连接c=DriverManager.getConnection(url);
语句s=c.createStatement();
结果集rs=s.executeQuery(“从tblFilm中选择电影名称”);
CachedRowSet director=RowSetProvider.newFactory().createCachedRowSet();
CachedRowSet film=RowSetProvider.newFactory().createCachedRowSet();
填充(s.executeQuery(“从tblDirector中选择DirectorName”);
film.populate(s.executeQuery(“从tblFilm中选择FilmName”);
javax.sql.rowset.JoinRowSet jrs=RowSetProvider.newFactory().createJoinRowSet();
jrs.addRowSet(董事);
jrs.addRowSet(电影);
while(jrs.next()){
对于(int i=1;iJoinRowSet想要知道如何连接数据。您需要设置一个匹配列。
在查询从tblDirector选择DirectorName
和从tblFilm选择FilmName
中,没有可用作匹配列的列。
我想每部电影都必须有一位导演,也许,在你的表格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);
别忘了,这只是一个基于我对您的数据结构的假设的示例。两列(directorName和FilmName)都位于表的第一个位置。错误已消失……但我认为我没有正确地迭代JoinRowSet……在完成您所说的操作后,输出中没有结果:(.这是您所说的修改后的代码。我从中获取此示例的书可能已经过时。任何示例如何正确地迭代连接行集?(我假设可能是错误的迭代)。迭代与我关于此主题的初始问题相同。