标签:技术 开发 pos 函数名 vat his 生成 cte 调用
一、
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);
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(); } }
二、JavaBean
将java代码和jsp代码分开存放
作用:
1、减轻jsp页面的负担,便于开发人员进行分块管理代码
2、提高代码复用率,封装成类,使用时直接调用即可。
定义
1、public修饰的类以及public修饰的无参构造
2、所有的属性都是private,并且还提供set/get (boolean类型提供set/is)
使用层面,分为两类:
1、封装业务逻辑的JavaBean (LoginDao.java封装了登录逻辑) 即逻辑
可以将jsp中的JDBC代码,封装到Dao.java类中 (Dao.java)
2、封装数据的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)
登录实例
Navicat中建表如下
login.jsp
<%@ 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.java
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.java
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.java
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; } }
演示截图
输入正确的密码
输入错误的用户名
Callablestatement与JavaBean及其实例
标签:技术 开发 pos 函数名 vat his 生成 cte 调用
原文地址:https://www.cnblogs.com/yeyueweiliang/p/12207147.html