while((str=br.readLine())!=null){ String[] array=str.split(",");
  
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,count);
ps.setTime(2,departure_time);
ps.setString(3,departure_airport);
ps.setTime(4,arrive_time);
ps.setString(5,arrive_airport);
ps.setString(6,flight_company);
ps.setString(7,flight_code);
ps.setString(8,aircraft_type);
ps.setInt(9,airport_count);
ps.setInt(10,fuel_count);
ps.setString(11,rebate);
ps.setString(12,fare);
ps.setString(13,space);
如何把br赋值给departure_time等这些字段啊
BufferedReader br=new BufferedReader(new FileReader("D:/Workspace/fileio/北京.重庆.2010-7-30.txt"));

解决方案 »

  1.   

    说明不太清楚,如果从文本文件中取出字符串,并按照指定分割符进行分割,可以获得
    所以你要设置的数据(数据的类型全是String),之后需要按照你的SQL的setXXX中使用的
    类型进行转换。把转换后值设定到指定的语句中即可了。
      

  2.   

    现在是这些字段都获取不到值啊
    Column 'departure_time' cannot be null
      

  3.   

    你完全错误理解PreparedStatement类的用法了 
    String spl=insert into table values(?,?,?,?,?,?,?,?,?,?,?,?,?);
    ps.setInt(1,count);//从文件中读取的第一个值覆盖掉count (比如 ps.setInt(1,10)),这就给第一个问号赋值
    ps.setTime(2,departure_time);//从文件中读取第二值覆盖掉departure_time(比如ps.setTime(2,2010-8-10))
    ps.setString(3,departure_airport);
    ps.setTime(4,arrive_time);
    ps.setString(5,arrive_airport);
    ps.setString(6,flight_company);
    ps.setString(7,flight_code);
    ps.setString(8,aircraft_type);
    ps.setInt(9,airport_count);
    ps.setInt(10,fuel_count);
    ps.setString(11,rebate);
    ps.setString(12,fare);
    ps.setString(13,space);给这13条语句用同样方法赋值一次就完成了第一条出具的传输
    这是我自己程序中的一段 希望你看了有帮助
    PreparedStatement pstmt ; 
    conn = this.getJdbcTemplate().getDataSource().getConnection();
    pstmt =(PreparedStatement)conn.prepareStatement("insert into data_xls values"+"("+b+")");//取得连接 
                for(int i=0;i<row_data.length-1;i++)//插入数据,遇到null的用空格代替
                {
                 String[]temp=row_data[i].split(",");
                 for(int j=1;j<=n;j++)
                 {
                 System.out.print(temp[j-1] + "  ");
                 if(!temp[j-1].equals("null"))pstmt.setString(j, temp[j-1]);
                 if(temp[j-1].equals("null"))pstmt.setString(j, " ");
                 }
                 System.out.println();
                 count++;
                 pstmt.addBatch();    //将一组参数添加到此 PreparedStatement 对象的批处理命令中
                 if(0==count%1000)//读取1000次后用执行一次
                 {
                 pstmt.executeBatch();   
                 conn.commit();//使自从上一次提交/回滚以来进行的所有更改成为持久更改,并释放此 Connection 对象当前保存的所有数据库锁定。
                        System.out.println("插入数据成功");
                 }
                }
                pstmt.executeBatch();//处理循环里未处理的数据
                conn.commit();
                System.out.println("插入数据成功");
                pstmt.close();//关闭对象           
    }catch(Throwable e){
    System.out.println("插入数据失败");
    e.printStackTrace();
    }finally{
    if(conn != null) try{
    conn.close();
    }catch(SQLException e){
    e.printStackTrace();
    }
    }
      

  4.   

    简单的说就是
     PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
                                         SET SALARY = ? WHERE ID = ?");
       pstmt.setBigDecimal(1, 153833.00)
       pstmt.setInt(2, 110592)
      

  5.   

    ps.setTime(2,departure_time);但是一读到这行就有错误啊
    Column 'departure_time' cannot be null   这是错误
      

  6.   

    下面是我的全部代码:public static void main(String[] args) throws SQLException, IOException {
    int count = 0;
    // TODO Auto-generated method stub
    try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","0");
    BufferedReader br=new BufferedReader(new FileReader("D:/Workspace/fileio/北京.重庆.2010-7-30.txt"));
    //ArrayList<String> list = new ArrayList<String>();
    String str=null;

    String sql="insert into piaojia(id,departure_time,departure_airport,arrive_time,arrive_airport,flight_company,flight_code,aircraft_type,airport_count,fuel_count,rebate,fare,space) value(?,?,?,?,?,?,?,?,?,?,?,?,?)";
    while((str=br.readLine())!=null){
    //departure_time = br.;
        String[] array=str.split(",");
      
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1,count);
        ps.setTime(2,departure_time);
        ps.setString(3,departure_airport);
        ps.setTime(4,arrive_time);
        ps.setString(5,arrive_airport);
        ps.setString(6,flight_company);
        ps.setString(7,flight_code);
        ps.setString(8,aircraft_type);
        ps.setInt(9,airport_count);
        ps.setInt(10,fuel_count);
        ps.setString(11,rebate);
        ps.setString(12,fare);
        ps.setString(13,space);
        ps.execute();
        ps.close(); }
    conn.close();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

  7.   

    这个地方有点问题:Column 'departure_time' cannot be null
      

  8.   


    Date date = new Date(array[1]);
      

  9.   

    从的源代码中没有看到给字段赋值啊,你的count有个初始值0所以不会报错
    你已经把文件的中的数据取出来存入数组了,首先你应该把数组中的数据和SQL中的字段对应关系搞清楚,然后给对应的字段赋值,因为都是string,所以还要转换成数据库中对应字段所需要的类型
      

  10.   

    PreparedStatement ps=conn.prepareStatement(sql);
    ps.setInt(1,Integer.parseInt(array[0]));
    ps.setTime(2,Time.valueOf(array[1]));
    ps.setString(3,array[2]);
    ps.setTime(4,Time.valueOf(array[3]));
    ps.setString(5,array[4]);
    ps.setString(6,array[5]);
    ps.setString(7,array[6]);
    ps.setString(8,array[7]);
    ps.setInt(9,Integer.parseInt(array[8]));
    ps.setInt(10,Integer.parseInt(array[9]));
    ps.setString(11,array[10]);
    ps.setString(12,array[11]);
    ps.setString(13,array[12]);
    ps.execute();
    ps.close();
      

  11.   

    恩,我弄出来了  呵呵
    ps.setString(1,array[0]);都改成这让就可以了
    谢谢大家了
      

  12.   

    我是这样写的                            ps.setString(1,array[0]);
        ps.setString(2,array[1]);
        ps.setString(3,array[2]);
        ps.setString(4,array[3]);
        ps.setString(5,array[4]);
        ps.setString(6,array[5]);
        ps.setString(7,array[6]);
        ps.setString(8,array[7]);
        ps.setString(9,array[8]);
        ps.setString(10,array[9]);
        ps.setString(11,array[10]);
        ps.setString(12,array[11]);
        
        ps.execute();
        ps.close();