标签:mysql imp exce manager new craft try print 链接
很多朋友都遇到了jmeter链接数据库时需要ssh连服务器后才能连数据库,分享下beanshell里ssh+jdbc用法。
import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import java.sql.*; String user = "";//SSH连接用户名 String password = "";//SSH连接密码 String host = "";//SSH服务器 int port = ;//SSH访问端口 try { JSch jsch = new JSch(); session = jsch.getSession(user, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); System.out.println(session.getServerVersion());//这里打印SSH服务器版本信息 int assinged_port = session.setPortForwardingL(3376,"mysqlip" ,mysqlport); //3376就是jdbc要用的端口 System.out.println("localhost:" + assinged_port + " -> " + mysqlip + ":" + mysqlport); } catch (Exception e) { e.printStackTrace(); }; Connection conn = null; ResultSet rs = null; Statement st = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3376/xhl_guild", "root", "12346"); //这里就写localhost或者127.0.0.1 st = conn.createStatement(); String sql = "SELECT user_id,u_nickname from base_user where user_id =2;"; rs = st.executeQuery(sql); while (rs.next()){ System.out.println(rs.getInt(1)+"\t"+rs.getString(2));} } catch (Exception e) { e.printStackTrace(); } rs.close(); st.close(); conn.close(); session.disconnect()
标签:mysql imp exce manager new craft try print 链接
原文地址:https://www.cnblogs.com/VashGu927/p/9606608.html