JAVA基础 🔪

JDBC连接数据库

[livicon_evo name="bomb.svg" style="original" size="60px" stroke_style="original" stroke_width="original" try_to_sharpen="true" rotate="none" flip_horizontal="false" flip_vertical="false" stroke_color="#22A7F0" stroke_color_action="#b3421b" stroke_color_alt="#F9B32F" stroke_color_alt_action="#ab69c6" fill_color="#91e9ff" fill_color_action="#ff926b" solid_color="#6C7A89" solid_color_action="#4C5A69" solid_color_bg="#ffffff" solid_color_bg_action="#ffffff" colors_on_hover="none" colors_hover_time="0.3" colors_when_morph="none" brightness="0.1" saturation="0.07" morph_state="start" morph_image="none" allow_morph_image_transform="false" stroke_width_factor_on_hover="none" stroke_width_on_hover_time="0.3" keep_stroke_width_on_resize="false" animated="true" event_type="hover" event_on="self" auto_play="false" delay="0" duration="default" repeat="default" repeat_delay="default" draw_on_viewport="false" viewport_shift="oneHalf" draw_delay="0" draw_time="1" draw_stagger="0.1" draw_start_point="middle" draw_color="same" draw_color_time="1" draw_reversed="false" draw_ease="Power1.easeOut" erase_delay="0" erase_time="1" erase_stagger="0.1" erase_start_point="middle" erase_reversed="true" erase_ease="Power1.easeOut" touch_events="false"][/livicon_evo]

public class JDBCTest {
    public static void main(String[] args) {
        String url = "jdbc:sqlserver://db.host.com;database=education";
        String username = "SA";
        String password = "PassWord";
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url,username,password);
            Statement sql = conn.createStatement();
            ResultSet result = sql.executeQuery("select * from users");
            while (result.next()) {
                var id = result.getInt(1);
                var name = result.getString(2);
                var pass = result.getString(3);
                System.out.println(id + "\t" + name + "\t" + pass);
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }
}

Sql Server数据备份还原

docker安装的Sql Server数据默认位置为/var/opt/mssql/data

  1. 拷贝容器名为mssql的Sql Server应用中的book数据库的数据和日志文件到/tmp目录

    docker cp mssql:/var/opt/mssql/data/book.mdf /tmp/
    docker cp mssql:/var/opt/mssql/data/book_log.ldf /tmp/
  2. 还原数据库,数据库文件和日志文件在数据库服务器中

    create database book on 
    (filename='/var/opt/mssql/data/book.mdf'),
    (filename='/var/opt/mssql/data/book_log.ldf')
    for attach;

👏 怎么样,给个评价呗?