日期:2014-05-16  浏览次数:20465 次

mysql数据库 插入“\”问题
mysql数据库在插入 带有“\”字符串的时候 数据库字段会屏蔽“\”例如 D:\wtpwebapps\LibraryManage\1.jpg时候 mysql数据库将屏蔽"\"-->""即变为D:wtpwebappsLibraryManage1.jpg  如果是需要打开该文件的路径,那么可以将D:\wtpwebapps\LibraryManage\1.jpg 转变为D:/wtpwebapps/LibraryManage/1.jpg 改路径同样可以打开该文件  所以在遇到改问题时候,可以在程序中 将"\"转变为"/" 但是同时又涉及到 \为转义字符,\'表示为一个',\"表示为",所以在程序设计的时候注意书书写代码:
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
及可以将改路径插入到数据库中,
测试代码:新建一个字符转换类:public class ChStr {
public static final String changestr(String str){
str=str.replace('\\','/');
return str;
}
}

新建普通类
    private ActionForward bookAdd(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response){

filePath = request.getRealPath("\\") + "myPic\\";//取当前系统路径
filePath = new ChStr().changestr(filePath+ file.getFileName());
int a=bookDAO.insert(bookForm);}


新建dao层:
public class BookDAO {

public int insert(BookForm bookForm) {
sql = "Insert into tb_bookinfo (bfile) values(

' bookForm.getFilename()'
)
}

测试成功:
图:
 


bookForm.setFilename(filePath);
}