Expand|Select|Wrap|Line Numbers
- package com.java2novice.jdbc;
- *
- import java.sql.*;
- *
- public class MyAutoGeneratedKeys {
- *
- public static void main(String a[]){
- ********
- Connection con = null;
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- con = DriverManager.getConnection("jdbc:odbc:student");
- String query = "insert into Student(fname,lname,gender) values (?,?,?)";
- *********
- pstmt = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
- pstmt.setString(1, "John");
- pstmt.setString(2, "methev");
- pstmt.setInt(3,"Male");
- ***********
- pstmt.executeUpdate();
- ************
- rs = pstmt.getGeneratedKeys();
- ************
- while(rs.next()){
- System.out.println("Generated Emp Id: "+rs.getObject(1).toString());
- }
- *
- } catch (SQLException e) {
- **********
- } finally{****}
- ****
- *
- }
- }