In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Connect to a remote database through java's jsch
Sometimes our local code may not be able to connect to the database, because the database added a whitelist, specified ip server can connect to the database, but we can connect to the specified ip server, and then connect to the database through the server, just like many database visualization software. By using jsch, we can also do this at the code level. Don't say much, just open the code:
JSch jsch = new JSch (); Session sess; / / where xxx.xxx.xxx.xxx is the whitelisted remote server sess = jsch.getSession ("ubuntu", "xxx.xxx.xxx.xxx", 22); sess.setPassword ("*"); sess.setConfig ("StrictHostKeyChecking", "no"); sess.connect () / / here xxx.xxx.xxx.xxx is the database connection address / / through this set method, the remote port 3306 can be designated as the local port 3308. Since the author has used 3306 locally, it is designated as port 3308 sess.setPortForwardingL (3308, "xxx.xxx.xxx.xxx", 3306). Try {/ / reads the configuration file. Note that the database address in the configuration file is localhost, and the port is 3308 reader = Resources.getResourceAsReader ("conf.xml") that you just specified;} catch (IOException e) {e.printStackTrace ();} sqlSessionFactory = new SqlSessionFactoryBuilder (). Build (reader); SqlSession session = sqlSessionFactory.openSession () String statement = "com.saishangmingzhu.getData"; / / Mapping the identity string List ll=session.selectList (statement) of sql; System.out.println (ll); session.commit (); session.close (); sess.disconnect ()
Of course, the use of jsch to achieve ssh connection is not limited to database connection, but you can also operate on the server on the ssh connection, such as executing shell and other related commands, and record it later.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
You can see that each time it increases to 1, the specific operation plan:
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.