简体   繁体   English

如何使用cmd中的java -D连接到SQL Server

[英]How to connect to SQL server with java -D from cmd

I want to run a java program that will connect to a SQL server. 我想运行一个将连接到SQL Server的Java程序。 The credentials to that server I would like to get from cmd by using the -D flag. 我想通过使用-D标志从cmd获取该服务器的凭据。

Could someone write how do I exactly do it and how do I retrieve those properties (System.getProperty()??? and what is the parameter I need to write in). 有人可以写出我该怎么做以及如何检索那些属性(System.getProperty()???以及我需要写的参数是什么)。

To learn more about JDBC connections, here you find a quite good tutorial: http://www.tutorialspoint.com/jdbc/jdbc-create-database.htm 要了解有关JDBC连接的更多信息,请在此处找到一个不错的教程: http : //www.tutorialspoint.com/jdbc/jdbc-create-database.htm

Re -D, taking the example from the link above, you simply replace Re -D,以上面的链接为例,您只需替换

static final String USER = "username";
static final String PASS = "password";

with

static final String USER = System.getProperty("username");
static final String PASS = System.getProperty("password");

Then you can pass the credentials as in 然后,您可以像下面那样传递凭据

java -jar -Dusername=<username> -Dpassword=<password> the.jar

Another note: Passing credentials via command line is insecure, because they are also fully visible in the process list. 另一个注意事项:通过命令行传递凭据是不安全的,因为它们在进程列表中也完全可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM