没有错误,但数据没有插入表中

时间:2015-12-11 17:51:48

标签: java postgresql

在我的数据库中,我有两个名为car和ad的表。在汽车中,我有一个外键引用广告中的adId值。当我运行我的代码时,我没有收到任何错误,但数据只插入广告,而不是汽车。有什么建议?这是我的代码:

public void createAd(Ad ad, Car car) {
    try {
        Class.forName("org.postgresql.Driver");
        if (con != null) {
            ps = con.prepareStatement("INSERT INTO \"ad\"(\"title\", \"description\", \"author\", \"image\") VALUES (?, ?, ?, ?)");
            ps.setString(1, ad.getTitle());
            ps.setString(2, ad.getDescription());
            ps.setString(3, ad.getAuthor());
            ps.setBytes(4, ad.getImage());
            ps.executeQuery();

            ps = con.prepareStatement("SELECT currval('\"ad_adId_seq\"');");
            rs = ps.executeQuery();
            rs.next();
            int adId = rs.getInt("currval");
            ps = con.prepareStatement("INSERT INTO \"ad\"(\"adId\") VALUES (?);");
            ps.setInt(1, adId);
            ps.executeUpdate();
            ad.setAdId(adId);

            ps = con.prepareStatement("INSERT INTO \"car\"(\"distanceTraveled\", \"age\", \"condition\", \"vin\", \"brand\", \"price\", \"ad_adId\") VALUES (?, ?, ?, ?, ?, ?, ?)");
            ps.setInt(1, car.getDistanceTraveled());
            ps.setInt(2, car.getAge());
            ps.setString(3, car.getCondition());
            ps.setString(4, car.getVIN());
            ps.setString(5, car.getBrand());
            ps.setInt(6, car.getPrice());
            ps.setInt(6, adId);
            ps.executeQuery();
            car.setAdId(adId);
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

2 个答案:

答案 0 :(得分:1)

您上次查询时出错:

ps.setInt(7, adId);

而不是

ps.setInt(6, adId);

答案 1 :(得分:1)

我建议你看一下spring-database。使用JdbcTemplate会减少很多JDBC锅炉板代码,更重要的是会照顾很多jdbc连接泄漏问题。

如果您需要使用普通JDBC,则需要正确初始化连接对象,并在使用update语句时使用update而不是query

您还需要正确关闭已创建的statement个对象。 您应该执行类似于以下代码的操作:

Connection con = null;
PrepareStatement ps1 = null;
PrepareStatement ps2 = null;
PrepareStatement ps3 = null;
PrepareStatement ps4 = null;
ResultSet rs = null;

String url = "jdbc:postgresql://localhost/dbName";
String user = "userName";
String password = "userPass";

try {
  con = DriverManager.getConnection(url, user, password);
  ps1 = con.prepareStatement("INSERT INTO \"ad\"(\"title\", \"description\", \"author\", \"image\") VALUES (?, ?, ?, ?)");
  ps1.setString(1, ad.getTitle());
  ps1.setString(2, ad.getDescription());
  ps1.setString(3, ad.getAuthor());
  ps1.setBytes(4, ad.getImage());
  ps1.executeUpdate();

  ps2 = con.prepareStatement("SELECT currval('\"ad_adId_seq\"');");
  rs = ps2.executeQuery();
  if(rs.next())
   int adId = rs.getInt("currval");
  }
  ps3 = con.prepareStatement("INSERT INTO \"ad\"(\"adId\") VALUES (?);");
  ps3.setInt(1, adId);
  ps3.executeUpdate();
  ad.setAdId(adId);

  ps4 = con.prepareStatement("INSERT INTO \"car\"(\"distanceTraveled\", \"age\", \"condition\", \"vin\", \"brand\", \"price\", \"ad_adId\") VALUES (?, ?, ?, ?, ?, ?, ?)");
  ps4.setInt(1, car.getDistanceTraveled());
  ps4.setInt(2, car.getAge());
  ps4.setString(3, car.getCondition());
  ps4.setString(4, car.getVIN());
  ps4.setString(5, car.getBrand());
  ps4.setInt(6, car.getPrice());
  ps4.setInt(7, adId);
  ps4.executeUpdate();
  car.setAdId(adId);
} catch (SQLException ex) {
   Logger lgr = Logger.getLogger(getClass().getName());
   lgr.log(Level.SEVERE, "Unable to execute updates", ex);
} finally {
   try {
     if (rs != null)
         rs.close();
      if (ps1 != null)
         ps1.close();
      if (ps2 != null) 
         ps2.close();
      if (ps3 != null) 
         ps3.close();
      if (ps4 != null)
         ps4.close();
      if (con != null)
         con.close();
   } catch (SQLException ex) {
       Logger lgr = Logger.getLogger(getClass().getName());
       lgr.log(Level.WARNING, "Unable to close the connection", ex);
   }
}