腾讯云上搭建MySql并且通过SSH通道使用Java来操作数据库的解决方案

因为接下来要做几个带数据库的小软件玩玩,就准备搞个数据库玩玩,但是一路肥肠的艰辛啊(汗

 

1.申请腾讯云

 

2.安全组那边要打开端口,详情看设置里,通俗易懂,我们需要的就是22和3306

 

3.安装mysql

https://blog.csdn.net/weixx3/article/details/80782479

跟我一样是Ubuntu18.04

 

4.远程看一下数据库:

如果遇到问题https://blog.csdn.net/homewm/article/details/81316852

(我用的是Navicat,软件很强大)

 

5.使用jdbc通过ssh通道连接mysql并且进行操作:

参考https://stackoverflow.com/questions/1968293/connect-to-remote-mysql-database-through-ssh-using-java

果然国外的大佬多。。

 

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.*;

public class UpdateMySqlDatabase {
    static int lport;
    static String rhost;
    static int rport;
    public static void go(){
        String user = "****";     //云服务器账号密码
        String password = "****";
        String host = "****"; //在这填写你的云服务器IP
        int port=22;
        try
            {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            lport = 22;   //Liunx都是走22
            rhost = "localhost"; //因为是虚拟机上的数据库
            rport = 3306; //数据库指定端口
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            System.out.println("Establishing Connection...");
            session.connect();
            int assinged_port=session.setPortForwardingL(lport, rhost, rport);
            System.out.println("localhost:"+assinged_port+" -> "+rhost+":"+rport);
            }
        catch(Exception e){System.err.print(e);}
        
        }
    public static void main(String[] args) {
        try{
            go();
        } catch(Exception ex){
            ex.printStackTrace();
        }

        
          Connection con = null;
          String driver = "com.mysql.cj.jdbc.Driver";
          String url = "jdbc:mysql://" + rhost +":" + lport + "/";
          String db = "****"; //分别是数据库名,数据库的用户和密码
          String dbUser = "***";
          String dbPasswd = "****"; 
          
          
          
          try{
          Class.forName(driver);
          con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
          try{
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from hero");
          while (rs.next()) {
              int id = rs.getInt("id");// 可以使用字段名
              String name1 = rs.getString(2);// 也可以使用字段的顺序
              float hp2 = rs.getFloat("hp");
              int damage3 = rs.getInt(4);
              System.out.printf("%d\t%s\t%f\t%d%n", id, name1, hp2, damage3);   //这段可以修改成你自己的数据结构,我这的数据结果见输出
          }
          }
          catch (SQLException s){
          System.out.println("SQL statement is not executed!");
          }
          }
          catch (Exception e){
          e.printStackTrace();
          }
          


//还没写自动关闭连接的代码,偷个懒 } }

输出如下:

335    大英雄1    194.755997    293
336    大英雄2    219.292999    188
337    大英雄3    821.286987    322
338    大英雄4    225.434006    204
339    大英雄5    140.785004    425
345    提莫    313.000000    50
346    提莫    313.000000    50
347    提莫    313.000000    50
348    提莫    313.000000    50
360    sdasd    23.000000    55
361    test    90.000000    60

 

posted @ 2020-04-26 17:00  Parachute黑喵  阅读(403)  评论(0编辑  收藏  举报