【发布时间】:2019-04-13 11:56:05
【问题描述】:
我正在尝试从使用 postgresql 创建的数据库中提取有关歌曲的数据。我一直收到这个错误,我不知道该怎么做,因为我调用了 resultSet.next()。 有什么建议吗?
String SQL = "SELECT trackName, albumName FROM tracks, artists,
albums WHERE 'song' = tracks.trackname AND tracks.mainartist =
artists.artistname AND albums.artist = artists.artistname";
try
{
org.postgresql.Driver.isRegistered();
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(url,username,password); //creates a new connection
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SQL);
resultSet.next();
String album = resultSet.getString(3);
String artist = resultSet.getString(4);
String trackName = resultSet.getString(2);
String feat = resultSet.getString(5);
String date = resultSet.getString(6);
console.println("");
console.println("");
console.println("Track Name: "+trackName);
console.println("");
console.println("Track Album: "+album);
console.println("");
console.println("Track Artist(s): "+artist);
console.println("");
console.println("Featuring: "+feat);
console.println("");
console.println("Track Release Date: "+date);
console.println("");
console.println("\n");
}
catch(Exception e)
{
console.println("SORRY, COULD NOT CONNECT YOU TO THE DATABASE.");
console.println(e);
console.println("");
e.printStackTrace();
}
我正在终端中打印出来:
SORRY, COULD NOT CONNECT YOU TO THE DATABASE.
org.postgresql.util.PSQLException: ResultSet not positioned properly,
perhaps you need to call next.
这是堆栈跟踪:
org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:1894)
at Info.songInfo(Info.java:49)
at __SHELL14.run(__SHELL14.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at bluej.runtime.ExecServer$3.run(ExecServer.java:752)
【问题讨论】:
标签: java sql postgresql