java - Fetch ID from seperate table in SQL -


i have 2 tables, 1 called member:

 name  |   personalnumber  |               id  |   ludde  |       9409251171  |                1  | 

and 1 called boats:

       type  |           length  |         memberid  |   motorsailer  |            100.0  |             null  |  

i want fetch id member , store in memberid in boats.

right try getting id specific member doing this:

public void createboat(boat boat, member member) {     try {         connection.createstatement().execute("insert boat (type, length) values " + boat.tostring());         connection.createstatement().executequery("select id member name " + "'" + member.getname() + "'" );     } catch(sqlexception e) {         system.out.println(e.getmessage());     } } 

however, i'm getting null instead of real data.

first select particular id member table using select statement. use member id in insert statement of boats table.

you can use following. may need small modification. dont know length of boat using 100 length. i'm assuming member id integer

public void createboat(boat boat, member member) {     try {          resultset rs=connection.createstatement().executequery("select id member name =" + "'" + member.getname() + "'");         while(rs.next()){             int memberid=rs.getint("id");             connection.createstatement().execute("insert boat (type, length,memberid) values (" + boat.tostring() ",100,"+memberid+" )");         }     } catch (sqlexception e) {         system.out.println(e.getmessage());     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -