JSch学习笔记

article/2025/10/29 11:43:12

JSch笔记

第 1 章 JSch简介

1.1 简述

1)jsch是ssh2的一个纯Java实现。它允许你连接到一个sshd服务器,使用端口转发、X11转发、文件传输等。

2)SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。

3)ftp协议通常是用来在两个服务器之间传输文件的,但是它本质上是不安全的。

4)SFTP可以理解为SSH + FTP,也就是安全的网络文件传输协议。

1.2 实现原理

Jsch进行服务器连接时可以看作java的jdbc连接,首先需要实例化一个jsch对象,再利用这个对象根据用户名,主机ip,端口获取一个Session对象,设置好相应的参数后,进行连接,创建连接后,这个session时一直可用的,所以不需要关闭。之后我们需要在session上建立channel通道。

1.3 JSch官网

http://www.jcraft.com/jsch/

1.4 导入依赖

<!--    jsch	--><dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.54</version></dependency>

1.5 Channel的常用类型

在这里插入图片描述

1)Channel的常用类型有 ChannelShell 、 ChannelExec 和 ChannelSftp

2)ChannelShell和ChannelExec区别:

​ ChannelShell可以看作是执行一个交互式的Shell,而ChannelExec是执行一个Shell脚本。

3)ChannelSftp对象实现文件上传下载,ChannelSftp类是Jsch实现SFTP核心类,它包含了所有SFTP的方法

方法操作
put()文件上传
get()文件下载
cd()进入指定目录
ls()得到指定目录下的文件或目录
rename()重命名指定文件或目录
rm()删除指定文件
mkdir()创建目录
rmdir()删除目录

第 2 章 Demo

2.1 SSH连接

# 说明
当前Demo可用作判断是否可以和该主机ip通信

JSchConnect.java

