java: DAL using SQL Server

---角色類型表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:

 

/*
 * 版權所有 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;
    }




}

  

測試:

//
    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());
        }


    }

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章