0

I want to get the PK for a record if the value provided exist. If not i want to insert the supplied value and get the auto incremented primary key. What i did is:

 String sql= "SELECT id FROM markets where market = 'value';";
 ResultSet rs=executeQuery(sql);
 int id;
 if (!rs.isBeforeFirst() ) {    
      //there is No record found. must be created
         rs.close();
         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         st = conn.createStatement();
         String query = "INSERT INTO markets (market) VALUES('value')";
         st.executeUpdate(query);
//       created now we want the ID of the new market
         rs=executeQuery(sql);
         id =rs.getInt("id");
         System.out.println("ID"+id);
          rs.close();
     } 
     else
       while (rs.next()){
          //the value exist, we get the ID of it
          id =rs.getInt("id");
          System.out.println(".."+id);
       }

and method executeQuery is defined:

public static ResultSet executeQuery(String query){
        Connection conn = null;
        Statement stmt = null;      
          try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
            stmt = conn.createStatement();
            String sql;         
            return  stmt.executeQuery(query);           
          } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
  1. Is this a correct aproach?
  2. if it is, why do i get a java.sql.SQLException: Before start of result set on

    //  created now we want the ID of the new market
    rs=executeQuery(sql);
    id =rs.getInt("id");//<<<<-----Exception Here
    System.out.println("ID"+id);
    
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Skaros Ilias
  • 1,008
  • 12
  • 40
  • Voted to close as duplicate because the solution of your problem: *`id =rs.getInt("id");//<<<<-----Exception Here`*, boils down to use `ResultSet#next` before using `ResultSet#getXyz()`. – Luiggi Mendoza May 07 '15 at 20:38
  • Thanks, i always use loops and forgot i need to use next. But the 1st question still stands, is this the proper way of doing? – Skaros Ilias May 07 '15 at 20:44
  • The best way would be to use the same connection, there's no need to obtain a new connection. And yes, you should use a single `Statement`/`PreparedStatement` per sql statement. – Luiggi Mendoza May 07 '15 at 20:45
  • I just thought it would be cleaner if i had the connection commands on a separate method and not inside the 'IF' command. and i should close all connections before executing somthing else? Is that what you are saying? – Skaros Ilias May 07 '15 at 20:49
  • 1
    You may pass a `Connection` parameter to your method `executeQuery` along with the query. In fact, I recommend using a method like this: `ResultSet executeQuery(Connection con, String sql, Object ... params)` as described [here](http://stackoverflow.com/a/17596547/1065197) (the example is for `executeUpdate` but it can be refined). – Luiggi Mendoza May 07 '15 at 20:51
  • @LuiggiMendoza thanks for that, but still, is this the proper way to check and insert data that might already exist? – Skaros Ilias May 07 '15 at 21:22
  • There's no other option. So yes, that's the approach (not right, not great, is just the solution). – Luiggi Mendoza May 07 '15 at 21:28

0 Answers0