使用SSH连接mysql数据库
2017-05-15 11:33
260 查看
在项目中遇见了使用ssh连接数据库的问题,在网上查过资料后写了如下的测试代码
需要先导入jar包 jsch-0.1.51.jar
1.测试ssh连接mysql数据库
2.在项目中使用ssh连接mysql数据库
(1)配置properties:
(2)实现util:
(3)测试类测试数据库连接状态及增删改查:
3.测试通过则说明连接成功
需要先导入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.测试通过则说明连接成功
相关文章推荐
- 使用Navicat进行ssh通道连接MySQL数据库
- Android直接使用JDBC+SSH连接外网MySQL数据库
- 使用Navicat进行ssh通道连接MySQL数据库
- Windows平台使用Navicat for MySQL通过SSH密钥认证连接远程MySQL数据库
- 使用mysql.data.mysqlclient实现C#连接MySQL数据库
- 使用.Net程序连接MySql数据库时,请注意你的License
- 使用ADO连接mysql数据库
- 如何使用Oracle sql developer连接Mysql数据库
- 使用JDBC连接MYSQL数据库
- windows下使用SSH连接到ubuntu服务器
- 使用Hibernate连接MySQL数据库发生closed.Connection问题的一种解决方法
- 使用JDBC连接MYSQL数据库
- 使用 SSH 远程连接运行图形界面程序(ubuntu)
- Java 使用SSH连接Linux 使用passwd命令修改密码
- 使用ssh反向连接访问内网linux机器
- 在VC6中使用c API方式连接MySQL数据库
- perl连接SSH的代码,RH AS3,Perl 5.8时使用的
- 使用Perl连接Mysql数据库
- Tomcat中使用数据源连接mysql数据库
- 使用plink以及ssh转发连接到局域网内的远程桌面或其它端口