1.记得要启动oracle监听服务
2.代码如下(网上抄的,顺手拿来改了下,并测试成功!做个记忆。)
package oraclejdbc;
import java.sql.*;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class oraclejdbc {
public static void go() {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("root", "192.168.1.166", 22);
session.setPassword("hzx");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
System.out.println(session.getServerVersion());//
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection conn =null;
try{
System.out.println("=============");
go();
System.out.println("===");
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.166:1521:orcl","test","test");
getData(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void getData(Connection conn) throws SQLException {
// 获取所有表名
Statement statement = conn.createStatement();
ResultSet resultSet = statement
.executeQuery("select * from b");
// 获取列名
while(resultSet.next()){ //10.遍历结果集
System.out.println("人员编号:"+resultSet.getString("id"));
}
statement.close();
conn.close();
}
}