jsp+javaBean查询数据库(异常已解决)

  jsp+javaBean查询数据库(异常已解决)

if (rs!=null){//判断RS是否为空        if (!rs.wasNull())也可换成这个判断SQL Null 是空返回true 不是返回false!  这样的话就不用在抛出异常的时候在把值设为空啦null
Bean:
package cn.dakchina;
import java.sql.*;

import cn.dakchina.*;

public class FirstBean {
 private String name,pwd,email,phone,qq,zhuanye,address,sex;
 private Connection conn;
 private ResultSet rs;
 private Statement stmt;
 public FirstBean(String name)throws SQLException{
  this.name=null;
  this.pwd=null;
  this.email=null;
  this.phone=null;
  this.qq=null;
  this.zhuanye=null;
  this.address=null;
  this.sex=null;
  
   try{
    Class.forName("com.mysql.jdbc.Driver");
  
    String url =
      "jdbc:mysql://localhost:3306/mytest";
  
    conn =
      DriverManager.getConnection(
         url,"root", "123456");
  
    stmt = conn.createStatement(
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                      ResultSet.CONCUR_UPDATABLE);
    
    }catch (SQLException e){
     
    System.out.println(e);
    }
    catch(ClassNotFoundException e){
     
    }
    String query=" select * from userinfor where name='"+name+"'" ;
    rs=stmt.executeQuery(query);
    
        
   }
  
 
 //初始化数据
 public void setName(String name){
  this.name=name;
 }
 public void setPwd(String pwd){
  this.pwd=pwd;
 }
 public void setQQ(String qq){
  this.qq=qq;
 }
 public void setPhone(String phone){
  this.phone=phone;
 }
 public void setAddress(String address){
  this.address=address;
 }
 
 public void setEmail(String email){
  this.email=email;
 }
 public void setSex(String sex){
  this.sex=sex;
 }
 public void setZhuanye(String zhuanye){
  this.zhuanye=zhuanye;
 }
  
 //获得数据
 public String getName(){
  return this.name;
 }
 public String getPwd(){
  return this.pwd;
 }
 public String getEmail(){
  return this.email;
 }
 public String getAddress(){
  return this.address;
 }
 public String getSex(){
  return this.sex;
 }
 public String getPhone(){
  return this.phone;
 }
 public String getQQ(){
  return this.qq;
 }
 public String getZhuanye(){
  return this.zhuanye;
 }
 
 
 public void closeConnection(){
  try{
   conn.close();
  }catch(SQLException e){
   e.printStackTrace();
  }
  conn=null;
 }
 //从数据库查询数据

       public void setData(){
     
        if (rs!=null){//判断RS是否为空        if (!rs.wasNull())也可换成这个判断SQL Null 是空返回true 不是返回false!  这样的话就不用在抛出异常的时候在把值设为空啦null
        try{
      rs.next();   //。。。。。。不知道为什么不用这一句就会出错,找不到记录      setName(rs.getString("name"));
   setPwd(rs.getString("pwd"));
   setAddress(rs.getString("userAddress"));
   setEmail(rs.getString("useremail"));
   setPhone(rs.getString("userphone"));
   setQQ(rs.getString("userqq"));
   setSex(rs.getString("usersex"));
   setZhuanye(rs.getString("zhuanye"));
         }catch(Exception e){    //如果扑捉到  Illegal operation on empty result set    则会把name,pwd等实例变量初始化为null,这样一来可以在jsp中判断是否有此用户
      this.name=null; 
       this.pwd=null;
       this.email=null;
       this.phone=null;
       this.qq=null;
       this.zhuanye=null;
       this.address=null;
       this.sex=null;
         }
        }
        
        else
        {
          this.name=null; 
       this.pwd=null;
       this.email=null;
       this.phone=null;
       this.qq=null;
       this.zhuanye=null;
       this.address=null;
       this.sex=null;
     
        }
     }
       public String noUser(){
        
        return "没有此用户";
        
       }
 
 }

JSP部分:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="cn.dakchina.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>信息查询</title>
</head>

<body>

<%
String name=request.getParameter("name");
FirstBean fb=new FirstBean(name);
fb.setData();
if (fb.getName()!=null){//判断是否有此用户,如果没有则调用noUser();
out.println("YourName:");
out.println(name);
out.println("<br>");
out.println("YourPwd:");
out.println(fb.getPwd());
out.println("<br>");
out.println("YourEmail:");
out.println(fb.getEmail());
out.println("<br>");
out.println("YourAddress:");
out.println(fb.getAddress());
out.println("<br>");
out.println("YourQQ:");
out.println(fb.getQQ());
out.println("<br>");
out.println("YourPhone:");
out.println(fb.getPhone());
out.println("<br>");
out.println("YourZhuanye:");
out.println(fb.getZhuanye());
}
else out.println(fb.noUser());

%>
</body>
</html>

你可能感兴趣的:(jsp,数据库,jdbc,null,email,scroll)