• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
Java中sql语句的引号问题

1..sql语句

在数据库中,当我们查询语句时,会使用类似的语句:

1 Select * from userinfo where userid='1' or 1;
2 
3 Select * from userinfo where username="jfl";

2.问题

Java通常需要连接数据库(Mysql,Oracle等)进行操作,在查询语句块中经常会用到where子句,在这里我们需要注意引号问题。

3.实例

在java中,我们需要将where子句的关键词变成变量,例如userid,username之类的。显然,直接写成以下语句不符合要求:

 1 public static HashMap<String, String> selectDataGetRecordMap(String id) throws SQLException {
 2 
 3      String sql = "SELECT * FROM userinfo WHERE customerid =id or 'id' ";
 4             HashMap<String, String> map = GenericDaoOper.queryGetRecordMap(sql);
 5             return map;
 6         }
 7 
 8 Scanner sc=new Scanner(System.in);        
 9         System.out.println("请输入所需查询的id:");
10         String id=sc.nextLine();
11         System.out.println( inser.selectDataGetRecordMap(id));

解决方法:

1 id=>' "+id+" '
2 
3 Select * from userinfo where userid=' "+id+" ';

注:单引号 双引号 +variable+双引号 单引号

posted on 2016-05-30 19:06  火磷  阅读(2162)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3