这个方法是根据表名,向表中插入各个字段值的,代码如下    public void updataDB(String strTableName,
                        int strCityID,
                        String strCityName,
                        String strDate_Solar,
                        String strDate_Lunar,
                        String strWeather,
                        String strTemperature,
                        String strWind) {
        this.initConnection();    //初始化数据库连接
        try {
            //声明SQL语句
            Statement stmt = conn.createStatement();
            String sql = "INSERT into '%" + strTableName + "%'" + "(cityID, cityName, date_Solar, date_Lunar, weather, temperature, wind)"
                        + "VALUES( '%" + strCityID + "%','%" +strCityName  + "%','%" 
                                    + strDate_Solar  + "%','%" + strDate_Lunar + "%','%"
                                    + strWeather + "%','%" + strTemperature + ",'%"
                                    + strWind + "%')";
            stmt.execute(sql);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
    }
关键是这其中的sql语句,不知该怎么正确书写,主要是被%'和'%这样的符号给搞乱了,希望大虾帮帮忙,谢谢!

解决方案 »

  1.   

    拼sql?
    1、'%模糊查询条件%';
    1、'模糊查询条件%';
    1、'%模糊查询条件';
      

  2.   

    建议用预处理preparestatment,你这样直接串在速度和安全放慢都要好。
      

  3.   


    方法名是 updateDBSQL语句开始是 INSERT intoSQL语句中是 where 条件中的like用到的%究竟想干嘛?把sql中所有的%去掉吧!
      

  4.   

     String sql = "INSERT into " + strTableName + "(cityID, cityName, date_Solar, date_Lunar, weather, temperature, wind)"
                            + "VALUES(" + strCityID + ",'" +strCityName  + "','" 
                                        + strDate_Solar  + "','" + strDate_Lunar + "','"
                                        + strWeather + "','" + strTemperature + ",'"
                                        + strWind + "')";
      

  5.   

    建议用预处理preparedstatment,你这样写起来既乱又影响查询速度。preparedstatmet较好,我们项目经理让我把工程中你的那种写法全换成preparedstatment了。
      

  6.   

    不知道lz要干什么,不过按照你的写法,有两处错误。
    改正如下:
    第一行 "INSERT into '%" + strTableName + "%' "   后边加了一个空格
    倒数第二行  + strWeather + "%','%" + strTemperature + "'%,'%" 加了个'%
                                                          --这里
      

  7.   


    倒数第二行     + strWeather + "%','%" + strTemperature + "%','%" 
    最后一个,前面 加了个 %'
    ps  9楼建议预处理  不错
      

  8.   

    insert中不需要'%',建议用preparedstatment