我正在尝试从使用pgAdmin创建的数据库中提取数据。我已经在pgAdmin中使用了查询,但是它正常工作,但是当我将此查询放入代码中时,会出现错误。
这是跟踪表的SQL:
-- Table: public.tracks
-- DROP TABLE public.tracks;
CREATE TABLE public.tracks
(
trackid integer NOT NULL,
trackname text COLLATE pg_catalog."default" NOT NULL,
album text COLLATE pg_catalog."default" NOT NULL,
mainartist text COLLATE pg_catalog."default" NOT NULL,
feat text COLLATE pg_catalog."default",
releasedate text COLLATE pg_catalog."default" NOT NULL,
filelink text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT tracks_pkey PRIMARY KEY (trackid, trackname),
CONSTRAINT track UNIQUE (trackname)
,
CONSTRAINT album FOREIGN KEY (album)
REFERENCES public.albums (albumname) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT artist FOREIGN KEY (mainartist)
REFERENCES public.artists (stagename) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.tracks
OWNER to postgres;
这是我尝试使用的代码,其中包括查询:
String SQL = "SELECT trackName, albumName FROM public.tracks,
public.albums WHERE tracks.trackname = 'Jungle' AND tracks.mainartist =
albums.artist;";
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);
if (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");
}
else
{
console.println("ERROR: There were no matching rows");
}
我收到以下错误消息:
org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
StackTrace是:
org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
at org.postgresql.jdbc.PgResultSet.checkColumnIndex(PgResultSet.java:2755)
at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2775)
at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:1894)
at Info.songInfo(Info.java:48)
at __SHELL10.run(__SHELL10.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)