【问题标题】:Inserting value into table in java在java中将值插入表中
【发布时间】:2013-01-25 07:08:41
【问题描述】:

我是 java 新手,正在尝试学习将用户名和密码以及个人资料图像路径插入 Mysql 数据库。当我运行以下代码时,它将插入到表中,但表'table_profile'第3列的路径字段如下

如果路径等于"C:/Users/Manohar/Documents/FileUplaodDemo/build/web/uploads/x.jpg"

然后它被插入为路径等于"C:UsersManoharDocumentsFileUplaodDemo uildwebuploads

stmt = connection.prepareStatement(
    "insert into table_profile values('"+userId+"','"+userName+"','"+path+"')");                                

【问题讨论】:

标签: java jdbc


【解决方案1】:

用户参数绑定如下:

stmt = connection.prepareStatement("insert into table_profile values(?, ?, ?)");
stmt.setInt(1, userId);
stmt.setString(2, userName);
stmt.setString(3, path);

让 Java 为您完成所有艰苦的工作:)

【讨论】:

  • 另外,它还可以防止 SQL 注入攻击。
猜你喜欢
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
相关资源
最近更新 更多