JDBC 向数据库插入数据 遇到个怪问题 求大神回答

代码如下
package insertCar;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class InsertEsCar {
public static void main(String[] args) throws SQLException {

     Connection conn = null;

     try {
        Class.forName("com.mysql.jdbc.Driver");
        conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/eauto100?useUnicode\\=true&characterEncoding\\=utf8&autoReconnect\\=true","root","");
        Statement stmt = conn.createStatement();
        String sql = "select * from es_car";
        ResultSet rs = stmt.executeQuery(sql);
        List<carInfo> list = new ArrayList<carInfo>();
        while(rs.next()){
            carInfo car = new carInfo();
            car.setCarId(rs.getInt("car_id"));
            car.setCarLineId(rs.getInt("car_line_id"));
            car.setCarName(rs.getString("car_name"));
            car.setMakeYear(rs.getString("make_year"));
            car.setAirCC(rs.getString("air_cc"));

            list.add(car);

        }

        String[] year = {"2016","2017"};

        for (carInfo c : list) {
            if(c.getCarId() !=4226 && c.getCarId() !=4526){

            String[] name = c.getCarName().split(" ");
                for (String i : year) {
                    name[1] = i + "款";
                    String carname = "";
                    for (String str : name) {
                        carname += str + " ";
                    }
                    c.setCarName(carname);
                    sql = "insert into es_car (car_id,car_line_id, car_name, make_year, air_cc) values(null,"+c.getCarLineId()+",'"+c.getCarName()+"',"+i+", '"+c.getAirCC()+"')";
                    int a =stmt.executeUpdate(sql);
                   // System.out.println(a);
                   // conn.setAutoCommit(false);
                   // conn.commit();

                }

                }
            }



     }
    catch (Exception e) {
       e.printStackTrace();
    }finally{
        if(conn!=null){
            conn.close();
        }
    }
 }

}

class carInfo{
private Integer carId;
private Integer carLineId;
private String carName;
private String makeYear;
private String airCC;

public Integer getCarId() {
    return carId;
}

public void setCarId(Integer carId) {
    this.carId = carId;
}

public Integer getCarLineId() {
    return carLineId;
}

public void setCarLineId(Integer carLineId) {
    this.carLineId = carLineId;
}

public String getCarName() {
    return carName;
}

public void setCarName(String carName) {
    this.carName = carName;
}

public String getMakeYear() {
    return makeYear;
}

public void setMakeYear(String makeYear) {
    this.makeYear = makeYear;
}

public String getAirCC() {
    return airCC;
}

public void setAirCC(String airCC) {
    this.airCC = airCC;
}

}
程序可以正常执行,打印出的sql语句放到数据库中也可以正常插入,a 的返回值为1,可是到数据库中查询表格里就是没有数据,救命!!!!!!!!!!!!!!!!!

3个回答

可能是事务的问题把,,,,

我确定不是事务的问题 事务是自动提交的

刷新一下数据表,其次把car_id这个属性的赋值去掉试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问