java: DAL using SQL Server
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 | ---角色类型表RoleType Geovin Du 涂聚文 IF EXISTS ( select * from sysobjects where id = object_id(N '[dbo].[RoleTypeList]' ) and OBJECTPROPERTY(id, N 'IsUserTable' ) = 1) DROP TABLE [RoleTypeList] GO CREATE TABLE RoleTypeList ( RoleType INT IDENTITY(1,1) PRIMARY KEY , RoleTypeName NVARCHAR(50) NOT NULL , RoleTypeDescribe NTEXT NULL ) GO insert into RoleTypeList(RoleTypeName,RoleTypeDescribe) values (N '系统管理' ,N '超级权限' ); insert into RoleTypeList(RoleTypeName,RoleTypeDescribe) values (N '管理层' ,N '高级权限' ); insert into RoleTypeList(RoleTypeName,RoleTypeDescribe) values (N '普通员工' ,N '一般用户' ); go select * from RoleTypeList go IF EXISTS ( SELECT * FROM sysobjects WHERE [ name ] = 'dbo.proc_Select_RoleTypeListAll' ) DROP PROCEDURE dbo.proc_Select_RoleTypeListAll GO CREATE PROCEDURE dbo.proc_Select_RoleTypeListAll AS SELECT * FROM dbo.RoleTypeList GO exec proc_Select_RoleTypeListAll go IF EXISTS ( SELECT * FROM sysobjects WHERE [ name ] = 'dbo.proc_Insert_Operation' ) DROP PROCEDURE dbo.proc_Insert_Operation GO CREATE PROCEDURE dbo.proc_Insert_Operation ( @RoleTypeName NVarChar(50), @RoleTypeDescribe NVarChar(200) ) AS INSERT INTO dbo.RoleTypeList ( RoleTypeName , RoleTypeDescribe ) VALUES ( @RoleTypeName , @RoleTypeDescribe ) GO IF EXISTS ( SELECT * FROM sysobjects WHERE [ name ] = 'dbo.proc_Insert_RoleTypeListOutput' ) DROP PROCEDURE dbo.proc_Insert_RoleTypeListOutput GO CREATE PROCEDURE dbo.proc_Insert_RoleTypeListOutput ( @RoleTypeName NVarChar(50), @RoleTypeDescribe NVarChar(200), @RoleType int output ) AS INSERT INTO dbo.RoleTypeList ( RoleTypeName , RoleTypeDescribe ) VALUES ( @RoleTypeName , @RoleTypeDescribe ) select @RoleType=@@IDENTITY GO IF EXISTS ( SELECT * FROM sysobjects WHERE [ name ] = 'dbo.proc_Update_RoleTypeList' ) DROP PROCEDURE dbo.proc_Update_RoleTypeList GO CREATE PROCEDURE dbo.proc_Update_RoleTypeList ( @RoleTypeName NVarChar(50), @RoleTypeDescribe NVarChar(200), @RoleType int ) AS UPDATE dbo.RoleTypeList SET [RoleTypeName]=@RoleTypeName , [RoleTypeDescribe]=@RoleTypeDescribe where RoleType =@RoleType GO IF EXISTS ( select * from sysobjects where [ name ] = 'dbo.proc_Delete_RoleTypeList' ) DROP PROCEDURE dbo.proc_Delete_RoleTypeList GO CREATE PROCEDURE dbo.proc_Delete_RoleTypeList ( @RoleType Int ) as DELETE dbo.RoleTypeList WHERE RoleType = @RoleType GO IF EXISTS ( SELECT * FROM sysobjects WHERE [ name ] = 'dbo.proc_Select_RoleTypeList' ) DROP PROCEDURE dbo.proc_Select_RoleTypeList GO CREATE PROCEDURE dbo.proc_Select_RoleTypeList ( @RoleType Int ) AS SELECT * FROM dbo.RoleTypeList WHERE RoleType = @RoleType GO |
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 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | /* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述: * IDE:IntelliJ IDEA 2021.2.3 * 历史版本: JDK 14.02 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 mssql-jdbc-9.4.1.jre8.jar. * 数据库:MSSQL Server 2019 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuMsSQLConn.java *https://www.microsoft.com/en-us/software-download/windows10 *https://github.com/PaddlePaddle/PaddleOCR *https://docs.microsoft.com/es-es/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 *https://github.com/microsoft/mssql-jdbc/blob/main/README.md * */ import Geovin.Model.OutValue; import Geovin.Model.RoleTypeList; import java.io.Reader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.*; import java.util.ArrayList; //import com.microsoft.*; /** * @author geovindu * @version 1.0 * * */ public class DuMsSqlConn { //数据库地址 //1 可以用 //String url = "jdbc:sqlserver://127.0.0.1:1433;DataBaseName=geovindu"; //GEOVINDU //localhost 127.0.0.1:1433 //2 String url = "jdbc:sqlserver://GEOVINDU;DataBaseName=geovindu" ; //1 String connectionUrl = "jdbc:sqlserver://GEOVINDU;databaseName=geovindu;user=sa;password=geovindu" ; // String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; //com.mysql.cj.jdbc.Driver //com.mysql.jdbc.Driver "com.microsoft.sqlserver.jdbc.SQLServerDriver // String userName = "sa" ; // String password = "geovindu" ; Connection con; Statement stmt = null ; //proc PreparedStatement pstmt= null ; //SQl CallableStatement cstmt= null ; //PRoc /** * 连接 * * */ public Connection getConnection() { try { Class.forName(driverName); //System.out.println("Ms SQL 数据库驱动加载成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(connectionUrl); //2 第二种方法 //con = DriverManager.getConnection(url,userName,password); // System.out.println("Ms SQL数据库连接成功"); //2 //stmt = con.createStatement(); // con.getMetaData() /* String sql; sql = "SELECT RoleType,RoleTypeName,RoleTypeDescribe FROM RoleTypeList"; pstmt=con.prepareStatement(sql); //2 // ResultSet rs = stmt.executeQuery(sql); ResultSet rs =pstmt.executeQuery(); // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int typid = rs.getInt("RoleType"); String rolename = rs.getString("RoleTypeName"); String roledec = rs.getString("RoleTypeDescribe"); // 输出数据 System.out.print("角色ID: " + typid); System.out.print(" , 角色名字: " + rolename); System.out.print(" , 角色描述: " + roledec); System.out.print("\n"); } // 完成后关闭 rs.close(); stmt.close(); con.close(); */ } catch (SQLException e) { e.printStackTrace(); } return con; } /** *SQL 语句查询所有 * @param * @return 返回集合 * */ public ArrayList<RoleTypeList> selectSqlAll() { ArrayList<RoleTypeList> alist= new ArrayList<RoleTypeList>(); RoleTypeList roleTypeList= null ; ResultSet resultSet= null ; try { Connection connection = getConnection(); String sql; sql = "SELECT RoleType,RoleTypeName,RoleTypeDescribe FROM RoleTypeList" ; pstmt = con.prepareStatement(sql); //2 // rs = stmt.executeQuery(sql); resultSet = pstmt.executeQuery(); // 展开结果集数据库 while (resultSet.next()) { // 通过字段检索 roleTypeList= new RoleTypeList(); roleTypeList.setRoleType(resultSet.getInt( "RoleType" )); roleTypeList.setRoleTypeName(resultSet.getString( "RoleTypeName" )); roleTypeList.setRoleTypeDescribe(resultSet.getString( "RoleTypeDescribe" )); alist.add(roleTypeList); } } catch (SQLException exception) { exception.printStackTrace(); } return alist; } /** *存储过程 查询所有 * @param * @return 返回集合 * */ public ArrayList<RoleTypeList> selectProclAll() { ArrayList<RoleTypeList> alist= new ArrayList<RoleTypeList>(); RoleTypeList roleTypeList= null ; ResultSet resultSet= null ; try { Connection connection = getConnection(); String sql; sql = "{ call proc_Select_RoleTypeListAll}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); //2 // resultSet = stmt.executeQuery(sql); // resultSet = pstmt.executeQuery(); resultSet = cstmt.executeQuery(); // 展开结果集数据库 while (resultSet.next()) { // 通过字段检索a roleTypeList= new RoleTypeList(); roleTypeList.setRoleType(resultSet.getInt( "RoleType" )); roleTypeList.setRoleTypeName(resultSet.getString( "RoleTypeName" )); roleTypeList.setRoleTypeDescribe(resultSet.getString( "RoleTypeDescribe" )); alist.add(roleTypeList); } } catch (SQLException exception) { exception.printStackTrace(); } return alist; } /** *添加 * @param roleTypeList * @return * */ public int AddSQL(RoleTypeList roleTypeList) { int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "INSERT INTO dbo.RoleTypeList(RoleTypeName ,RoleTypeDescribe) VALUES(? ,?)" ; pstmt = con.prepareStatement(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); pstmt.setString( 1 ,roleTypeList.getRoleTypeName()); pstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); //2 // rs = stmt.executeQuery(sql); isok = pstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *添加 存储过程 * @param roleTypeList * @return * */ public int AddProc(RoleTypeList roleTypeList) { //SQLServerCallableStatement int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "{call proc_Insert_Operation(? ,?)}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); cstmt.setString( 1 ,roleTypeList.getRoleTypeName()); cstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); //2 // rs = stmt.executeQuery(sql); isok = cstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *添加 返回 * @param roleTypeList * @param outValue 返回值 不成功 * */ public int AddSQLOut(RoleTypeList roleTypeList, OutValue outValue) { int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "INSERT INTO dbo.RoleTypeList(RoleTypeName ,RoleTypeDescribe) VALUES(? ,?)" ; pstmt = con.prepareStatement(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); pstmt.setString( 1 ,roleTypeList.getRoleTypeName()); pstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); //2 // rs = stmt.executeQuery(sql); isok = pstmt.executeUpdate(); String sql2= "select @@IDENTITY" ; PreparedStatement preparedStatement= null ; preparedStatement=con.prepareStatement(sql2); //cstmt= connection.prepareCall(sql2); ResultSet resultSet=preparedStatement.executeQuery(); //cstmt.executeQuery(); outValue.setIntValue(resultSet.getInt( 1 )); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *添加反回值 * @param roleTypeList * @param outValue * @return * */ public int AddProcOut(RoleTypeList roleTypeList, OutValue outValue) { //SQLServerCallableStatement int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "{call proc_Insert_RoleTypeListOutput(? ,?,?)}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); cstmt.setString( 1 ,roleTypeList.getRoleTypeName()); cstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); cstmt.registerOutParameter( 3 ,Types.INTEGER); //2 // //cstmt.execute();// isok=cstmt.executeUpdate(); // System.out.println(cstmt.getInt(3)); outValue.setIntValue(cstmt.getInt( 3 )); // isok =1; } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *更新 * @param roleTypeList * @return * */ public int UpdateSQL(RoleTypeList roleTypeList) { int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "UPDATE dbo.RoleTypeList set RoleTypeName=? ,RoleTypeDescribe=? where RoleType=?" ; pstmt = con.prepareStatement(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); pstmt.setString( 1 ,roleTypeList.getRoleTypeName()); pstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); pstmt.setInt( 3 ,roleTypeList.getRoleType()); //2 // rs = stmt.executeQuery(sql); isok = pstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *更新 存储过程 * @param roleTypeList * @return * */ public int UpdateProc(RoleTypeList roleTypeList) { //SQLServerCallableStatement int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "{call proc_Update_RoleTypeList(? ,?,?)}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); cstmt.setString( 1 ,roleTypeList.getRoleTypeName()); cstmt.setString( 2 , roleTypeList.getRoleTypeDescribe()); cstmt.setInt( 3 ,roleTypeList.getRoleType()); //2 // rs = stmt.executeQuery(sql); isok = cstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *删除 * @param id * @return * */ public int DeleteSQL( int id) { int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "DELETE dbo.RoleTypeList WHERE RoleType =?" ; pstmt = con.prepareStatement(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); pstmt.setInt( 1 ,id); //2 // rs = stmt.executeQuery(sql); isok = pstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *删除 存储过程 * @param id * @return * */ public int DeleteProc( int id) { //SQLServerCallableStatement int isok= 0 ; try { Connection connection = getConnection(); String sql; sql = "{call proc_Delete_RoleTypeList(?)}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); // pstmt.setInt(1,roleTypeList.getRoleType()); cstmt.setInt( 1 ,id); //2 // rs = stmt.executeQuery(sql); isok = cstmt.executeUpdate(); } catch (SQLException exception) { exception.printStackTrace(); } return isok; } /** *查询 * @param id * @return * **/ public RoleTypeList selectIdSql( int id) { RoleTypeList roleTypeList= null ; ResultSet resultSet= null ; try { Connection connection = getConnection(); String sql; sql = "SELECT RoleType,RoleTypeName,RoleTypeDescribe FROM RoleTypeList where RoleType=?" ; pstmt = con.prepareStatement(sql); pstmt.setInt( 1 ,id); //2 // rs = stmt.executeQuery(sql); resultSet = pstmt.executeQuery(); // 展开结果集数据库 while (resultSet.next()) { // 通过字段检索 roleTypeList= new RoleTypeList(); roleTypeList.setRoleType(resultSet.getInt( "RoleType" )); roleTypeList.setRoleTypeName(resultSet.getString( "RoleTypeName" )); roleTypeList.setRoleTypeDescribe(resultSet.getString( "RoleTypeDescribe" )); } } catch (SQLException exception) { exception.printStackTrace(); } return roleTypeList; } /** *存储过程 查询 * @param id * @return * */ public RoleTypeList selectIdProcl( int id) { RoleTypeList roleTypeList= null ; ResultSet resultSet= null ; try { Connection connection = getConnection(); String sql; sql = "{call proc_Select_RoleTypeList(?)}" ; //pstmt = con.prepareStatement(sql); cstmt=con.prepareCall(sql); cstmt.setInt( 1 ,id); //2 // resultSet = stmt.executeQuery(sql); // resultSet = pstmt.executeQuery(); resultSet = cstmt.executeQuery(); // 展开结果集数据库 while (resultSet.next()) { // 通过字段检索a roleTypeList= new RoleTypeList(); roleTypeList.setRoleType(resultSet.getInt( "RoleType" )); roleTypeList.setRoleTypeName(resultSet.getString( "RoleTypeName" )); roleTypeList.setRoleTypeDescribe(resultSet.getString( "RoleTypeDescribe" )); } } catch (SQLException exception) { exception.printStackTrace(); } return roleTypeList; } } |
测试:
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 | // public static void main(String[] args) { DuMsSqlConn msqlcon= new DuMsSqlConn(); RoleTypeList info= new RoleTypeList(); // info.setRoleTypeName("geovindu"); // info.setRoleTypeDescribe("测试"); // info.setRoleTypeName("涂聚文"); // info.setRoleTypeDescribe("测试2"); //info.setRoleType(14); // info.setRoleTypeName("江西涂"); // info.setRoleTypeDescribe("江333 "); // int ok=msqlcon.AddSQL(info); //int ok=msqlcon.AddProc(info); OutValue outValue= new OutValue(); //int ok=msqlcon.AddProcOut(info,outValue); //int ok=msqlcon.UpdateSQL(info); //int ok=msqlcon.UpdateProc(info); //int ok=msqlcon.DeleteSQL(15); //int ok=msqlcon.DeleteProc(14); //info=msqlcon.selectIdSql(1); info=msqlcon.selectIdProcl( 1 ); int ok= 1 ; if (ok> 0 ) { System.out.println( "add ok" +outValue.getIntValue()+info.getRoleTypeName()); } else { System.out.println( "add no" +outValue.getIntValue()); } ArrayList<RoleTypeList> list= new ArrayList<RoleTypeList>(); //list= msqlcon.selectSqlAll(); list= msqlcon.selectProclAll(); for (RoleTypeList roleTypeList:list) { System.out.println( "id:" + +roleTypeList.getRoleType()+ " Name:" +roleTypeList.getRoleTypeName()+ " Decs:" +roleTypeList.getRoleTypeDescribe()); } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Java
标签:
java
, sql server
【推荐】国内首个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帮你做增删改查!!
2017-12-25 postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)