I am studying JDBC use on Spring Framework following this tutorial: http://www.tutorialspoint.com/spring/spring_jdbc_example.htm
So I have the following table named Student on my MySql DB:
mysql> describe Student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| NAME | varchar(20) | NO | | NULL | |
| AGE | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
As you can see in the tutorial link I have an interface named StudentDAO that describe the CRUD operation on this Student table of my DAO object
Ok, looking at the StudentJDBCTemplate class: this is an implementation of the StudentDAO interface, so in this class I have all the implementation for the CRUD method declared inside the interface.
Ok…I have some doubt about the JDBC method mean in this class.
For example when I have something like:
public void create(String name, Integer age) {
String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update(SQL, name, age);
System.out.println("Created Record Name = " + name + " Age = " + age);
return;
}
Ok…I think that this method create a new record in my Student table.
The SQL variable contains a String that rappresents my query and here I have the first doubt
When I have:
String SQL = “insert into Student (name, age) values (?, ?)”;
what exactly mean? It means that the two input parameter of my create() take the place of the “name” and “age” int this String?
mmm…this sound strange for me, because this look like a String (infact is whithin “”)
The scond doubt is related to the following line:
jdbcTemplateObject.update(SQL, name, age);
If I have the SQL query inside the SQL String, why have I to pass also the variable name and age?
Can you help me to understand well how JDBC work?
Tnx
Andrea
JDBC will replace the two
?placeholders with the appropriate values. This is seen as a good practice as it helps prevent SQL injection.