如何在IntelliJ IDEA中使用SSH连接Oracle数据库

huangapple go评论97阅读模式
英文:

How to connent to Oracle database with SSH in IntelliJ IDEA

问题

以下是您要翻译的内容:

我正在尝试使用SSH配置数据源,以便从校外的IntelliJ访问数据库。配置如屏幕截图所示,我遇到了以下错误:

[08006][17002] IO错误:从读取调用中获得负一,连接持续时间30003毫秒,认证持续时间0毫秒。oracle.net.ns.NetException:从读取调用中获得负一。

实际上,我已经成功地在Java程序中使用loginProxy()loginDB()连接到数据库。通过运行代码,我知道jdbcPort应该是动态的,我认为这也应该填写在“数据源和驱动程序”配置窗口中“常规”选项卡中的“端口”空白处。


问题来了,如果要填写的端口是动态的,我该如何进行配置?或者我是否有什么误解,实际上应该有另一种方法?

另外一个问题:String URL = "jdbc:oracle:thin:@" + jdbcHost + ":" + jdbcPort + "/" + database; 这里使用了什么URL格式?它看起来既不像SID、服务名,也不像TNS,但它却起作用了……有趣的是,当我将"/"替换为":",与SID格式匹配时,它就不起作用了。

	/**
	 * 登录代理。请勿更改此函数。
	 * 
	 * @return boolean
	 */
	public boolean loginProxy() {
		if (getYESorNO("是否使用SSH隧道?")) { // 如果使用SSH隧道
			String[] namePwd = getUsernamePassword("登录cs实验室计算机");
			String sshUser = namePwd[0];
			String sshPwd = namePwd[1];
			try {
				proxySession = new JSch().getSession(sshUser, proxyHost, proxyPort);
				proxySession.setPassword(sshPwd);
				Properties config = new Properties();
				config.put("StrictHostKeyChecking", "no");
				proxySession.setConfig(config);
				proxySession.connect();
				proxySession.setPortForwardingL(forwardHost, 0, databaseHost, databasePort);
				forwardPort = Integer.parseInt(proxySession.getPortForwardingL()[0].split(":")[0]);
                // forwardPort在这里设置,似乎是动态的...
			} catch (JSchException e) {
				e.printStackTrace();
				return false;
			}
			jdbcHost = forwardHost;  // 在SSH连接情况下使用,即"localhost"
			jdbcPort = forwardPort;  // 在SSH连接情况下使用
		} else {
			jdbcHost = databaseHost;
			jdbcPort = databasePort;
		}
		return true;
	}
	/**
	 * 登录Oracle系统。根据指示更改此函数。
	 * 
	 * @return boolean
	 */
	public boolean loginDB() {
		String username = "我的数据库用户名";
		String password = "我的数据库密码";
		
		/* 请勿更改以下代码 */

		String URL = "jdbc:oracle:thin:@" + jdbcHost + ":" + jdbcPort + "/" + database;

		try {
			System.out.println("正在登录 " + URL + " ...");
			conn = DriverManager.getConnection(URL, username, password);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
英文:

I'm trying to configure the data source using SSH to access the Database from IntelliJ off-campus. The configuration is as shown in the screenshots, and I got
>[08006][17002] IO Error: Got minus one from a read call, connect lapse 30003 ms., Authentication lapse 0 ms. oracle.net.ns.NetException: Got minus one from a read call.

In fact, I've succeeded to connect to the DB with the loginProxy() and loginDB() in a Java program. From running the code, I knew that the jdbcPort should be dynamic, and I assume that's also what should be filled in the "Port" blank in the "General" tab in "Data Source and Drivers" configuration window.


So here comes the problem, how can I configure it if the Port to be filled in is DYNAMIC? Or did I get anything wrong so that actually there should be another approach?

An additional question: String URL = "jdbc:oracle:thin:@" + jdbcHost + ":" + jdbcPort + "/" + database; What URL format is used here? It doesn't look like SID, Service Name, or TNS, but it does work... and it's funny that when I substitute the "/" with ":", which matches the SID format, it doesn't work anymore...

	/**
	 * Login the proxy. Do not change this function.
	 * 
	 * @return boolean
	 */
	public boolean loginProxy() {
		if (getYESorNO("Using ssh tunnel or not?")) { // if using ssh tunnel
			String[] namePwd = getUsernamePassword("Login cs lab computer");
			String sshUser = namePwd[0];
			String sshPwd = namePwd[1];
			try {
				proxySession = new JSch().getSession(sshUser, proxyHost, proxyPort);
				proxySession.setPassword(sshPwd);
				Properties config = new Properties();
				config.put("StrictHostKeyChecking", "no");
				proxySession.setConfig(config);
				proxySession.connect();
				proxySession.setPortForwardingL(forwardHost, 0, databaseHost, databasePort);
				forwardPort = Integer.parseInt(proxySession.getPortForwardingL()[0].split(":")[0]);
                // 👆 forwardPort is set here, seems to be dynamic...
			} catch (JSchException e) {
				e.printStackTrace();
				return false;
			}
			jdbcHost = forwardHost;  // 👈 this is used in case of SSH connection, which is "localhost"
			jdbcPort = forwardPort;  // 👈 this is used in case of SSH connection
		} else {
			jdbcHost = databaseHost;
			jdbcPort = databasePort;
		}
		return true;
	}
	/**
	 * Login the oracle system. Change this function under instruction.
	 * 
	 * @return boolean
	 */
	public boolean loginDB() {
		String username = "myDBUsername";
		String password = "myDBPassword";
		
		/* Do not change the code below */

		String URL = "jdbc:oracle:thin:@" + jdbcHost + ":" + jdbcPort + "/" + database;

		try {
			System.out.println("Logging " + URL + " ...");
			conn = DriverManager.getConnection(URL, username, password);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

如何在IntelliJ IDEA中使用SSH连接Oracle数据库

如何在IntelliJ IDEA中使用SSH连接Oracle数据库

答案1

得分: 1

在常规选项卡上,您需要指定真实的数据库服务器主机名和端口,而不是localhost。在下一个选项卡上配置了SSH隧道后,所有连接方面的事情都将自动完成。

英文:

On general tab you need to specify real db server hostname and port, not localhost. With configured SSH tunnel on next tab all the things for connection will be done automatically.

huangapple
  • 本文由 发表于 2020年4月10日 03:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定