Java Mysql Prepared Statement Return Generated Keys
Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement. Some prepared statements return multiple results; the execute method handles these complex statements as well as the simpler form of statements handled by the methods executeQuery and executeUpdate. If generated keys are requested on a table that has no auto increment column, the JDBC driver will return a null result set. When you insert rows by executeUpdate or execute an INSERT statement or an INSERT within SELECT statement, you need to indicate that you will want to retrieve automatically generated key values. You do that by setting a. The driver will ignore the flag if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements is vendor-specific). Note: This method cannot be called on a PreparedStatement or CallableStatement.
- Mysql Prepared Statement Parameters
- Java Mysql Prepared Statement Return Generated Keys 2017
- Java Mysql Prepared Statement Return Generated Keys Pdf
The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized query.
Let's see the example of parameterized query:
As you can see, we are passing parameter (?) for the values. Its value will be set by calling the setter methods of PreparedStatement.
Why use PreparedStatement?
Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.
How to get the instance of PreparedStatement?
The prepareStatement() method of Connection interface is used to return the object of PreparedStatement. Syntax:
Methods of PreparedStatement interface
The important methods of PreparedStatement interface are given below:
Method | Description |
---|---|
public void setInt(int paramIndex, int value) | sets the integer value to the given parameter index. |
public void setString(int paramIndex, String value) | sets the String value to the given parameter index. |
public void setFloat(int paramIndex, float value) | sets the float value to the given parameter index. |
public void setDouble(int paramIndex, double value) | sets the double value to the given parameter index. |
public int executeUpdate() | executes the query. It is used for create, drop, insert, update, delete etc. |
public ResultSet executeQuery() | executes the select query. It returns an instance of ResultSet. |
Example of PreparedStatement interface that inserts the record
First of all create table as given below:
Now insert records in this table by the code given below:
Example of PreparedStatement interface that updates the record
Example of PreparedStatement interface that deletes the record
Example of PreparedStatement interface that retrieve the records of a table
Example of PreparedStatement to insert records until user press n
Date: November 04, 2004 11:39AM
[java] at com.mysql.jdbc.Statement.getGeneratedKeys(Statement.java:308)
--- details below ---
MySql version: 4.0.21-nt
MySql JConnector Driver version: 3.0.15-ga
table:
create table dp_objtype
(
objtypeid int not null auto_increment,
objname varchar(64) not null,
objclass varchar(255) null,
PRIMARY KEY (objtypeid)
) type = innoDB;
statement:
PreparedStatement pstmt = conn.preparedStatement('insert into dp_objtype (objname, objclass) values (?,?)', Statement.RETURN_GENERATED_KEYS);
If I use pstmt.execute(), the insert-stmt executed without any problem. If I use pstmt.getGeneratedKeys(), I get the following Exception:
[java] psqlstmt = insert into dp_objtype (objname, objclass) values (?,?)
[java] set val[1] = user
[java] set val[2] = com.datapipes.db.User
[java] java.lang.NullPointerException
[java] at com.mysql.jdbc.Statement.getGeneratedKeys(Statement.java:308)
[java] at com.mysql.jdbc.PreparedStatement.getGeneratedKeys(PreparedSta
tement.java:538)
[java] at com.ace.db.DbHelper.insertObject(DbHelper.java:351)
If someone can tell me how to correct this, or if there is a fix for it somewhere.
thanks
-alex
Mysql Prepared Statement Parameters
Java Mysql Prepared Statement Return Generated Keys 2017
Java Mysql Prepared Statement Return Generated Keys Pdf
Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.