java: framework from BLL、DAL、IDAL、MODEL、Factory, using MySql 8.0
sql script:
drop table BookKindList; #书目录 create table BookKindList ( BookKindID INT NOT NULL AUTO_INCREMENT, #自动增加 BookKindName nvarchar(500) not null, BookKindParent int null, PRIMARY KEY(BookKindID) #主键 );
#删除 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`DeleteBookKind` $$ CREATE PROCEDURE `geovindu`.`DeleteBookKind` (IN param1 INT) BEGIN Delete From bookkindlist WHERE BookKindID = param1; END $$ DELIMITER ; delete from bookkindlist WHERE BookKindID =10; SELECT * FROM bookkindlist; execute DeleteBookKind(10); #查询所有 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindListAll` $$ CREATE PROCEDURE `geovindu`.`proc_Select_BookKindListAll` () BEGIN SELECT * FROM bookkindlist; END $$ DELIMITER ; DROP PROCEDURE proc_Select_BookKindListAll; select * from `geovindu`.`bookkindlist`; SELECT * FROM bookkindlist; #统计 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`BookKindCount` $$ CREATE PROCEDURE `geovindu`.`BookKindCount` (OUT param1ID INT) BEGIN select COUNT(*) into param1ID From bookkindlist; END $$ DELIMITER ; #更新 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Update_BookKindList` $$ CREATE PROCEDURE `geovindu`.`proc_Update_BookKindList` (IN param1ID Int,IN param1Name NVarChar(1000),IN param1Parent Int) BEGIN IF NOT EXISTS (SELECT * FROM BookKindList WHERE BookKindName=param1Name) then #如果存在相同的记录,不更新名称 UPDATE BookKindList SET BookKindName=param1Name , BookKindParent=param1Parent where BookKindID=param1ID; ELSE UPDATE BookKindList SET BookKindParent=param1Parent where BookKindID=param1ID; END IF; END $$ DELIMITER ; #查询一条 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Select_BookKindList` $$ CREATE PROCEDURE `geovindu`.`proc_Select_BookKindList` (IN param1 INT) BEGIN SELECT * FROM BookKindList WHERE BookKindID = param1; END $$ DELIMITER ; #插入一条 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindList` $$ CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindList` (IN param1Name NVarChar(1000),IN param1Parent Int) BEGIN insert into BookKindList(BookKindName,BookKindParent) values(param1Name,param1Parent); END $$ DELIMITER ; #插入一条返回值 DELIMITER $$ DROP PROCEDURE IF EXISTS `geovindu`.`proc_Insert_BookKindOut` $$ CREATE PROCEDURE `geovindu`.`proc_Insert_BookKindOut` (IN param1Name NVarChar(1000),IN param1Parent Int,OUT ID INT) BEGIN IF NOT EXISTS (SELECT * FROM BookKindList WHERE BookKindName=param1Name) then #如果存在相同的记录,不添加 INSERT INTO BookKindList (BookKindName,BookKindParent)VALUES(param1Name ,param1Parent); #set ID=Last_insert_id() SELECT LAST_INSERT_ID() into ID; end if; END $$ DELIMITER ;
MODEL:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * 实体类 *@author geovindu 涂聚文 Geovin Du * @ * * */ public class BookKind { // private int bookKindID; private String bookKindName; private int bookKindParent; /** * @param * @return 得到ID * */ public int getBookKindID() { return bookKindID; } /** * @param bookKindID 设置输入参数 * * */ public void setBookKindID(int bookKindID) { this.bookKindID = bookKindID; } /** * @param * @return 得到目录名称 * */ public String getBookKindName() { return bookKindName; } /** * @param bookKindName 设置输入参数 * * */ public void setBookKindName(String bookKindName) { this.bookKindName = bookKindName; } /** * @param * @return 得到父节点的值 * */ public int getBookKindParent() { return bookKindParent; } /** * @param bookKindParent 设置输入参数 * * */ public void setBookKindParent(int bookKindParent) { this.bookKindParent = bookKindParent; } }
DAL:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:DAL数据访问层 数据业务层,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Geovin Du * 2021-12-15 修改:涂聚文 * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKindDAL.java * 配置文件: * driver=com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ //#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end package Geovin.DAL; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.sql.*; import java.lang.reflect.Parameter; import Geovin.Interface.BookKindInterface; import Geovin.UtilitieDB.MySqlHelper; import Geovin.Model.*; import Geovin.Interface.*; import Geovin.Factory.*; /** *#parse("File Header.java") * @apiNote 数据业务层 * @deprecated * @Description * @projectName * @author geovindu 涂聚文 Geovin Du * @date * @version 1.0 * */ public class BookKindDAL implements BookKindInterface { /** * @param info 输入一个实体 * @return 返回int 1 是否插入一条记录 * @Description 添加一条记录 * */ public int Add(BookKind info) { int ok=0; ResultSet resultSet = null; try { String sql = "{call proc_Insert_BookKindList(?,?)}"; String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) }; MySqlHelper.CallProc(sql,parameters); ok=1; } catch (Exception exception) { ok=0; exception.printStackTrace(); } finally { MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection()); } return ok; } /** * @param info 输入实体 * @return 返回值 * * */ public int AddOut(BookKind info) { int ok=0; ResultSet resultSet = null; try { String sql = "{call proc_Insert_BookKindOut(?,?,?)}"; //多少个参数,多少个问号,包括输入,输出参数后面,输入,输出的个数量要明晰 String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) }; Integer[] out = { Types.INTEGER }; CallableStatement cs=(CallableStatement)MySqlHelper.CallProcOutInt(sql,parameters,out); ok= cs.getInt(3); } catch (Exception exception) { ok=0; exception.printStackTrace(); } finally { MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection()); } return ok; } /** * 添加返回值 * @param info 输入实体 * @param outValue 返回值 * @return 返回值 * * */ public int AddOut(BookKind info,OutValue outValue) { int ok=0; ResultSet resultSet = null; try { String sql = "{call proc_Insert_BookKindOut(?,?,?)}"; String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()) }; Integer[] out = { Types.INTEGER }; CallableStatement cs=(CallableStatement)MySqlHelper.CallProcOutInt(sql,parameters,out); outValue.setIntValue(cs.getInt(3)); ok=cs.getInt(3); } catch (Exception exception) { ok=0; exception.printStackTrace(); } finally { MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection()); } return ok; } /** *添加返回值 * @param info 一个实体记录 * @return * * */ public int AddOut2(BookKind info) { int ok=0; ResultSet resultSet = null; try { String sql = "{call proc_Insert_BookKindOut(?,?,?)}"; String[] parameters = {info.getBookKindName(), String.valueOf(info.getBookKindParent()),""}; Integer out =Types.INTEGER; MySqlHelper.callProcInputAndOutPutString(sql,parameters); ok=out; //不是添加的ID值 } catch (Exception exception) { ok=0; exception.printStackTrace(); } finally { MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection()); } return ok; } /** * #parse("更新记录") * @param info 输入实体 * @return 返回参数 * * */ public int Update(BookKind info) { int ok=0; ResultSet resultSet = null; try { String sql = "{call proc_Update_BookKindList(?,?,?)}"; String[] parameters = {String.valueOf(info.getBookKindID()), info.getBookKindName(), String.valueOf(info.getBookKindParent()) }; MySqlHelper.CallProc(sql,parameters); ok=1; // } catch (Exception exception) { ok=0; exception.printStackTrace(); } finally { MySqlHelper.close(resultSet, MySqlHelper.getCs(), MySqlHelper.getConnection()); } return ok; } /** * 查询一条记录 * @param id * @return BookKind 指定查找的ID记录 * @author geovindu * @date 2021-12-20 * */ public BookKind SelectSQLBookKindInfo(String id) { BookKind info=null; String sql = "SELECT * FROM BookKindList where BookKindID=?"; String[] parameters = { id }; try { info=new BookKind(); ResultSet rs = MySqlHelper.DuexecuteQuery(sql, parameters); while (rs.next()) { info.setBookKindID(rs.getInt("BookKindID")); info.setBookKindName(rs.getString("BookKindName")); info.setBookKindParent(rs.getInt("BookKindParent")); } //return info; } catch (SQLException e) { e.printStackTrace(); } finally { MySqlHelper.close(MySqlHelper.getRs(), MySqlHelper.getPs(), MySqlHelper .getConnection()); } return info; } /** * 查询所有记录 * @param * @return BookKind 所有记录 * @date 2021-12-20 * @author geovindu * */ public ArrayListSelectSQLBookKindAll() { ArrayList list=new ArrayList (); String sql = "SELECT * FROM BookKindList"; try { BookKind info=null; ResultSet rs = MySqlHelper.DuexecuteQuery(sql,null); while (rs.next()) { info=new BookKind(); info.setBookKindID(rs.getInt("BookKindID")); info.setBookKindName(rs.getString("BookKindName")); info.setBookKindParent(rs.getInt("BookKindParent")); list.add(info); } //return info; } catch (SQLException e) { e.printStackTrace(); } finally { MySqlHelper.close(MySqlHelper.getRs(), MySqlHelper.getPs(), MySqlHelper .getConnection()); } return list; } /** * 存储过程查询 * @param * * */ public ArrayList SelectSProcBookKindAll() { ArrayList list=new ArrayList (); String sql = "call proc_Select_BookKindListAll()"; try { BookKind info=null; ResultSet rs = MySqlHelper.ExecuteQueryProcData(sql,null); while (rs.next()) { info=new BookKind(); info.setBookKindID(rs.getInt("BookKindID")); info.setBookKindName(rs.getString("BookKindName")); info.setBookKindParent(rs.getInt("BookKindParent")); list.add(info); } //return info; } catch (SQLException e) { e.printStackTrace(); } finally { MySqlHelper.close(MySqlHelper.getRs(), MySqlHelper.getPs(), MySqlHelper .getConnection()); } return list; } }
edit:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:DAL数据访问层 数据业务层,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-22 创建者 geovindu * 2021-12-25 添加 Lambda * 2021-12-25 修改:date * 接口类 * 2021-12-25 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc GeovinDuDAL.java * 配置文件: * driver=com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ //#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end package Geovin.DAL; import java.awt.print.Book; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.sql.*; import java.lang.reflect.Parameter; import Geovin.Model.*; import Geovin.UtilitieDB.DuMySqlHelper; /** * 数据业务层操作 * @author geovindu 涂聚文 Geovin Du * @version 1.0 * * * */ public class GeovinDuDAL { DuMySqlHelper duMySqlHelperr=new DuMySqlHelper(); /** *SQL 语句添加 * @param bookKind * @return bool * */ public Boolean AddSql(BookKind bookKind) { Boolean isok=false; String sql="INSERT INTO BookKindList(BookKindName,BookKindParent) values(? ,?)"; ArrayListduParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindName()); duParameter.setDataType("String"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindParent()); duParameter.setDataType("int"); duParameters.add(duParameter); isok=duMySqlHelperr.ExecuteInsertSql(sql,duParameters); return isok; } /** * 存储过程添加 * @param bookKind * @return bool * **/ public Boolean AddProc(BookKind bookKind) { Boolean isok=false; String sql="{CALL proc_Insert_BookKindList(? ,?)}"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindName()); duParameter.setDataType("String"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindParent()); duParameter.setDataType("int"); duParameters.add(duParameter); isok=duMySqlHelperr.ExecuteInsert(sql,duParameters); return isok; } /** * 添加有返回值 * @param bookKind 输入参数 * @param outValue 返回参数 * @return bool 返回添加是否成功 * * */ public Boolean AddProc(BookKind bookKind,int outValue) { Boolean isok=false; String sql="{CALL proc_Insert_BookKindList(? ,?)}"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindName()); duParameter.setDataType("String"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindParent()); duParameter.setDataType("int"); duParameters.add(duParameter); isok=duMySqlHelperr.ExecuteInsertOutSingleInt(sql,duParameters,outValue); return isok; } /** * 修改 sql * @param bookKind * @return int * */ public int EditSQL(BookKind bookKind) { int isok=0; String sql="UPDATE BookKindList SET BookKindName=?,BookKindParent=? where BookKindID=?"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindName()); duParameter.setDataType("String"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindParent()); duParameter.setDataType("int"); duParameters.add(duParameter); duParameter.setParameterValue(bookKind.getBookKindID()); duParameter.setDataType("int"); duParameters.add(duParameter); isok=duMySqlHelperr.ExecuteUpdateSql(sql,duParameters); return isok; } /** *修改 存储过程 * @param bookKind * @return int * */ public int EditProc(BookKind bookKind) { int isok=0; String sql="CALL proc_Update_BookKindList(?,?,?)"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindID()); duParameter.setDataType("int"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindName()); duParameter.setDataType("String"); duParameters.add(duParameter); duParameter=new DuParameter(); duParameter.setParameterValue(bookKind.getBookKindParent()); duParameter.setDataType("int"); duParameters.add(duParameter); isok=duMySqlHelperr.ExecuteUpdate(sql,duParameters); return isok; } /** * 删除 SQL语句 * @param id * @return int * * */ public int DelSQL(int id) { int isok=0; String sql="Delete From bookkindlist WHERE BookKindID =?"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(id); duParameter.setDataType("int"); duParameters.add(duParameter); isok= duMySqlHelperr.ExecuteDeletSql(sql,duParameters); return isok; } /** * 删除 存储过程语句 * @param id * @return int * * */ public int DelProc(int id) { int isok=0; String sql="{CALL DeleteBookKind(?)}"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(id); duParameter.setDataType("int"); duParameters.add(duParameter); isok= duMySqlHelperr.ExecuteDelte(sql,duParameters); return isok; } /** *SQL语句查询 * @param id * @return * */ public BookKind selectSQL(int id) { ResultSet resultSet=null; BookKind bookKind=null; String sql = "SELECT * FROM BookKindList where BookKindID=?"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(id); duParameter.setDataType("int"); duParameters.add(duParameter); try { resultSet = duMySqlHelperr.ExecuteQuery(sql, duParameters); while (resultSet.next()) { bookKind =new BookKind(); bookKind.setBookKindID(resultSet.getInt("BookKindID")); bookKind.setBookKindName(resultSet.getString("BookKindName")); bookKind.setBookKindParent(resultSet.getInt("BookKindParent")); } } catch (Exception exception) { exception.printStackTrace(); } return bookKind; } /** *存储过程语句 * @param id * @return * */ public BookKind selectProc(int id) { ResultSet resultSet=null; BookKind bookKind=null; String sql = "{CALL proc_Select_BookKindList(?)}"; ArrayList duParameters=new ArrayList (); DuParameter duParameter=null; duParameter=new DuParameter(); duParameter.setParameterValue(id); duParameter.setDataType("int"); duParameters.add(duParameter); try { resultSet = duMySqlHelperr.ExecuteQuery(sql, duParameters); while (resultSet.next()) { bookKind =new BookKind(); bookKind.setBookKindID(resultSet.getInt("BookKindID")); bookKind.setBookKindName(resultSet.getString("BookKindName")); bookKind.setBookKindParent(resultSet.getInt("BookKindParent")); } } catch (Exception exception) { exception.printStackTrace(); } return bookKind; } /** *SQL语句查询 * @param * @return * */ public ArrayList selectAllSQL() { ArrayList list=new ArrayList (); ResultSet resultSet=null; BookKind bookKind=null; String sql = "SELECT * FROM BookKindList"; try { resultSet = duMySqlHelperr.ExecuteQuery(sql, null); while (resultSet.next()) { bookKind =new BookKind(); bookKind.setBookKindID(resultSet.getInt("BookKindID")); bookKind.setBookKindName(resultSet.getString("BookKindName")); bookKind.setBookKindParent(resultSet.getInt("BookKindParent")); list.add(bookKind); } } catch (Exception exception) { exception.printStackTrace(); } return list; } /** *存储过程语句 * @param * @return * */ public ArrayList selectAllProc() { ArrayList list=new ArrayList (); ResultSet resultSet=null; BookKind bookKind=null; String sql = "{CALL proc_Select_BookKindListAll()}"; try { resultSet = duMySqlHelperr.ExecuteQuery(sql, null); while (resultSet.next()) { bookKind =new BookKind(); bookKind.setBookKindID(resultSet.getInt("BookKindID")); bookKind.setBookKindName(resultSet.getString("BookKindName")); bookKind.setBookKindParent(resultSet.getInt("BookKindParent")); list.add(bookKind); } } catch (Exception exception) { exception.printStackTrace(); } return list; } }
IDAL:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:Interface 接口层,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKindInterface.java * 配置文件: * driver=com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Interface; import Geovin.Model.BookKind; import java.util.ArrayList; /** * #parse("接口") * @author geovindu 涂聚文 Geovin Du * @version 1.0 * */ public interface BookKindInterface { /** * @param info * @return * */ public int Add(BookKind info); /** * * @param info * @return * * */ public int AddOut(BookKind info); /** * * @param info * @return * * */ public int Update(BookKind info); /** * * @param id * @return * * **/ public BookKind SelectSQLBookKindInfo(String id); /** * * @param * @return * * */ public ArrayListSelectSQLBookKindAll(); }
Factory:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:工厂层,抽象工厂 连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Factory; import Geovin.DAL.BookKindDAL; import Geovin.Interface.BookKindInterface; /** * #parse("抽象工厂") * @author geovindu 涂聚文 Geovin Du * @version 1.0 * * */ public class AbstractFactory { /** * * * */ public static BookKindInterface CreateBookKind() { BookKindInterface iBookKindInterface=new BookKindDAL(); return iBookKindInterface; } }
BLL:
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:业务逻辑层,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.BLL; import Geovin.Model.*; import Geovin.Factory.AbstractFactory; import Geovin.Interface.*; import java.util.ArrayList; /** * #parse("业务逻辑层") * @author geovindu 涂聚文 Geovin Du * @ * */ public class BookKindBLL { private static BookKindInterface dal=AbstractFactory.CreateBookKind(); /** * * */ public int Add(BookKind info) { return dal.Add(info); } /** * * */ public int AddOut(BookKind info) { return dal.AddOut(info); } /** * * */ public int Update(BookKind info) { return dal.Update(info); } /** * * */ public BookKind SelectSQLBookKindInfo(String id) { return dal.SelectSQLBookKindInfo(id); } /** * * */ public ArrayListSelectSQLBookKindAll() { return dal.SelectSQLBookKindAll(); } }
测试:
//CustomerDAL dal=new CustomerDAL(); //dal.SelectSQLCustomer("1"); // BookKindDAL dal=new BookKindDAL(); BookKindBLL dal=new BookKindBLL(); BookKind info=dal.SelectSQLBookKindInfo("1"); System.out.println("\t\n实体读出:id-"+info.getBookKindID()+";类目名称:"+info.getBookKindName()+";父节点ID:"+info.getBookKindParent()); BookKind newinfo=new BookKind(); newinfo.setBookKindID(5); newinfo.setBookKindName("聚文小说"); newinfo.setBookKindParent(2); int ok=dal.Update(newinfo); if(ok>0) { System.out.println("更新记录,ok"+String.valueOf(ok)); } else { System.out.println("更新不成功,no"); } info=new BookKind(); info=dal.SelectSQLBookKindInfo("5"); System.out.println("\t\n"+info.getBookKindName());
java Out Parameters
from: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-a-stored-procedure-with-output-parameters?view=sql-server-ver15
https://stackoverflow.com/questions/4455693/how-to-create-in-out-or-out-parameters-in-java
https://mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/
http://javacamp.org/javavscsharp/outparam.html 这个没在有啥参考价值
https://stackoverflow.com/questions/50713653/multiple-out-parameters-in-simplejdbccall
http://kscodes.com/java/callablestatement-example-with-in-out-parameters/
https://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html
https://www.codejava.net/java-se/jdbc/jdbc-examples-for-calling-stored-procedures-mysql
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体层 ,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKindDAL.java * 配置文件: * driver=com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * 用于返回值 * @author geovindu 涂聚文 Geovin Du * @date 2021-12-20 * **/ public class OutValue { private int intValue; /** * 得到值 * @param * @return * * */ public int getIntValue() { return intValue; } /** * 设定值 * @param intValue 输入值 * @param * */ public void setIntValue(int intValue ) { this.intValue=intValue; } private String stringValue; /** * 得到值 * @param * @return * * */ public String getStringValue() { return stringValue; } /** * 设定值 * @param stringValue 设定值 * @param * */ public void setStringValue(String stringValue) { this.stringValue=stringValue; } }
/** * 存储过返回值 * @param info 输入实体类型值 * @return 返回添加的ID的值 * @ * */ public int proc(BookKind info) { int out=0; Connection connection=null; CallableStatement cstm =null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); String sql = "{CALL proc_Insert_BookKindOut(?,?,?)}"; //调用存储过程 cstm = connection.prepareCall(sql); //实例化对象cstm,执行存储过程 cstm.setString(1, info.getBookKindName()); //存储过程输入参数 //也是可以的 //cstm.setString(2,String.valueOf(info.getBookKindParent()));//可以判断什么类型,进行什么类型转换 // cstm.setInt(2,info.getBookKindParent()); cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 即返回值 cstm.execute(); // 执行存储过程 System.out.println(cstm.getInt(3)); out=cstm.getInt(3); cstm.close(); connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return out; } /** * 得到返回的值 * @param info 输入参数 * @param outValue 得到返回的值 * @return 返回值 * */ public int proc(BookKind info, OutValue outValue) { int out=0; Connection connection=null; CallableStatement cstm =null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); String sql = "{CALL proc_Insert_BookKindOut(?,?,?)}"; //调用存储过程 cstm = connection.prepareCall(sql); //实例化对象cstm,执行存储过程 cstm.setString(1, info.getBookKindName()); //存储过程输入参数 //也是可以的 //cstm.setString(2,String.valueOf(info.getBookKindParent()));//可以判断什么类型,进行什么类型转换 // cstm.setInt(2,info.getBookKindParent()); cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 即返回值 cstm.execute(); // 执行存储过程 System.out.println(cstm.getInt(3)); out=cstm.getInt(3); outValue.setIntValue(cstm.getInt(3)); cstm.close(); connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return out; }
测试:
OutValue value=new OutValue(); BookKindDAL dal=new BookKindDAL(); BookKind info=new BookKind(); //info.setBookKindName("社会科学"); //info.setBookKindName("自然科学"); info.setBookKindName("文学"); info.setBookKindParent(2); // int ok= dal.proc(info); int ok=dal.proc(info,value); if(ok>0) { System.out.println("ok:"+ok+",out:"+value.getIntValue()); } else { System.out.println("no"); }
如何判断值类型
变量的值类型
一个类的属性的值类型
public interface GoevinDuA
{}
public interface GoevinDuB
{}
public class Du implements GeovinDuA
{}
public interface GeovinDuC extends GeovinDuA
{}
public class AB extends Object implements GeovinDuA,GeovinDub
{}