Java准备的语句无法更新数据库

时间:2019-02-12 19:59:48

标签: java sql database prepared-statement

我正在使用准备好的语句将一些变量存储到数据库中。该程序可以正常运行,但是数据库不会更新。

 public void setData(Dealer sDealer)
 {
     String fName = sDealer.getFirstName();
     String lName = sDealer.getLastName();
     int age = sDealer.getAge();
     int xp = sDealer.getExperience();
     String mStatus = sDealer.getMartialStatus();
     String dAdd = sDealer.getAddress();
     String pNum = sDealer.getPhoneNumber();
     String email = sDealer.getEmailAddress();
     String crime = sDealer.getCriminalRecord();
     String type = sDealer.getCategory();
         String SQL ="INSERT INTO DEALERS  ("
                 +"firstName, " +"lastName ," +"dAge, "
                 +"dXp, " +"maritalStatus , " +"dAddress, "
                 +"phoneNumber, " +"dMail, " +"criminalRecord, " +"dType )"
                 +"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

     try{

我怀疑我在此方法中使用了以下准备好的语句,但是我无法弄清楚自己在做什么错。

         PreparedStatement pStat = dConnect.prepareStatement(SQL);

         pStat.setString(1, fName);
         pStat.setString(2, lName);
         pStat.setInt(3, age);
         pStat.setInt(4, xp);
         pStat.setString(5, mStatus);
         pStat.setString(6, dAdd);
         pStat.setString(7, pNum);
         pStat.setString(8, email);
         pStat.setString(9, crime);
         pStat.setString(10, type);

     }catch(Exception sx){
         System.out.println("Error is found :"+sx);
     }
 }

2 个答案:

答案 0 :(得分:0)

您只需要通过添加一个额外的调用来执行该语句:

> python3
Python 3.7.2 (default, Feb 12 2019, 08:15:36) 
[.Clang 10.0.0 (clang-1000.11.45.5)] on darwin

答案 1 :(得分:0)

您需要执行该语句并通过在try-catch块中添加行来提交这些数据库更改:

pStat.executeUpdate();
dConnect.commit();