Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9096557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:53:01+00:00 2026-06-16T23:53:01+00:00

I have two tables in my MySQL database: CREATE TABLE table1 ( id int

  • 0

I have two tables in my MySQL database:

CREATE TABLE table1 (
  id int auto_increment,
  name varchar(10),
  CONSTRAINT pk_id primary key(id)
) 

and

CREATE TABLE table2 (
  id_fk int,
  stuff varchar(30),
  CONSTRAINT fk_id FOREIGN KEY(id_fk) REFERENCES table1(id) 
) 

I want to insert a record in both of these tables. Basically, I’ve id, name & stuff as data. How do I insert them into both tables using Spring JDBC?

I’m inserting into tables as shown below:

    SimpleJdbcInsert insert1 = new SimpleJdbcInsert(this.getDataSource())
        .withTableName("table1")
        .usingColumns("name");

    Map<String, Object> parameters1 = new HashMap<String, Object>();
    parameters1.put("name", myObj1.getStuff());
    insert.execute(parameters1);

While inserting into table2, how do I get the id value from table1?

    SimpleJdbcInsert insert2 = new SimpleJdbcInsert(this.getDataSource())
        .withTableName("table2")
        .usingColumns("stuff");

    Map<String, Object> parameters2 = new HashMap<String, Object>();
    parameters2.put("stuff", myObj2.getStuff());
    insert.execute(parameters2);

Also, how do I maintain transaction?

Also, how do I fetch data for a given name?

Any help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:53:02+00:00Added an answer on June 16, 2026 at 11:53 pm

    See this simple example, all methods in class Test will be transactional, read Spring Framework docs for more

    @Transactional
    public class Test {
        @Autowired
        DataSource ds;
    
        public void test1() throws Exception {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("c1", "test");
            SimpleJdbcInsert insert = new SimpleJdbcInsert(ds).withTableName("t1").usingColumns("c1")
                    .usingGeneratedKeyColumns("id");
            long id = insert.executeAndReturnKey(params).longValue();
    
        params = new HashMap<String, Object>();
        params.put("stuff", "stuff");
        params.put("id_fk", id);
        SimpleJdbcInsert insert2 = new SimpleJdbcInsert(ds).withTableName(
                "table2").usingColumns("stuff", "id_fk");
        insert2.execute(params);
    
            NamedParameterJdbcTemplate tmpl = new NamedParameterJdbcTemplate(ds);
            params = new HashMap<String, Object>();
            params.put("id", id);
            String c1 = tmpl.queryForObject("select c1 from t1 where id = :id", params, String.class);
        }
    

    context

    <context:annotation-config />
    <tx:annotation-driven />
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test?user=root&amp;password=root" />
    </bean>
    
    <bean class="Test" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's the setup. I have two tables: CREATE TABLE dbo.TmpFeesToRules1(Name varchar, LookupId int)
I have two tables in a Mysql database. PACKAGES: ID (auto_increment) CATEGORY (INT) NAME
I have the following two tables in my mysql database. Table Name: groups id
I have two tables in MySQL sales database: Orders table: CREATE TABLE salestest.`orders` (
I have in my MySQL database these two tables: CREATE TABLE IF NOT EXISTS
In a MySQL database I have two tables linked in a join. One table
What I have is two tables inside of a mysql database. One table contains
I have to create a database with two tables in MySQL, but the script
I have an h2 database with two tables with foreign keys like CREATE TABLE
I'm using a MySQL database. I have two tables, Table 1 and Table 2.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.