AJAX实现JSP连接数据库(Oracle)异步验证

1、  Oracle 数据库创建用户表,添加数据。

 

2、创建util类,连接数据库,关闭数据库。

public class DBConnection {
        private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        private static String user = "scott";
        private static String password = "tiger";
         static{
                  try{
                           //加载驱动类
                           Class.forName("oracle.jdbc.driver.OracleDriver");
                  }catch (ClassNotFoundException e) {
                           e.printStackTrace();
                  }
         }
        public static Connection getConn(){
                  //获取连接对象
                  Connection con = null;
                  try{
                           con= DriverManager.getConnection(url, user, password);
                  }catch (SQLException e) {
                           e.printStackTrace();
                  }
                                    return con;
         }
                 public static void close(Connectionconn,PreparedStatement ps, ResultSet rs){
                  if(rs!= null){
                           try{
                                    rs.close();
                           }catch (SQLException e) {
                                    e.printStackTrace();
                           }
                  }
                  if(ps!= null){
                           try{
                                    ps.close();
                           }catch (SQLException e) {
                                    e.printStackTrace();
                           }
                  }
                  if(conn!= null){
                           try{
                                    conn.close();
                           }catch (SQLException e) {
                                    e.printStackTrace();
                           }
                  }
         }
}

3、创建Dao层类。
public classUserDao {
    String sql = null;
    Connection connection= null;
    PreparedStatement ps = null;
    ResultSet rs = null;
   
    public boolean queryUsername(Stringname){
        sql =" select * from users where username =?";
        boolean a = false;
        try {
            connection = DBConnection.getConn();
           ps= connection.prepareStatement(sql);
           ps.setString(1,name);          
           rs = ps.executeQuery();
           while(rs.next()){
           a = true;
           }
          } catch (SQLException e) {
           e.printStackTrace();
        }
        DBConnection.close(connection, ps, rs);
        return a;
    }
}

4、创建services类。

public class UserService {
    public boolean checkName(Stringname){
        return new UserDao().queryUsername(name);
    }
}

5、创建servlet类

public class CheckNameServlet2 extendsHttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                           throwsServletException, IOException {
                  request.setCharacterEncoding("utf-8");
                  String name = request.getParameter("uname");
                  String message = null;
                  boolean a = new UserService().checkName(name);
                  if(a== true){
                           message="用户已存在,请重新输入。";
                  }else{
                           message="用户可用";
                  }
                  response.setContentType("text/html");
                  response.setCharacterEncoding("utf-8");
                  PrintWriterout = response.getWriter();
                  out.println(message);
                  out.flush();
                  out.close();
         }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                           throwsServletException, IOException {
                  this.doGet(request,response);
         }
}

6、创建JSP页面

 

<%@ page language="java"import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 
   
   My JSP'index.jsp' starting page
    
    
       
    
    
    
    
 
 
 
用户名:
密 码:

Q  Q:


7、效果图:

AJAX实现JSP连接数据库(Oracle)异步验证_第1张图片

AJAX实现JSP连接数据库(Oracle)异步验证_第2张图片


你可能感兴趣的:(AJAX实现JSP连接数据库(Oracle)异步验证)