我如何将一个String变量插入到java中的Sql语句中

时间:2015-03-15 17:20:12

标签: java sql string combobox

如何将值从comboBox传递给sql语句?

 String name = comboBox.getSelectedItem().toString(); 
    String sql = "SELECT surname FROM employee WHERE etairia = name ";
    result = statement.executeQuery(sql);

1 个答案:

答案 0 :(得分:2)

试试这个:

String name = comboBox.getSelectedItem().toString(); 
String sql = "SELECT surname FROM employee WHERE etairia = (?)";
statement = conn.prepareStatement(sql); // conn is Connection
statement.setString(1, name); // '1' is the param index, it's always start from 1, not from zero
result = statement.executeQuery();