1、html
Callablestatement:調用 數據庫中的存儲過程、存儲函數
connection.prepareCall(參數:存儲過程/存儲函數名)
參數格式:
存儲過程:(無返回值return,用Out參數代替返回值)
{call 存儲過程名(參數列表)}
存儲函數:(有返回值return)
{ ?=call 存儲函數名(參數列表)}
存儲過程:
create or replace procedure addTwoNum( num1 in number,num2 in number,result out number) --1+2-->3
as
begin
result :=num1+num2;
end;
/ --/結束
強調:
若是經過sqlplus訪問數據庫,只須要開啓:OracleServiceSID
經過其它程序訪問數據(Sqldevelop、Navicat、JDBC),須要開啓:OracleServiceSID、XxxListener
JDBC調用存儲過程的步驟
a.產生調用存儲過程的對象(CallableStatement )cstmt=connection.prepareCall("{call addTwoNum(?,?,?)}");
b.利用setXXX()進行處理 cstmt.setInt(1, 10)
c.經過registerOutParameter()處理輸出參數類型 cstmt.registerOutParameter(3, Types.INTEGER);//必須放在execute()前面
d.cstmt.execute();//execute()進行執行
e.接受返回值int result=cstmt.getInt(3);java
package JDBCDemo; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.Types; public class JDBCcallablestatementDemo { private static final String URL="jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&characterEncoding=utf-8"; private static final String USERNAME="root"; private static final String PWD="vayne"; public static void update() { CallableStatement cstmt=null; Connection connection=null; try { //a.導入驅動,加載具體的驅動類 Class.forName("com.mysql.cj.jdbc.Driver"); //b.與數據庫創建鏈接 connection = DriverManager.getConnection(URL,USERNAME,PWD); //使用Ctrl+1,快速生成值來獲取Connection //c.發送SQL,執行(增刪改、查) cstmt=connection.prepareCall("{call addTwoNum(?,?,?)}"); cstmt.setInt(1, 10); cstmt.setInt(2, 20); cstmt.registerOutParameter(3, Types.INTEGER);//必須放在execute()前面 cstmt.execute();//execute()前處理輸入值及輸出參數類型,execute()後處理輸出值 //設置輸出參數的類型 int result=cstmt.getInt(3); //處理結果 System.out.println(result+"操做成功!!!"); }catch(ClassNotFoundException e) { e.printStackTrace(); }catch(SQLException e) { e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally { try { //先開的後關,後開的先關 if(cstmt!=null)cstmt.close(); if(connection !=null)connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } } public static void main(String[] args) { update(); } }
2、JavaBean
將java代碼和jsp代碼分開存放
做用:
一、減輕jsp頁面的負擔,便於開發人員進行分塊管理代碼
二、提升代碼複用率,封裝成類,使用時直接調用便可。
定義
一、public修飾的類以及public修飾的無參構造
二、全部的屬性都是private,而且還提供set/get (boolean類型提供set/is)
使用層面,分爲兩類:
一、封裝業務邏輯的JavaBean (LoginDao.java封裝了登陸邏輯) 即邏輯
能夠將jsp中的JDBC代碼,封裝到Dao.java類中 (Dao.java)
二、封裝數據的JavaBean (實體類,Student.java Person.java ) 即數據
對應於數據庫中的一張表
User user=new User(name,pwd);//即用User對象 封裝了2個數據(用戶名 和密碼)
封裝數據的JavaBean 對應於數據庫中的一張表 (User(name,pwd))
封裝業務邏輯的JavaBean 用於操做 一個封裝數據的JavaBean
能夠發現,JavaBean能夠簡化 代碼(jsp->jsp+java)、提供代碼複用(Dao.java)
mysql
登陸實例sql
Navicat中建表以下數據庫
login.jspjsp
<%@ 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> <form action="check.jsp" method="post"> 用戶名<input type="text" name="uname"><br/> 密碼<input type="password" name="upwd"><br/> <input type="submit" value="登陸"><br/> </form> </body> </html>
check.jsp函數
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="Dao.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name=request.getParameter("uname"); String pwd=request.getParameter("upwd"); User user=new User(name,pwd); Dao dao =new Dao(); int result=Dao.login(user); if(result>0){ out.print("登陸成功"); }else{ out.print("用戶名或密碼錯誤"); }%> </body> </html>
DBUtil.DBUtil.javapost
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Dao.User; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/system?serverTimezone=UTC&characterEncoding=utf-8"; public static String db_user = "root"; public static String db_pass = "vayne"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Dao.User.javathis
package Dao; public class User { private int id; private static String name; private static String pwd; private static String hobby; public User() { } public User( String name, String pwd) { this.name = name; this.pwd = pwd; } public User(int id, String name, String pwd, String hobby) { this.id = id; this.name = name; this.pwd = pwd; this.hobby = hobby; } public int getId() { return id; } public void setId(int id) { this.id = id; } public static String getName() { return name; } public void setName(String name) { this.name = name; } public static String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public static String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } }
Dao.Dao.javaurl
package Dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Dao.User; import DBUtil.DBUtil; public class Dao { public static int login(User user) { int f=-1; String sql = "select * from JDBCjsp where name = '" + User.getName() + "' and password = '"+User.getPwd()+"'"; // Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); if (rs.next()) { f = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return f; } }
演示截圖
輸入正確的密碼
輸入錯誤的用戶名