「java添加信息」java添加信息到数据库中
今天给各位分享java添加信息的知识,其中也会对java添加信息到数据库中进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中怎么将个人信息(学号、姓名、年龄、住址)对象添加到Map接口中
- 2、JAVA如何在文件头添加内容?
- 3、java 生成二维码后如何给该二维码添加信息
- 4、java中添加一条与原有的信息一样的信息再添加时改变原有信息的一个状态怎么做
java中怎么将个人信息(学号、姓名、年龄、住址)对象添加到Map接口中
可以使用HashMap类。
假设
学生类
Student
st1=new
Student("学号1","姓名1","年龄1","地址1");
Student
st2=new
Student("学号2","姓名2","年龄2","地址2");
Map
mpst=new
HashMap();
mpst.put("键","值");
HashMap是键-值对
比如:
mpst.put("学生1","st1");
mpst.put("学生2","st2")
JAVA如何在文件头添加内容?
“先读入,再写回”的方法时可行的
这个文件有几十兆,不代表整个读入,不是占用几十兆内存。先写入头部的文字,再循环读一点源文件,写一点源文件。
RandomAccessFile 也可以,只是最初要留出空间,比如一些空格
java 生成二维码后如何给该二维码添加信息
java可使用zxing生成二维码并为其添加信息。
以下是详细步骤:
1、创建MatrixToImageWriter类
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x width; x++) {
for (int y = 0; y height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
2、生成二维码并添加信息
import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String text = "";
int width = 300;
int height = 300;
//二维码的图片格式
String format = "gif";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
//生成二维码
File outputFile = new File("d:"+File.separator+"new.gif");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
java中添加一条与原有的信息一样的信息再添加时改变原有信息的一个状态怎么做
姑且认为你是添加到数据库。
首先你要开启一个事物,在这个事物中做一下两件事情。
1.根据添加的信息,查询原数据库中是否有相同信息,如果有,则update原数据状态。
2.再insert 添加的信息。
其他比如缓存操作什么的,思路都一样。
java添加信息的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java添加信息到数据库中、java添加信息的信息别忘了在本站进行查找喔。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。