【发布时间】:2019-08-28 04:17:56
【问题描述】:
我的更新命令抛出异常
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","Durga");
st = con.createStatement();
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Bonus Amount : ");
int bonus_amt = Integer.parseInt(br.readLine());
System.out.println("salary range:");
float sal_range = Float.parseFloat(br.readLine());
int rowCount = st.executeUpdate("UPDATE employee SET esal = esal "+bonus_amt+ " where esal <" +sal_range);
【问题讨论】:
-
将 sql 命令保存在一个变量 (
String sql = "UPDATE employee ....") 中并在执行之前打印它以查看它的样子。还指示使用PreparedStatement而不是将值直接注入到命令中,请参阅Exploit of a Mom(和Little Bobby Tables) -
不要将值连接到 SQL 字符串中。了解如何正确使用 PreparedStatement:mkyong.com/jdbc/jdbc-preparestatement-example-update-a-record
标签: java sql database oracle jdbc