使用bean属性生成preparedstatement

时间:2014-03-04 06:53:11

标签: java eclipse netbeans prepared-statement

我有一个名为Customer的bean,有40个属性。我使用java prepare语句将bean数据插入数据库,所以每次我必须写setString()或setInt()时,任何人都可以建议我如何最小化这个?表示Netbeans或Eclipse IDE中是否有任何快捷方式用于生成带有bean getter属性的预准备语句

int x = 0;
Connection con = null;
String query = "INSERT INTO Customers (CustomerName, ContactName, Address, City,  PostalCode, Country) VALUES (?,?,?,?,?,?); 
PreparedStatement ps = con.prepareStatement(query);
ps.setString(++x, person.custName());
 .
 .
 .
 .
 .
 40 times 

1 个答案:

答案 0 :(得分:0)

bean-query的帮助下,您可以将bean(代码中的person对象)转换为包含键和值的映射,然后迭代该映射的每个条目,并调用{{1} }