package com.ix.utils;import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Properties;public class JSchConnect {private static final Logger logger = LoggerFactory.getLogger(JSchConnect.class);public static void main(String[] args) {String username = "root";String password = "123456";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;boolean reulst = false;try {// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 存放主机密码jSchSession.setPassword(password);Properties config = new Properties();// 去掉首次连接确认config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();// 获取连接结果reulst = jSchSession.isConnected();} catch (JSchException e) {logger.warn(e.getMessage());} finally {// 关闭jschSesson流if (jSchSession != null && jSchSession.isConnected()) {jSchSession.disconnect();}}if (reulst) {logger.error("【SSH连接】连接成功");} else {logger.error("【SSH连接】连接失败");}}
}

2.2 文件上传

Jsch支持三种文件传输模式

OVERWRITE完全覆盖模式,JSch默认文件传输模式,如果目标文件已经存在,传输的文件将完全覆盖目标文件,产生新的文件
RESUME恢复模式,如果文件已经传输一部分,这时由于网络或其他任何原因导致文件传输终端,如果下次传输相同的文件,则会从上次终端的地方继续上传
APPEND追加模式,如果目标文件已经存在,传输的文件将在目标文件后追加。

实现文件上传可以调用ChannelSftp对象的put方法。ChannelSftp中有12个put方法的重载方法

public void put(String src, String dst)将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。 采用默认的传输模式:OVERWRITE
public void put(String src, String dst, int mode)将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。 指定文件传输模式为mode(mode可选值为:ChannelSftp.OVERWRITE,ChannelSftp.RESUME, ChannelSftp.APPEND)
public void put(String src, String dst, SftpProgressMonitor monitor)将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。 采用默认的传输模式:OVERWRITE 并使用实现了SftpProgressMonitor接口的monitor对象来监控文件传输的进度。
public void put(String src, String dst, SftpProgressMonitor monitor, int mode)将本地文件名为src的文件上传到目标服务器,目标文件名为dst,若dst为目录,则目标文件名将与src文件名相同。 指定传输模式为mode 并使用实现了SftpProgressMonitor接口的monitor对象来监控文件传输的进度。
public void put(InputStream src, String dst)将本地的input stream对象src上传到目标服务器,目标文件名为dst,dst不能为目录。 采用默认的传输模式:OVERWRITE
public void put(InputStream src, String dst, int mode)将本地的input stream对象src上传到目标服务器,目标文件名为dst,dst不能为目录。 指定文件传输模式为mode
public void put(InputStream src, String dst, SftpProgressMonitor monitor)将本地的input stream对象src上传到目标服务器,目标文件名为dst,dst不能为目录。 采用默认的传输模式:OVERWRITE 并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。
public void put(InputStream src, String dst, SftpProgressMonitor monitor, int mode)将本地的input stream对象src上传到目标服务器,目标文件名为dst,dst不能为目录。 指定文件传输模式为mode 并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。
public OutputStream put(String dst)该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dst,dst不能为目录。 采用默认的传输模式:OVERWRITE
public OutputStream put(String dst, final int mode)该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dst,dst不能为目录。 指定文件传输模式为mode
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode)该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dst,dst不能为目录。 指定文件传输模式为mode 并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode, long offset)该方法返回一个输出流,可以向该输出流中写入数据,最终将数据传输到目标服务器,目标文件名为dst,dst不能为目录。 指定文件传输模式为mode 并使用实现了SftpProgressMonitor接口的monitor对象来监控传输的进度。 offset指定了一个偏移量,从输出流偏移offset开始写入数据。
package com.ix.utils;import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Properties;public class JSchUpload {private static final Logger logger = LoggerFactory.getLogger(JSchUpload.class);public static void main(String[] args) {String username = "root";String password = "123456";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;ChannelSftp chSftp = null;try {// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 存放主机密码jSchSession.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();// 打开SFTP通道chSftp = (ChannelSftp)jSchSession.openChannel("sftp");// 建立SFTP通道的连接chSftp.connect();// 设置编码格式chSftp.setFilenameEncoding("UTF-8");/*** 说明:* 1、当前文件上传信息没有任何反馈,如果没有异常则代表成功* 2、如果需要判断是否读取成功的进度,可参考https://blog.csdn.net/coding99/article/details/52416373?locationNum=13&fps=1* 3、将src文件上传到dst路径中*/chSftp.put("D:/node.txt", "/root/2.txt");logger.info("文件上传成功");} catch (JSchException | SftpException  e) {logger.warn(e.getMessage());} finally {// 关闭sftpChannelif (chSftp != null && chSftp.isConnected()) {chSftp.quit();}// 关闭jschSesson流if (jSchSession != null && jSchSession.isConnected()) {jSchSession.disconnect();}}}
}

2.3 文件下载

# 说明
当前Demo适用场景:对指定虚拟机下载一个文件
package com.ix.utils;import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Properties;public class JSchDownload {private static final Logger logger = LoggerFactory.getLogger(JSchDownload.class);public static void main(String[] args) {String username = "root";String password = "123456";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;ChannelSftp chSftp = null;try {// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 存放主机密码jSchSession.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();// 打开SFTP通道chSftp = (ChannelSftp)jSchSession.openChannel("sftp");// 建立SFTP通道的连接chSftp.connect();// 设置编码格式chSftp.setFilenameEncoding("UTF-8");/*** 说明:* 1、当前上读取文件信息没有任何反馈,如果没有异常则代表成功* 2、如果需要判断是否读取成功的进度,可参考https://blog.csdn.net/coding99/article/details/52416373?locationNum=13&fps=1* 3、将src文件下载到dst路径中*/chSftp.get("/root/2.txt", "D:/node.txt");logger.info("文件下载成功");} catch (JSchException | SftpException e) {logger.warn(e.getMessage());} finally {// 关闭sftpChannelif (chSftp != null && chSftp.isConnected()) {chSftp.quit();}// 关闭jschSesson流if (jSchSession != null && jSchSession.isConnected()) {jSchSession.disconnect();}}}
}

2.4 执行shell命令

package com.ix.utils;import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.BufferedReader;
import java.io.InputStream;
import java.util.Properties;public class JSchExecShell {private static final Logger logger = LoggerFactory.getLogger(JSchExecShell.class);public static void main(String[] args) {String username = "root";String password = "123456";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;Channel jschChannel = null;// 存放执行命令结果StringBuffer result = new StringBuffer();int exitStatus = 0;String command = "ls -l";try {// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 存放主机密码jSchSession.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();jschChannel = jSchSession.openChannel("exec");((ChannelExec) jschChannel).setCommand(command);jschChannel.setInputStream(null);// 错误信息输出流,用于输出错误的信息,当exitstatus<0的时候((ChannelExec)jschChannel).setErrStream(System.err);// 执行命令,等待执行结果jschChannel.connect();// 获取命令执行结果InputStream in = jschChannel.getInputStream();/*** 通过channel获取信息的方式,采用官方Demo代码*/byte[] tmp=new byte[1024];while(true){while(in.available() > 0){int i = in.read(tmp, 0, 1024);if (i < 0) {break;}result.append(new String(tmp, 0, i));}// 从channel获取全部信息之后,channel会自动关闭if(jschChannel.isClosed()){if (in.available() > 0) {continue;}exitStatus = jschChannel.getExitStatus();break;}try{Thread.sleep(1000);}catch(Exception ee){}}} catch (Exception e) {logger.warn(e.getMessage());} finally {// 关闭sftpChannelif (jschChannel != null && jschChannel.isConnected()) {jschChannel.disconnect();}// 关闭jschSesson流if (jSchSession != null && jSchSession.isConnected()) {jSchSession.disconnect();}}logger.info("获取执行命令的结果结果:"+result);logger.info("退出码为:"+exitStatus);}
}

2.5 交互执行shell命令

# 说明
当前Shell交互有弊端,执行完毕之后没有进行资源释放
package com.ix.utils;import com.jcraft.jsch.*;
import java.util.Properties;public class JSchInteractionShell{public static void main(String[] arg){String username = "root";String password = "123456";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;Channel jschChannel = null;try{// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 存放主机密码jSchSession.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();jschChannel = jSchSession.openChannel("shell");jschChannel.setInputStream(System.in);jschChannel.setOutputStream(System.out);jschChannel.connect();}catch(Exception e){System.out.println(e);}}}

2.6 通过公钥免密登录

# 前置准备# 生成公私钥,第一回车可以选择生成的位置
$ ssh-keygen -t rsa -C 'mysqlapi'# 默认情况下,最新版本的OpenSSH(7.8及更高版本)会以新的OpenSSH格式生成密钥,其开头为:
$ ssh-keygen -p -f  /c/Users/86186/.ssh/id_rsa -m pem -P "" -N ""# 记录公钥
cat /home/mysqlapi/.ssh/id_rsa.pub# 记录私钥
cat /home/mysqlapi/.ssh/id_rsa# 该目录如果没有known_hosts文件,进行创建
known_hosts# 登录需要连接的主机
[root@Node1 ~/.ssh]# pwd
/root/.ssh# 将JSch服务器所在的主机的公钥放入该文件中
[root@Node1 ~/.ssh]# vim authorized_keys 
package com.ix.utils;import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Properties;public class JSchIdentity {private static final Logger logger = LoggerFactory.getLogger(JSchIdentity.class);public static void main(String[] args) {String username = "root";String host = "192.168.66.36";int port = 22;// 创建JSch对象JSch jSch = new JSch();Session jSchSession = null;boolean reulst = false;try {// 存放公钥jSch.setKnownHosts("C:/Users/86186/.ssh/known_hosts");jSch.addIdentity("C:/Users/86186/.ssh/id_rsa");// 根据主机账号、ip、端口获取一个Session对象jSchSession = jSch.getSession(username, host, port);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");jSchSession.setConfig(config);// 超时连接时间为3秒jSchSession.setTimeout(3000);// 进行连接jSchSession.connect();// 获取连接结果reulst = jSchSession.isConnected();} catch (JSchException e) {logger.warn(e.getMessage());} finally {// 关闭jschSesson流if (jSchSession != null && jSchSession.isConnected()) {jSchSession.disconnect();}}if (reulst) {logger.error("【SSH连接】连接成功");} else {logger.error("【SSH连接】连接失败");}}
}

第 3 章 提取工具类

3.1 单次使用工具类

JschUtil.java

package com.ix.utils;import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.*;
import java.nio.charset.Charset;
import java.util.Properties;public class JschUtil {private static final Logger logger = LoggerFactory.getLogger(JschUtil.class);private JSch jSch;// session对象private Session session;// JAVA与主机的连接通道private Channel channel;// sftp通道ChannelSftp chSftp;// 主机ipprivate String host;// 主机端口号private int port;// 主机账号private String username;// 主机密码private String password;public JschUtil(String host, int port, String username, String password) {this.host = host;this.port = port;this.username = username;this.password = password;}public JschUtil() {}/*** 检测是否可以和主机通信* @return*/public boolean connect() {jSch = new JSch();boolean reulst = false;try {// 根据主机账号、ip、端口获取一个Session对象session = jSch.getSession(username, host, port);// 存放主机密码session.setPassword(password);// 首次连接,去掉公钥确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);// 超时连接时间为3秒session.setTimeout(3000);// 进行连接session.connect();// 获取连接结果reulst = session.isConnected();if (!reulst) {logger.error("【连接】获取连接失败");}} catch (JSchException e) {logger.warn(e.getMessage());} finally {close();}return reulst;}/*** 关闭连接*/public void close() {if (channel != null && channel.isConnected()) {channel.disconnect();}if (session != null && session.isConnected()) {session.disconnect();}if (chSftp != null && chSftp.isConnected()) {chSftp.quit();}}/*** 执行shell命令* @param command* @return*/public String execCommand(String command) {jSch = new JSch();// 存放执行命令结果StringBuffer result = new StringBuffer();int exitStatus = 0;try {// 根据主机账号、ip、端口获取一个Session对象session = jSch.getSession(username, host, port);// 存放主机密码session.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);// 超时连接时间为3秒session.setTimeout(3000);// 进行连接session.connect();channel = session.openChannel("exec");((ChannelExec) channel).setCommand(command);channel.setInputStream(null);// 错误信息输出流,用于输出错误的信息,当exitstatus<0的时候((ChannelExec)channel).setErrStream(System.err);// 执行命令,等待执行结果channel.connect();// 获取命令执行结果InputStream in = channel.getInputStream();/*** 通过channel获取信息的方式,采用官方Demo代码*/byte[] tmp=new byte[1024];while(true){while(in.available() > 0){int i = in.read(tmp, 0, 1024);if (i < 0) {break;}result.append(new String(tmp, 0, i));}// 从channel获取全部信息之后,channel会自动关闭if(channel.isClosed()){if (in.available() > 0) {continue;}exitStatus = channel.getExitStatus();break;}try{Thread.sleep(1000);}catch(Exception ee){}}} catch (IOException e) {logger.error(e.getMessage());} catch (JSchException e) {logger.error(e.getMessage());} finally {close();}logger.info("退出码为:"+exitStatus);return result.toString();}/*** 文件上传至主机* @param directory 当前文件路径* @param uploadFile 上传至主机的路径*/public void upload(String directory, String uploadFile) {// 创建JSch对象jSch = new JSch();try {// 根据主机账号、ip、端口获取一个Session对象session = jSch.getSession(username, host, port);// 存放主机密码session.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);// 超时连接时间为3秒session.setTimeout(3000);// 进行连接session.connect();// 打开SFTP通道chSftp = (ChannelSftp)session.openChannel("sftp");// 建立STFP连接chSftp.connect();// 设置编码格式chSftp.setFilenameEncoding("UTF-8");/*** 说明:* 1、当前文件上传信息没有任何反馈,如果没有异常则代表成功* 2、如果需要判断是否读取成功的进度,可参考https://blog.csdn.net/coding99/article/details/52416373?locationNum=13&fps=1* 3、将src文件上传到dst路径中*/chSftp.put(directory, uploadFile);logger.info("文件上传成功");} catch (JSchException | SftpException e) {logger.warn(e.getMessage());} finally {close();}}/*** 将主机文件下载至本地* @param directory  下载到本地的位置* @param downloadFile  下载文件在虚拟机的位置*/public void download(String directory, String downloadFile) {try {jSch = new JSch();// 根据主机账号、ip、端口获取一个Session对象session = jSch.getSession(username, host, port);// 存放主机密码session.setPassword(password);// 去掉首次连接确认Properties config = new Properties();config.put("StrictHostKeyChecking", "no");session.setConfig(config);// 超时连接时间为3秒session.setTimeout(3000);// 进行连接session.connect();// 打开SFTP通道chSftp = (ChannelSftp)session.openChannel("sftp");// 建立SFTP通道的连接chSftp.connect();// 设置编码格式chSftp.setFilenameEncoding("UTF-8");/*** 说明:* 1、当前上读取文件信息没有任何反馈,如果没有异常则代表成功* 2、如果需要判断是否读取成功的进度,可参考https://blog.csdn.net/coding99/article/details/52416373?locationNum=13&fps=1* 3、将src文件下载到dst路径中*/chSftp.get(directory, downloadFile);logger.info("文件下载成功");} catch (JSchException | SftpException e) {logger.warn(e.getMessage());}  finally {close();}}
}

RunJsch.java

package com.ix.utils;public class RunJsch {public static void main(String[] args) {JschUtil jSchUtil = new JschUtil("192.168.66.36",22,"root","123456");boolean connect = jSchUtil.connect();System.out.println("ssh连接检测:" + connect);String ls = jSchUtil.execCommand("ls");System.out.println("执行ls命令的结果:" + ls);// 文件上传jSchUtil.upload("D:/2.txt", "/home/1.txt");// 文件下载jSchUtil.download("/home/1.txt", "D:/node1.txt");}
}

http://chatgpt.dhexx.cn/article/mqBdU1Q4.shtml

相关文章

OrmLite For Android 学习笔记 之一 Ormlite 介绍及使用

Android 自带的数据库是SQLite&#xff0c;这种数据库适合用于于小型设备中。在实际使用数据库的应用中&#xff0c;我们经常需要把数据库记录转换为 业务对象实体。在桌面应用或者web应用中我们有很多成熟的ORM工具。Android本身没有提供这么一种工具。 Ormlite 是一种ORM工具…

OrmLite 数据库使用大全

本文介绍OrmLite的数据库表的使用以及在项目中选择他的原因。 1. 选用 OrmLite 数据库的原因 目前用的最多的就是GreenDAO 和 OrmLite 了&#xff0c;两者各有优缺点。 GreenDAO 性能高&#xff0c;号称Android最快的关系型数据库&#xff1b;内存占用较小&#xff1b;支持数…

Android ORM数据库之OrmLite使用框架及源码分析

一、简介 OrmLite是一个数据库框架&#xff0c;这个可以让我们快速实现数据库操作&#xff0c;避免频繁手写sql&#xff0c;提高我们的开发效率&#xff0c;减少出错的机率。  首先可以去它的官网看看www.ormlite.com&#xff0c;它的英文全称是Object Relational Mapping&am…

ORMLite完全解析(一)通过实例理解使用流程

在android中使用原始的SQLiteOpenHelper操作数据库显得过于繁琐&#xff0c;而且对于不是很熟悉数据库操作的人来说比较容易出现一些隐藏的漏洞。所以一般都会想到使用相关的ORMLite框架完成开发&#xff0c;类似于J2EE开发中的Hibernate和Mybatis等等&#xff0c;在提高开发效…

Android数据库ORMlite框架

前言 由于第二章是整个文档的核心&#xff0c;内容也很多&#xff0c;所以分次翻译。下一章的内容会继续本章接着翻译。 ------------------------------------------------------------------------------------- 2 如何使用 这一章进入到更多详细地使用ORMLite的各种功能。 2…

Ormlite 介绍 一

概述 ORMlite是类似hibernate的对象映射框架,主要面向java语言,同时,是时下最流行的android面向数据库的的编程工具。 官方网站:http://ormlite.com/ 如果需要开发android,只需要下载core和android两个jar包: ORMlite的使用 1,建立映射关系 Ormlite与数据库…

ormlite介绍一

概述 ORMlite是类似hibernate的对象映射框架&#xff0c;主要面向java语言&#xff0c;同时&#xff0c;是时下最流行的android面向数据库的的编程工具。 官方网站&#xff1a;http://ormlite.com/ 如果需要开发android&#xff0c;只需要下载core和android两个jar包&#xff…

Lite-Orm数据库

1. 初步认识 GItHub库 自动化且比系统自带数据库操作快1倍&#xff01; LiteOrm是android上的一款数据库&#xff08;ORM&#xff09;框架库。速度快、体积小、性能高。开发者基本一行代码实现数据库的增删改查操作&#xff0c;以及实体关系的持久化和自动映射。 2.导入orm相…

Android 数据库框架ormlite 使用精要

Android 数据库框架ormlite 使用精要 前言 本篇博客记录一下笔者在实际开发中使用到的一个数据库框架&#xff0c;这个可以让我们快速实现数据库操作&#xff0c;避免频繁手写sql&#xff0c;提高我们的开发效率&#xff0c;减少出错的机率。 ormlite是什么&#xff1f; 首…

ormlite 的简单应用

在android开发中还有哪些技术可以方便的操作数据库&#xff0c;我不大清楚&#xff0c;今天学习了一下 ormlite&#xff0c;觉得还不错&#xff0c;非常方便。 ormlite官网下载&#xff1a;http://ormlite.com/releases/ 1、引入jar包 2、写实体类 package com.example.aandr…

OrmLite for android--Ormlite的大概介绍

Ormlite 是一种ORM工具&#xff0c;并且是一种轻量级别的工具。我们可以使用它来对Android中内嵌的sqlite数据库进行相关的操作。Android 的应用程序应使用 Ormlite for android 版本来进行相关的开发。Ormlite for android 提供两个jar库&#xff1a;ormlite-android-4.22.j…

Ormlite 介绍 一

概述 ORMlite是类似hibernate的对象映射框架&#xff0c;主要面向java语言&#xff0c;同时&#xff0c;是时下最流行的android面向数据库的的编程工具。 官方网站&#xff1a;http://ormlite.com/ 如果需要开发android&#xff0c;只需要下载core和android两个jar包&#xff…

Ormlite基本使用

首先需要导入ORMLite的依赖&#xff1a;在build.gradle中加入以下代码&#xff1a; implementation com.j256.ormlite:ormlite-android:5.1implementation com.j256.ormlite:ormlite-core:5.1建立Bean类&#xff08;以OneTableBean为例&#xff09; import com.j256.ormlite.f…

Android 数据库框架ormlite 使用

ormlite是什么&#xff1f; 首先可以去它的官网看看www.ormlite.com&#xff0c;它的英文全称是Object Relational Mapping&#xff0c;意思是对象关系映射&#xff1b;如果接触过Java EE开发的&#xff0c;一定知道Java Web开发就有一个类似的数据库映射框架——Hibernate。简…

Android ORMLite数据库简介

&#xfeff;&#xfeff; 一般的项目中&#xff0c;Android自身提供的SQLite数据库&#xff0c;可以满足轻量级的数据存储应用&#xff0c;但是&#xff0c;只要是存储模型稍微复杂的项目&#xff0c;以及数据结构模型复杂的应用&#xff0c;就很难再用SQLite支撑整个项目的数…

python的网络请求库urllib、urllib2、urllib3、request的联系

文章目录 1. 简介2. urllib3. urllib24. urllib35. requests6. 相关文章 1. 简介 urllib、urllib2、urllib3、request均能通过网络访问互联网上的资源文件&#xff0c;它们通过使用统一资源定位符&#xff08;URL&#xff09;并结合re模块完成很多意想不到的操作。 urllib&am…

python3安装urllib2_python3.6想使用urllib2包怎么办

Python3.6.6或者说python3.x找不到urllib2语法问题修改之后&#xff0c;会报一个没有安装urllib2的包的错误。 通过pip install urllib2也会提示找不到包。(推荐学习&#xff1a;Python视频教程) 通过pip3 install urllib2也会提示找不到包。 这是因为builtwith依赖于urllib2包…

pythonurllib2方法_解决python3.6想使用urllib2包的方法

解决python3.6想使用urllib2包的方法 发布时间&#xff1a;2020-08-11 14:24:50 来源&#xff1a;亿速云 阅读&#xff1a;131 作者&#xff1a;小新 小编给大家分享一下解决python3.6想使用urllib2包的方法&#xff0c;相信大部分人都还不怎么了解&#xff0c;因此分享这篇文章…

python安装urllib2_Python如何安装urllib2库

urllib2 是 Python2.7 自带的模块(不需要下载,导入即可使用)。 urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地。 在Python中有很多库可以用来抓取网页,我们先学习urllib2。urllib2 是 Python2.7 自带的模块(不需要下载,…

python爬虫-urllib2的使用方法详解(python3)

文章目录 python编程快速上手&#xff08;持续更新中…&#xff09;python爬虫从入门到精通urllib2概述python2与python3对比urlopenRequestUser-Agent添加更多的Header信息 urllib2默认只支持HTTP/HTTPS的GET和POST方法URL编码转换&#xff1a;urllib.parse.urlencode模拟百度…