当前位置:   article > 正文

java中PreparedStatement类的setString用法

setstring


文档中是这样写的void setString(int parameterIndex,String x)

conn = DriverManager.getConnection(url, userName, password);//自己链接数据库

PreparedStatement pstmtInsert = conn.prepareStatement("INSERT INTO student VALUES(?, ?,

?, ?)"); // 创建语句,里面的参数等可以用问号代替pstmtInsert.setString(1,"a");//给第一个

问号赋值"a";pstmtInsert.setString(2,"b");//给第二个问号赋值"b";...................

  1. public synchronized void insert(String sql, LinkedHashMap<String, String> data, String startFileName) {
  2. Connection conM = getConnection();
  3. Map<String, Object> map = null;
  4. PreparedStatement pst = null;
  5. try {
  6. pst = conM.prepareStatement(sql);
  7. Iterator<String> kits = data.keySet().iterator();
  8. int fi = 0;
  9. while (kits.hasNext()) {
  10. fi++;
  11. String f = kits.next();
  12. pst.setString(fi, data.get(f));
  13. }
  14. LogsUtil.log(startFileName + "_sql.sql", "" + pst.toString().substring(pst.toString().indexOf(":") + 1) + ";", "utf8");
  15. // pst.execute();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. } finally {
  19. try {
  20. if (pst != null) {
  21. pst.close();
  22. pst = null;
  23. }
  24. } catch (Exception e2) {
  25. e2.printStackTrace();
  26. }
  27. }
  28. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/141620
推荐阅读
相关标签
  

闽ICP备14008679号