SQL JDBC JAVA: getGeneratedKeys() after IF NOT EXISTS ( SELECT ...) INSERT
INTO (...)
I am now searching for a while not finding a solution for my problem. I
would like to insert a new row into a table if it currently not exists. I
need the auto-incremented key of that new entry or the existing key of the
existing entry.
Also I'm not clear about the idea of the return-value of my INSERT, what
if one column is inserted, value = 1, and what if the row exists? value =
0 or NULL or ?!
If possible I do NOT want to "BATCH-INSERT", I must use these two
return-values for the next INSERT as foreign keys... or has anyone an idea
how to do this all with batch inserts?
Thanks to everybody who can help!!
System: MS SQL 2012 standard database, JAVA SE 1.6, sqljdbc4
This is my code, that gives me a SQLException:
public long executeUpdateActionStatement(String sqlStatement, int
autoGeneratedKeys) {
ResultSet rs = null;
Statement stmt = null;
int rowCount = 0;
try {
connect();
stmt = connect.createStatement();
rowCount = stmt.executeUpdate(sqlStatement, autoGeneratedKeys);
// if no new row is inserted, later I do a select to get the key
if (rowCount < 1)
return -1;
stmt.getGeneratedKeys();
if (rs.next()) {
long id = rs.getLong(1);
System.out.println(" !! ==---> ID: " + id + " | : " +
sqlStatement);
return id;
}
} catch (SQLFeatureNotSupportedException feature) {
feature.printStackTrace();
System.out.println("|| ---->>> Fehler:
SQLFeatureNotSupportedException: " + sqlStatement);
} catch (SQLServerException sqlse) {
sqlse.printStackTrace();
System.out.println("|| ---->>> Fehler: SQLServerException: " +
sqlStatement);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("|| ---->>> Fehler: SQLException: " +
sqlStatement);
}
finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return -1;
}
The SQLException I get:
com.microsoft.sqlserver.jdbc.SQLServerException: Die Anweisung muss
ausgeführt werden, bevor Ergebnisse abgerufen werden können.
at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at
com.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(SQLServerStatement.java:2040)
in english:
The statement must be executed before any results can be obtained.
The SQL Statements:
executeUpdateActionStatement("IF NOT EXISTS (SELECT GeoLocationId FROM
GeoLocationCoordinates WHERE Longitude = " + longitude +
" AND Latitude = " + latitude + ") INSERT INTO GeoLocationCoordinates
VALUES (" + longitude + ", " + latitude + ")",
Statement.RETURN_GENERATED_KEYS); //Statement.RETURN_GENERATED_KEYS);
AND
executeUpdateActionStatement("IF NOT EXISTS (SELECT PlaceId FROM
Places WHERE TwitterPlaceId = \'" + p.getId() + "\') INSERT INTO
Places VALUES (" +
placeData + ")", Statement.RETURN_GENERATED_KEYS);
The database-tables:
/****** Object: Table [dbo].[GeoLocationCoordinates] Script Date:
16.08.2013 17:55:04
**************************************************************************/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[GeoLocationCoordinates](
[GeoLocationId] [bigint] IDENTITY(1,1) NOT NULL,
[Longitude] [float] NOT NULL,
[Latitude] [float] NOT NULL, CONSTRAINT
[PK_GeoLocationCoordinates] PRIMARY KEY CLUSTERED (
[GeoLocationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
and
/****** Object: Table [dbo].[Places] Script Date: 16.08.2013
17:57:16
**************************************************************************/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Places](
[PlaceId] [bigint] IDENTITY(1,1) NOT NULL,
[TwitterPlaceId] [nvarchar](255) NULL,
[PlaceName] [nvarchar](255) NULL,
[PlaceFullName] [nvarchar](255) NULL,
[StreetAdress] [nvarchar](255) NULL,
[Country] [nvarchar](255) NULL,
[PlaceType] [nvarchar](255) NULL,
[PlaceUrl] [nvarchar](255) NULL,
[BoundingBoxType] [nvarchar](255) NULL,
[CountryTwoLetterCode] [nchar](2) NULL,
CONSTRAINT [PK_Places] PRIMARY KEY CLUSTERED
(
[PlaceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
No comments:
Post a Comment