Pergunta

So at the moment the following code works and inserts in to mysql from my java application:

String sql = "INSERT INTO location(address1, address2, postcode, town)";
sql+= "VALUES(:address1, :address2, :postcode, :town)";

Map parameters = new HashMap();
parameters.put("address1", location.getAddress1());
parameters.put("address2", location.getAddress2());
parameters.put("postcode", location.getPostCode());
parameters.put("town", location.getTown());

this.namedParameterJdbcTemplate.update(sql, parameters);

However, I'm not sure how to add the "type" column in location table. This column/attribute relates to another table called location_type. so if i was to insert it via mysql code I do

INSERT INTO location(address1, address2, postcode, town, type)
VALUES("2","abc street","ab1 1cd", "nice town", (SELECT id FROM location_type WHERE type="main")  ) 

How would I go about doing that from a java application?

Foi útil?

Solução

You would do it the same way in a Java application:

String sql = "INSERT INTO location(address1, address2, postcode, town, type)";
             + " VALUES (:address1, :address2, :postcode, :town,"
                           + " (SELECT id FROM location_type WHERE type='main'))";

Map parameters = new HashMap();
parameters.put("address1", address1);
parameters.put("address2", address2);
parameters.put("postcode", postcode);
parameters.put("town", town);

this.namedParameterJdbcTemplate.update(sql, parameters);

There's no reason the database accepts a SQL query from the command line and rejects it when it comes from a Java application.

If the hard-coded main above is in fact another parameter, you would set it the same way as all the other parameters:

String sql = "INSERT INTO location(address1, address2, postcode, town, type)";
             + " VALUES (:address1, :address2, :postcode, :town,"
                           + " (SELECT id FROM location_type WHERE type= : type))";

Map parameters = new HashMap();
parameters.put("address1", address1);
parameters.put("address2", address2);
parameters.put("postcode", postcode);
parameters.put("town", town);
parameters.put("type", type);

this.namedParameterJdbcTemplate.update(sql, parameters);

Outras dicas

Hi you can retrieve fk separately, Try this: First through one query fetch type from location_type table and then try inserting what you got in your main insert query like this :

String sqlForType = "SELECT id FROM location_type WHERE type =?";
String type = null;
 try {
            dbConnection = getDBConnection();
            PreparedStatement preparedStatement = dbConnection.prepareStatement(sqlForType);
            preparedStatement.setString(1, "main");
            // execute select SQL stetement
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {type= rs.getString("type");}

And then follow your working code and add type also to your Insert query and HashMap as well which will come from above query result:

// your previous working code
//and add this also in your HashMap
parameters.put("type", type);

this.namedParameterJdbcTemplate.update(sql, parameters);

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top