mysql查询只返回id_mysql查询数据库中有没有这个id 有的话返回true 没有返回false...

该博客介绍了如何使用Java和MySQL查询数据库中是否存在特定ID。通过PreparedStatement执行SQL并检查ResultSet,如果查询结果为false则表示ID不存在,返回false;反之,如果查询到数据,返回true。
摘要由CSDN通过智能技术生成

其中 if (rs.next()==false) {查不到数据 sql执行false rs.next() 如果第一次返回False表示返回为空 如果执行后第一个结果是ResultSet,则返回true,否则返回false。`

public boolean getAllopenid(int tieziid,String openid6) {//public ArrayList getAll(int id)需要找id查找时

//nrbean nr=new nrbean();

//int i=book6.getId();

Connection con = null;

Statement stmt = null;

PreparedStatement pstmt = null;

ResultSet rs = null;

ArrayList hfnr = new ArrayList();

boolean flag = false;

try{

con=sjk.con6();//con由import coo中con6 封装查询

// Class.forName("com.mysql.jdbc.Driver"); 锟斤拷

//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/qqg?useUnicode=true&characterEncoding=utf8","root","456");

pstmt = con.prepareStatement("select * from hf where tieziid ='"+tieziid+"' and hfopenid='"+openid6+"'"); //在这where上面的id

// select 字段1,字段2... from 表1,表2... [where 条件]

//System.out.print(tieziid);

// System.out.print(arg0);

//pstmt.setInt(1,id);

stmt = con.createStatement();

rs = pstmt.executeQuery();

if (rs.next()==false) {// //查不到数据 sql执行false rs.next() 如果第一次返回False表示返回为空 如果执行后第一个结果是ResultSet,则返回true,否则返回false。

//String openid6=rs.getString("hfopenid");

//if((openid6).equals(openid)){//openid){

flag= false;

System.out.print("meiyou");

}

else{

flag=true;

System.out.print("you");

//}

}}

catch(Exception e){System.out.println(e.getMessage());}

return flag;

// return resultBook;

}

下面是调用上面的dao 后 用boolean值 判断有没有查到消息

Boolean a=s4.getallopenid(id,openid);

HashMap li=new HashMap();//null;

//System.out.print(a);

if(a){

int i=0;

li.put("nr",i);

//int i=0;

//String j=JSONArray.fromObject(i).toString();

//JSONArray json = new JSONArray();

// JSONObject jsob=new JSONObject();

// jsob.put("nr", i);

// json.add(jsob);

//response.setContentType("text/html;charset=UTF-8");

// PrintWriter pt=response.getWriter();

// pt=response.getWriter();

// pt.println(jsob);//打印json

System.out.print("cccccccccccccccccccccccc");

}

// request.getRequestDispatcher("../ok.jsp").forward(request, response);}

else{//int i=1;

int i=6;

li.put("nr", i);

//JSONArray json = new JSONArray();

// JSONObject jsob=new JSONObject();

// jsob.put("nr", i);

// json.add(jsob);

//response.setContentType("text/html;charset=UTF-8");

// PrintWriter pt=response.getWriter();

// pt=response.getWriter();

// pt.println(jsob);//打印json

// System.out.print("666666666666666666666666666");

//String j=JSONArray.fromObject(i).toString();

//response.setContentType("text/html;charset=UTF-8");

// PrintWriter pt=response.getWriter();

// pt=response.getWriter();

// pt.println(jsob);}

// request.getRequestDispatcher("regist.jsp").forward(request, response);}

}

// Listli=new ArrayList();// po=null;

// List aa=new List()

// li=d.getAll(la, lo);

// System.out.print(li);

return li;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值