window docker 安装 mssql 服务并测试
发布日期:2021-06-30 16:23:24 浏览次数:2 分类:技术文章

本文共 4024 字,大约阅读时间需要 13 分钟。

执行官网命令

docker pull microsoft/mssql-server-linux

启动mssql 并指定密码yourPwd123456 

docker run --name mssql -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourPwd123456" -p 1433:1433 microsoft/mssql-server-linux

重启mssql服务

docker restart mssql

进入mssql容器测试创建数据库

docker exec -it mssql /bin/bash

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA    这条需要手动输入密码   yourPwd123456

1> CREATE DATABASE TestDB2> GO1> USE TestDB2> CREATE TABLE t_user(id INT IDENTITY(1,1),name NVARCHAR(50),age INT)3> INSERT INTO t_user VALUES(1,'abc',12)4> INSERT INTO t_user VALUES(2,'aba',22)5> GOChanged database context to 'TestDB'.(1 rows affected)(1 rows affected)1> SELECT * FROM t_user2> GOid          name                                               age----------- -------------------------------------------------- -----------          1 abc                                                         12          2 aba                                                         22(2 rows affected)1> QUIT

使用Navicat Premium连接SQL Server

此处需先安装sqlncli_x64.msi , 已安装可忽略,程序位置在navicat安装目录

测试

IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[t_user]') AND type IN ('U'))	DROP TABLE [dbo].[t_user]GOCREATE TABLE [dbo].[t_user] (  [id] int IDENTITY(1,1) NOT NULL,  [name] nvarchar(50) NOT NULL,  [age] int NULL)GO

SET IDENTITY_INSERT [dbo].[t_user] ONGOINSERT INTO [dbo].[t_user] ([id], [name], [age]) VALUES (N'1', N'aaa', N'11')GOINSERT INTO [dbo].[t_user] ([id], [name], [age]) VALUES (N'2', N'bbb', N'12')GOINSERT INTO [dbo].[t_user] ([id], [name], [age]) VALUES (N'3', N'ccc', N'13')GOSET IDENTITY_INSERT [dbo].[t_user] OFFGO

使用java测试连接

mssql.driver.class=com.microsoft.sqlserver.jdbc.SQLServerDrivermssql.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=TestDBmssql.user=samssql.password=yourPwd123456
package com.mssql.jdbc.test;import java.io.IOException;import java.io.InputStream;import java.sql.*;import java.util.Properties;public class MssqlJDBCTest {    public static void main(String[] args) {        //1.读取配置文件中的4个基本信息        InputStream is =  MssqlJDBCTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        Properties properties = new Properties();        try {            properties.load(is);        } catch (IOException e) {            e.printStackTrace();        }        String driverClass = properties.getProperty("mssql.driver.class");        String url = properties.getProperty("mssql.url");        String user = properties.getProperty("mssql.user");        String password = properties.getProperty("mssql.password");        //2.加载驱动        try {            Class.forName(driverClass);        } catch (ClassNotFoundException e) {            e.printStackTrace();        }        //3.获取连接        Connection conn = null;        try {            conn = DriverManager.getConnection(url, user, password);        } catch (SQLException throwables) {            throwables.printStackTrace();        }        //4获取预加载处理器,并查询        String sql = "SELECT * FROM t_user";        PreparedStatement preparedStatement = null;        try {            preparedStatement = conn.prepareStatement(sql);            //获取查询结果集            ResultSet resultSet = preparedStatement.executeQuery();            while(resultSet.next()){                int id = resultSet.getInt(1);                String name = resultSet.getString(2);                int age = resultSet.getInt(3);                System.out.println(id + "," + name + "," + age);            }        } catch (SQLException throwables) {            throwables.printStackTrace();        }finally {            //关闭连接            if(preparedStatement !=null){                try {                    preparedStatement.close();                } catch (SQLException throwables) {                    throwables.printStackTrace();                }            }            if(conn != null){                try {                    conn.close();                } catch (SQLException throwables) {                    throwables.printStackTrace();                }            }        }    }}

 

转载地址:https://kevin.blog.csdn.net/article/details/116895099 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java.io.EOFException: Unexpected EOF read on the socket
下一篇:m * n 二维地图,数字1是指定区域边界,统计边界内0的个数

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月16日 08时32分26秒