java: MS Sql Server Connection using JDK 14.02

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
/*
 * 版权所有 2021 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * IDE: Eclipse IDE for Enterprise Java and Web Developers - 2021-09
 * 历史版本: JDK 14.0.2
 * 2021-12-12 创建者 geovindu
 * 2021-12-15 添加 Lambda
 * 2021-12-15 修改:date
 * 接口类 mssql-jdbc-9.4.1.jre16.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 java.sql.*;
import com.microsoft.*;
 
 
 
 
 
public class DuMsSQLConn {
     
    Connection con;
    Statement stmt = null;
    //
    public Connection getConnection()
    {
         
          //数据库地址
          String url = "jdbc:sqlserver://GEOVINDU;DataBaseName=geovindu";  //GEOVINDU  //localhost
          //
          String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"//com.mysql.cj.jdbc.Driver  //com.mysql.jdbc.Driver
          //
          String userName = "sa";
          //
          String password = "7涂聚文";
 
          try {
              Class.forName(driverName);
              System.out.println("Ms SQL 数据库驱动加载成功");
          } catch
          (ClassNotFoundException e)
          {
              e.printStackTrace();
          }
           
          try
          {
              //1
              String connectionUrl = "jdbc:sqlserver://GEOVINDU;databaseName=geovindu;user=sa;password=涂聚文";             
              con = DriverManager.getConnection(connectionUrl);
              //2
             // con = DriverManager.getConnection(url,userName,password);
              System.out.println("Ms SQL数据库连接成功");
              System.out.println(" 实例化Statement对象...");       // 执行查询
              stmt = con.createStatement();
              String sql;
              sql = "SELECT RoleType,RoleTypeName,RoleTypeDescribe  FROM RoleTypeList";
              ResultSet rs = stmt.executeQuery(sql);
           
              // 展开结果集数据库
              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;
     
    }
     
 
}
     
     
    

  

 

 

 

 

 

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
/*
 * 版权所有 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 java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.microsoft.*;
 
 
public class DuMsSqlConn {
 
 
    Connection con;
    Statement stmt = null;
    //
    public Connection getConnection()
    {
 
        //数据库地址
        //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";
        //
        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 = "7涂聚文";
 
        try {
            Class.forName(driverName);
            System.out.println("Ms SQL 数据库驱动加载成功");
        } catch
        (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
 
        try
        {
 
            //1
            String connectionUrl = "jdbc:sqlserver://GEOVINDU;databaseName=geovindu;user=sa;password=7涂聚文";
            con = DriverManager.getConnection(connectionUrl);
            //2 第二种方法
            //con = DriverManager.getConnection(url,userName,password);
            System.out.println("Ms SQL数据库连接成功");
 
            stmt = con.createStatement();
            String sql;
            sql = "SELECT RoleType,RoleTypeName,RoleTypeDescribe  FROM RoleTypeList";
            ResultSet rs = stmt.executeQuery(sql);
 
            // 展开结果集数据库
            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;
 
    }
 
 
}

  

1.安装好IDE,先把文件编码格式设置好,UTF-8,开发工具,服务,数据库等的对象,都统一协调于unicode,utf-8格式,否则占双字节的文字乱码。
2.要考虑IDE,tomcat等支持的jdk的版本范围
3.不同IDE支持引用的JAR,JDK版本支持也不一样。
4.字体文件格式、字体名称也是要考虑的,平台系统和客户端浏览器的不同要程序代码上考虑的其统一
(暂时遇到问题)

已是选择“自然科学”的“自然属生”,还是选择“社会科学”中的“社会属性”-- 地“活着”?还是“感性”地选择整合“两性”的优越性?问题“概念”都未懂?会不会神经质问题?...

 

 

posted @   ®Geovin Du Dream Park™  阅读(52)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2011-12-18 User select font-face/color/size/backgroundColor设置 字体,颜色,大小,背景色兼容主流浏览器
2010-12-18 Pure CSS folded-corner effect(IE8以上,其他瀏覽器可以)
2010-12-18 Pure CSS speech bubbles(IE7以下無效,其他瀏覽器有效)
2010-12-18 Javascript Context Menu Ex - dhtml popup menu mode
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示