jsp连接数据的核心:jdbc
jsp连接数据库和Java连接数据库的不同:
导入驱动类:
Java:需要将jar放入项目,build path ——》add to build path
jsp:直接将jar包复制到项目的WebContent——WEB-INF——lib
导包:
Java:import java.sql.*;
jsp:<% page import = "java.sql.*;
Java和jsp分开写:
<%@page import="sjk.LoginDao"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String uname = request.getParameter("uname"); String pwd = request.getParameter("upwd"); LoginDao dao = new LoginDao(); int result = dao.dao(uname, pwd); if(result > 0 ){ out.print("登录成功"); }else if (result ==0){ out.print("登录失败"); }else{ out.print("系统异常"); } %> </body> </html>
package sjk; import java.sql.*; public class LoginDao { public int dao(String uname,String pwd) { String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test"; String user = "sa"; String password = "123456"; Connection connection = null; ResultSet rs = null; Statement stmt = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection(url, user, password); stmt = connection.createStatement(); String sql = "select count(*) from login where upwd='"+pwd+"' and uname ='"+uname+"' "; rs = stmt.executeQuery(sql); int count = -1 ; if(rs.next()){ count =rs.getInt(1); } return count; }catch(SQLException e) { e.printStackTrace(); return -1; }catch(ClassNotFoundException e) { e.printStackTrace(); return -1; }catch(Exception e) { e.printStackTrace(); return -1; }finally { try { if(rs!=null)rs.close(); if(stmt!= null)stmt.close(); if(connection!= null)connection.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
jsp和Java嵌套
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import = "java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test"; String user = "sa"; String password = "123456"; Connection connection = null; ResultSet rs = null; Statement stmt = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); connection = DriverManager.getConnection(url, user, password); stmt = connection.createStatement(); String name = request.getParameter("uname"); String pwd = request.getParameter("upwd"); String sql = "select count(*) from login where upwd='"+pwd+"' and uname ='"+name+"' "; rs = stmt.executeQuery(sql); int count = -1 ; if(rs.next()){ count =rs.getInt(1); } if(count>0){ out.print("登录成功"); }else { out.print("登陆失败"); } }catch(SQLException e) { e.printStackTrace(); }catch(ClassNotFoundException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { if(rs!=null)rs.close(); if(stmt!= null)stmt.close(); if(connection!= null)connection.close(); }catch(SQLException e){ e.printStackTrace(); } } %> </body> </html>
注意:
如果jsp出现错误:The import xxx cannot be resolved
解决方案:
1、可能是jdk、tomcat版本问题:右键项目——》build path,将其中报错的lib删除重新导入;
2、清空各种缓存:右键项目——》clean tomcat;project ——》clean;
3、重新安装tomcat,计算机重启;
4、如果内之前没有包,则将该类导入包中
Javabean:
作用:减轻jsp的复杂度;提高代码复用度(登录操作,可以用LoginDao类)
JavaBean(就是一个Java类)的定义:
1、public 修饰的类,public无参构造
2、所有属性(如果有)都是private,并且提供set/get(如果是boolean,则get可以替换成is)
使用层面,JavaBean分为两大类:
1、封装业务逻辑的JavaBean(LoginDao.java封装了逻辑) 逻辑
可以将jsp中的jdbc代码,封装到Login.java中
2、封装数据的JavaBean (实体类,Student.java Preson.Java) 数据
Login login = new Login(uname,upwd);//即用Login对象封装两个数据(用户名和密码)
具体查看Login.java
区别:
封装数据的JavaBean,对应于一张表(Login(name,pwd))
封装业务逻辑的JavaBean,用于操作一个封装数据的JavaBean。