您的位置:首页 > 数据库 > MySQL

使用SSH连接mysql数据库

2017-05-15 11:33 260 查看
  在项目中遇见了使用ssh连接数据库的问题,在网上查过资料后写了如下的测试代码
      需要先导入jar包   jsch-0.1.51.jar

   1.测试ssh连接mysql数据库



    2.在项目中使用ssh连接mysql数据库

       (1)配置properties:

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:5393/cm?useUnicode\=true&characterEncoding\=utf8
username=root
passwd=123456

lport=5393
rhost=192.16.101.122
rport=5393
sshusername=admin
sshpasswd=pwd12345
sshhost=192.16.101.111
sshport=22


      (2)实现util:

package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.util.LogUtil;

public class SshmysqlUtil {
//定义变量
private static Connection ct = null;
//大多数情况下用preparedstatement替代statement
private static PreparedStatement ps = null;
private static ResultSet rs = null;

//连接数据库的参数
private static String url = "";
private static String username = "";
private static String driver = "";
private static String passwd = "";

//ssh参数
private static String sshusername = "";
private static String sshpasswd = "";
private static String sshhost = "";
private static int   sshport;
private static int lport;
private static String rhost;
private static int rport;

private static CallableStatement cs = null;
public static CallableStatement getCs()
{
return cs;
}
private static Properties  pp = null;
private static InputStream fis = null;
//加载驱动,只需要一次,用静态代码块
static
{
try
{
//从dbinfo.properties
pp = new Properties();
//fis=Mysqlutil.class.getClassLoader().getResourceAsStream("mysql.properties");
fis= SshmysqlUtil.class.getClassLoader().getResourceAsStream("mysql.properties");
//fis = new FileInputStream();
pp.load(fis);
url = pp.getProperty("url");
username = pp.getProperty("username");
driver = pp.getProperty("driver");
passwd = pp.getProperty("passwd");

sshusername=pp.getProperty("sshusername");
sshpasswd=pp.getProperty("sshpasswd");
sshhost=pp.getProperty("sshhost");
sshport=Integer.parseInt(pp.getProperty("sshport").toString());

lport=Integer.parseInt(pp.getProperty("lport").toString());
rhost=pp.getProperty("rhost");
rport=Integer.parseInt(pp.getProperty("rport").toString());
// System.out.println(username +"   "+ sshusername +"   "+ rhost  );
try {
JSch jsch = new JSch();
Session session = jsch.getSession(sshusername, sshhost, sshport);
// System.out.println("1");
session.setPassword(sshpasswd);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
//System.out.println("2");
System.out.println(session.getServerVersion());//这里打印SSH服务器版本信息
int assinged_port = session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
} catch (Exception e) {
e.printStackTrace();
}

Class.forName(driver);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{ fis.close();}
catch(IOException e) {e.printStackTrace();}
fis = null;//垃圾回收站上收拾
}

}
//得到连接
public static Connection getConnection()
{
try
{ct = DriverManager.getConnection(url,username,passwd);}
catch(Exception e) {e.printStackTrace();}
return ct;

/*try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
LogUtil.error("get Connection failed!:"+e.getMessage());
return null;
}*/
}

}


      (3)测试类测试数据库连接状态及增删改查:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.util.LogUtil;

public class Test {
public static void main(String[] args) {
Test t=new Test();
t.getNoState("1713006");
System.out.println(t.getNoState("1713006"));

}

public String getNoState(String number) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
StringBuffer ddl = new StringBuffer();
ddl.append("SELECT * FROM cm_subs_table WHERE servNumber=?");
conn = SshmysqlUtil.getConnection();
pstmt = conn.prepareStatement(ddl.toString());
pstmt.setString(1, mobileNo);
rs = pstmt.executeQuery();

if (rs != null) {
rs.first();

return rs.getString(1);
} else {
LogUtil.info("未查到号码:" + number);
return null;
}
} catch (Exception e) {
LogUtil.info("查询号码返回的流水:" + number + "错误信息:" + e.getMessage());
return null;
} finally {
try {
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}


      3.测试通过则说明连接成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql ssh 数据库