andysern 2016-05-24 05:46 采纳率: 6.3%
浏览 1361

mysql 里executeQuery在for循环里执行失效怎么解决?

问题就是,之前是有时候可以上传图片,并被数据库记录,但会经常图片上传成功,数据库记录却没有。

在javabean的数据库里是这样创建conn 和 st 及rs的:
public ft_con(){
if(conn==null){
try{
//Class.forName("com.microsoft.sqlserver.jdbc"); 基本不使用。
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //看是否引入数据包成功
Class.forName("com.mysql.jdbc.Driver");
}

    catch(Exception ex){

        System.out.println(ex);

    }
    try{
        conn = DriverManager.getConnection(url,user,password);     //创建连接

        }
    catch(Exception ex){
        System.out.println("connection create false");                  //连接不成功则输出
        System.out.println(ex);     

        }   
    }

}

public Connection getConn(){

        return this.conn;

    }

public void Set_st(){
    try{
        if(st!=null)
    st = conn.createStatement();
    }catch(Exception ex){
        System.out.println("无法创建statement");
        System.out.println(ex);
    }
}

public ResultSet getRs(String sql){
    try{
        rs = st.executeQuery(sql);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    return rs;
}

public int updata(String sql){
    int num=0;
        try{
            num = st.executeUpdate(sql);
            }catch(SQLException e){
            e.printStackTrace();
            }
        return num;
} 

public void close(){
    try{
        if(rs!=null){
            rs.close();
            rs = null;
            }
        if(st!=null){
            st.close();
            st = null;
            }
        if(conn!=null){
        conn.close();
        conn = null;
            }           
        }catch(SQLException e){
        e.printStackTrace();
            }
}

说明一下,这个在其他页面都运行良好。

但是在jsp页面里(之前还是运行良好,但是有时候会有数据传不进去)

for (int i=0;i<su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
// 若文件不存在则继续
if (file.isMissing()) continue;

String img_upload = getIpName.getIPTimeRand()+"."+file.getFileExt(); //img_upload需要传给数据库以备储存

updata_img_zhuce.Set_st();
String sql = "insert into ft_img (img_name,upload_date,upload_who,tel,bank_name,bank_card) values('"+img_upload+"',now(),'"+name+"','"+tel+"','"+bank+"','"+bank_card+"')";
out.print(updata_img_zhuce.updata(sql));

file.saveAs("/upload/ft/"+img_upload,su.SAVE_VIRTUAL); //为上传的图片取一个唯一的名字

——————————————————————

这里问题就来了,我的图片已经上传到服务器了,也就是file.saveAs是被正确执行的。但是在数据库里却没有图片上传的条目,也就是update(sql)没有执行成功,print executeQuery出来是“00”。请教一下,到底是哪里出问题了呢?

我也怀疑过是否Statement创建多了,把Statement移除了for循环,但还是不行。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有赏,i卡绘世画不出
    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码