java jdbc使用SSH隧道连接mysql数据库demo

时间:2022-08-07 06:27:50

java jdbc使用SSH隧道连接mysql数据库demo

 
  1.  
    package com.yws.echo_socket;
  2.  
     
  3.  
     
  4.  
    import com.jcraft.jsch.JSch;
  5.  
    import com.jcraft.jsch.Session;
  6.  
     
  7.  
    import java.sql.*;
  8.  
     
  9.  
    //http://my.oschina.net/Thinkeryjgfn/blog/177283
  10.  
    //http://www.cnblogs.com/I-will-be-different/p/3925351.html?utm_source=tuicool&utm_medium=referral
  11.  
    //java jdbc使用SSH隧道连接mysql数据库demo
  12.  
    public class ChangeDB {
  13.  
     
  14.  
     
  15.  
     
  16.  
    public static void go() {
  17.  
    try {
  18.  
    JSch jsch = new JSch();
  19.  
    Session session = jsch.getSession("yunshouhu", "192.168.0.102", 22);
  20.  
    session.setPassword("xxxx");
  21.  
    session.setConfig("StrictHostKeyChecking", "no");
  22.  
    session.connect();
  23.  
    System.out.println(session.getServerVersion());//这里打印SSH服务器版本信息
  24.  
     
  25.  
    //ssh -L 192.168.0.102:5555:192.168.0.101:3306 yunshouhu@192.168.0.102 正向代理
  26.  
    int assinged_port = session.setPortForwardingL("192.168.0.101",5555, "192.168.0.101", 3306);//端口映射 转发
  27.  
     
  28.  
    System.out.println("localhost:" + assinged_port);
  29.  
     
  30.  
    //ssh -R 192.168.0.102:5555:192.168.0.101:3306 yunshouhu@192.168.0.102
  31.  
    //session.setPortForwardingR("192.168.0.102",5555, "192.168.0.101", 3306);
  32.  
    // System.out.println("localhost: -> ");
  33.  
    } catch (Exception e) {
  34.  
    e.printStackTrace();
  35.  
    }
  36.  
    }
  37.  
     
  38.  
     
  39.  
     
  40.  
    public static void main(String[] args) {
  41.  
     
  42.  
     
  43.  
    try {
  44.  
    //1、加载驱动
  45.  
    Class.forName("com.mysql.jdbc.Driver");
  46.  
    } catch (ClassNotFoundException e) {
  47.  
    e.printStackTrace();
  48.  
    }
  49.  
    //2、创建连接
  50.  
    Connection conn = null;
  51.  
    Connection conn2 = null;
  52.  
    try {
  53.  
     
  54.  
    conn2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "hadoop", "xxxx");
  55.  
    getData(conn2);
  56.  
    } catch (SQLException e) {
  57.  
    System.out.println("未连接上数据库");
  58.  
    e.printStackTrace();
  59.  
    }
  60.  
     
  61.  
    try{
  62.  
    System.out.println("=============");
  63.  
    go();
  64.  
    conn = DriverManager.getConnection("jdbc:mysql://192.168.0.101:5555/mysql", "hadoop", "xxx");
  65.  
    getData(conn);
  66.  
     
  67.  
     
  68.  
    } catch (SQLException e) {
  69.  
    e.printStackTrace();
  70.  
    }
  71.  
     
  72.  
     
  73.  
     
  74.  
    }
  75.  
     
  76.  
    private static void getData(Connection conn) throws SQLException {
  77.  
     
  78.  
    // 获取所有表名
  79.  
    Statement statement = conn.createStatement();
  80.  
    ResultSet resultSet = statement
  81.  
    .executeQuery("select * from help_keyword");
  82.  
    // 获取列名
  83.  
    ResultSetMetaData metaData = resultSet.getMetaData();
  84.  
    for (int i = 0; i < metaData.getColumnCount(); i++) {
  85.  
    // resultSet数据下标从1开始
  86.  
    String columnName = metaData.getColumnName(i + 1);
  87.  
    int type = metaData.getColumnType(i + 1);
  88.  
    if (Types.INTEGER == type) {
  89.  
    // int
  90.  
    } else if (Types.VARCHAR == type) {
  91.  
    // String
  92.  
    }
  93.  
    System.out.print(columnName + "\t");
  94.  
    }
  95.  
    System.out.println();
  96.  
    // 获取数据
  97.  
    while (resultSet.next()) {
  98.  
    for (int i = 0; i < metaData.getColumnCount(); i++) {
  99.  
    // resultSet数据下标从1开始
  100.  
    System.out.print(resultSet.getString(i + 1) + "\t");
  101.  
    }
  102.  
    System.out.println();
  103.  
     
  104.  
    }
  105.  
    statement.close();
  106.  
    conn.close();
  107.  
    }
  108.  
     
  109.  
    }

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
    <parent>
  6.  
    <artifactId>grpc-demo</artifactId>
  7.  
    <groupId>org.jiepu</groupId>
  8.  
    <version>1.0</version>
  9.  
    </parent>
  10.  
    <modelVersion>4.0.0</modelVersion>
  11.  
     
  12.  
    <artifactId>echo_socket</artifactId>
  13.  
     
  14.  
    <properties>
  15.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16.  
    <java.version>1.6</java.version>
  17.  
    </properties>
  18.  
     
  19.  
    <dependencies>
  20.  
     
  21.  
    <dependency>
  22.  
    <groupId>com.jcraft</groupId>
  23.  
    <artifactId>jsch</artifactId>
  24.  
    <version>0.1.53</version>
  25.  
    </dependency>
  26.  
    <dependency>
  27.  
    <groupId>mysql</groupId>
  28.  
    <artifactId>mysql-connector-java</artifactId>
  29.  
    <version>5.1.36</version>
  30.  
    </dependency>
  31.  
    </dependencies>
  32.  
    <build>
  33.  
    <plugins>
  34.  
    <plugin>
  35.  
    <groupId>org.apache.maven.plugins</groupId>
  36.  
    <artifactId>maven-compiler-plugin</artifactId>
  37.  
    <configuration>
  38.  
    <source>${java.version}</source>
  39.  
    <target>${java.version}</target>
  40.  
    </configuration>
  41.  
    </plugin>
  42.  
     
  43.  
    <plugin>
  44.  
    <groupId>org.apache.maven.plugins</groupId>
  45.  
    <artifactId>maven-shade-plugin</artifactId>
  46.  
    <executions>
  47.  
    <execution>
  48.  
    <phase>package</phase>
  49.  
    <goals>
  50.  
    <goal>shade</goal>
  51.  
    </goals>
  52.  
    <configuration>
  53.  
    <transformers>
  54.  
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  55.  
    <mainClass>com.yws.echo_socket.ChangeDB</mainClass>
  56.  
    </transformer>
  57.  
    </transformers>
  58.  
    </configuration>
  59.  
    </execution>
  60.  
    </executions>
  61.  
    </plugin>
  62.  
    </plugins>
  63.  
    </build>
  64.  
    </project>