加入SQL命令未正确结束

时间:2014-11-07 18:10:36

标签: java javascript sql command spring-jdbc

我遇到连接声明问题

 String q1="select e.employee_id,e.manager_id,e.first_name,e.last_name,e.salary,e.commission_pct,d.manager_id,d.employee_id from employees as e ,employees as d where  e.manager_id=d.employee_id and e.employee_id="+jComboBox1.getSelectedItem();
    try{
        OracleDataSource ods=new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:hr/hr@localhost:1521/XE");
        Connection con=ods.getConnection();

        Statement s=con.createStatement();
        s.execute(q1);
        ResultSet rs=s.getResultSet();
        String x=(String)jComboBox1.getSelectedItem();
        while(rs.next()){
            if (x.equals(rs.getString("e.employee_id"))){
                jTextField1.setText(rs.getString("e.first_name"));
                jTextField2.setText(rs.getString("e.last_name"));
                jTextField3.setText(rs.getString("e.salary"));
                jTextField4.setText(rs.getString("e.commission_pct"));
                jTextField5.setText(rs.getString("d.first_name"));
            }
        }
        con.close();

    }catch(Exception e){e.printStackTrace();}

我怎么能解决这个问题?! .................................................. .................................................. .................................................. .................................................. ............................

1 个答案:

答案 0 :(得分:0)

您应该使用预准备语句来避免SQL注入问题。无论如何,你的问题是:

select e.employee_id,e.manager_id,e.first_name,e.last_name,e.salary,e.commission_pct,d.manager_id,d.employee_id 
from employees as e ,employees as d 
where  e.manager_id=d.employee_id 
and e.employee_id="+jComboBox1.getSelectedItem();

我看到你使用rs.getString作为employee_id,所以你缺少where子句中e.employee_id字段的引号。