JAVA基础 🔪

JDBC连接数据库

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;

👏 怎么样,给个评价呗?