java: framework from BLL、DAL、IDAL、MODEL、Factory, using MySql 8.0
sql script:
1 2 3 4 5 6 7 8 9 | drop table BookKindList; #书目录 create table BookKindList ( BookKindID INT NOT NULL AUTO_INCREMENT, #自动增加 BookKindName nvarchar(500) not null , BookKindParent int null , PRIMARY KEY (BookKindID) #主键 ); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #删除 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /* * 版权所有 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | /* * 版权所有 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 ArrayList<BookKind> SelectSQLBookKindAll() { ArrayList<BookKind> list= new ArrayList<BookKind>(); 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<BookKind> SelectSProcBookKindAll() { ArrayList<BookKind> list= new ArrayList<BookKind>(); 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /* * 版权所有 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(? ,?)" ; ArrayList<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<DuParameter> duParameters= new ArrayList<DuParameter>(); 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<BookKind> selectAllSQL() { ArrayList<BookKind> list= new ArrayList<BookKind>(); 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<BookKind> selectAllProc() { ArrayList<BookKind> list= new ArrayList<BookKind>(); 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /* * 版权所有 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 ArrayList<BookKind> SelectSQLBookKindAll(); } |
Factory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /* * 版权所有 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /* * 版权所有 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 ArrayList<BookKind> SelectSQLBookKindAll() { return dal.SelectSQLBookKindAll(); } } |
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | /* * 版权所有 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | /** * 存储过返回值 * @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; } |
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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
{}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2019-12-20 Sql: Oracle paging