怎么实现用java远程访问oracle数据库

怎么实现用java远程访问oracle数据库,第1张

先期准备:

1、两台电脑必须要陵毁联网,能够访问到

2、将oracle的驱动程序拷贝到你的java的电脑上,并设置到环境变量中。

java代码

import java.sql.Connection

import java.sql.DriverManager

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

public class DbTest {

public static void main(String[] args) {

Connection conn = null

PreparedStatement prstm = null

ResultSet rs = null

try {

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()

String url="jdbc:oracle:thin:@ip:port:orcl" //数据库连接字符串,需要替换ip,port,orcl

String user="user" //用户名,需要替换你禅汪岩的数据库的用户名

String password="pswd" //密码,需要替换为对应的密码

conn = DriverManager.getConnection(url,user,password) //获取连接

prstm = conn.prepareStatement("SELECT 1 TEST_ID FROM DUAL")

rs = prstm.executeQuery()

while (rs.next()) {

System.out.println("查询结果为:" + rs.getInt("TEST_ID"))

}

} catch (InstantiationException e) {

/贺御/ TODO Auto-generated catch block

e.printStackTrace()

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} finally {

//使用完毕进行相关内容的关闭,注意相关顺序

if (rs != null) {

try {

rs.close()

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

if (prstm != null) {

try {

prstm.close()

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

if (conn != null) {

try {

conn.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

}

}

}

Java使用SSH远程访问Windows并执行命令

import java.io.BufferedReader

import java.io.IOException

import java.io.InputStream

import java.io.InputStreamReader

import ch.ethz.ssh2.Connection

import ch.ethz.ssh2.Session

import ch.ethz.ssh2.StreamGobbler

public class SSHWindows {

public static void main(String[] args) {

// TODO Auto-generated method stub

String hostname ="192.168.30.10"

String username="administrator"

String password="Talent123"

try{

/缓裂桐/建立连接

Connection conn= new Connection(hostname)

// System.out.println("set up connections")

conn.connect()

//利用用户名和密码进行授权

boolean isAuthenticated = conn.authenticateWithPassword(username, password)

if(isAuthenticated ==false)

{

// System.out.println("--------")

throw new IOException("Authorication failed")

}

//打开会话

Session sess = conn.openSession()

//System.out.println("cmd----")

//执行命令

sess.execCommand("ruby C:\\WhatWeb-master\\扰坦whatweb --output-xml http://216.139.147.75:443/")

// System.out.println("The execute command output is:"源戚)

InputStream stdout = new StreamGobbler(sess.getStdout())

BufferedReader br = new BufferedReader(new InputStreamReader(stdout))

while(true)

{

String line = br.readLine()

if(line==null) break

System.out.println(line)

}

// System.out.println("Exit code "+sess.getExitStatus())

sess.close()

conn.close()

// System.out.println("Connection closed")

}catch(IOException e)

{

System.out.println("can not access the remote machine")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12372897.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存