⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 saveinfo2db.java

📁 保存图片信息到数据库 数据库Oracle 里面用到的库需要自己重新建立
💻 JAVA
字号:
package src;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SaveInfo2DB {

	private static final String Connection = null;

	private Connection dbCon = null;

	private Statement state = null;

	private ResultSet rst = null;

	private String dbDriver = "oracle.jdbc.driver.OracleDriver";

	private String dbUrl = "jdbc:oracle:thin:@192.168.2.5:1521:ORGDB005";

	private String userName = "服务定制网站";

	private String passWord = "APPLE";

	/**
	 * 连接数据库
	 * 
	 * @return
	 */
	public boolean connet2DB() {
		if (dbCon == null) {
			try {
				Class.forName(dbDriver);
				dbCon = DriverManager.getConnection(dbUrl, userName, passWord);
				// state = dbCon.createStatement();
			} catch (Exception exe) {
				System.out.println("数据库连接失败!\n" + "driver: " + dbDriver
						+ "\nurl: " + dbUrl + "\nuserNam: " + userName
						+ "\npassWord :" + passWord);
				exe.printStackTrace();
				return false;
			}
		}
		return true;
	}

	/**
	 * 
	 * @return
	 */
	public void releaseConnect() {
		if (dbCon != null) {
			try {
				state.close();
				state = null;
				dbCon.close();
				dbCon = null;
			} catch (SQLException sqlExe) {
				System.out.println("释放数据库连接失败!");
				sqlExe.printStackTrace();
			}
		}
	}

	/**
	 * 描述 保存本地图片信息到数据库 参数 serviceID 服务编号 photoName 文件名字 photoPath 图片保存在本地磁盘上的路径
	 * photoID 图片编号
	 */
	public void savePhotoInfo2DB(String serviceID, String photoID,
			String photoName, String filePath) throws Exception {

		// 数据库连接
		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		Connection con = DriverManager.getConnection(
				"jdbc:oracle:thin:@192.168.2.5:1521:ORGDB005", "服务定制网站",
				"APPLE");
		con.setAutoCommit(false);
		Statement st = con.createStatement();

		st
				.execute("insert into tupianxxb(fuwubianhao,tupianbianhao,tupianmingcheng,tupianneirong)values('"
						+ serviceID
						+ "','"
						+ photoID
						+ "','"
						+ photoName
						+ "'," + "empty_blob())");
		String sqlStr1 = "select * from  tupianxxb where  fuwubianhao = " + "'"
				+ serviceID + "'" + " for update";

		// System.out.println(sqlStr1);测试SQL语句是否正确

		// 用for update方式锁定数据行
		ResultSet rs = st.executeQuery(sqlStr1);

		if (rs.next()) {
			// 使用oracle.sql.BLOB类
			// '8'表示Blob所在的列
			oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(8);

			// 到数据库的输出流
			OutputStream outStream = blob.getBinaryOutputStream();

			// 这里用一个文件模拟输入流
			File file = new File(filePath);
			InputStream fin = new FileInputStream(file);

			// 将输入流写到输出流
			byte[] b = new byte[blob.getBufferSize()];
			int len = 0;
			while ((len = fin.read(b)) != -1) {
				outStream.write(b, 0, len);
			}
			// 依次关闭
			fin.close();
			outStream.flush();
			outStream.close();
		}
		con.commit();
		con.close();
		System.out.println("<保存图片信息成功>");
	}

	/**
	 * 描述 按服务编号读取数据库图片信息到本地 参数 serviceID 服务编号 filePath 从数据库中读取图片的保存路径
	 */
	public void readPhotoInfoByServiceID(String serviceID, String filePath)
			throws Exception {

		// 数据库连接
		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		Connection con = DriverManager.getConnection(
				"jdbc:oracle:thin:@192.168.2.5:1521:ORGDB005", "服务定制网站",
				"APPLE");

		// st用于执行SQL语句
		Statement st = con.createStatement();
		// 将结果保存在rs中
		String a = "select tupianneirong from tupianxxb where fuwubianhao="
				+ "'" + serviceID + "'";
		// String photoName = "110";
		// String a = "select tupianneirong from tupianxxb where
		// fuwubianhao="+"'"+serviceID+"'"+" AND
		// "+"tupianmingcheng="+"'"+photoName+"'";
		// String a = "select tupianneirong from tupianxxb where
		// tupianmingcheng="
		// + "'" + photoName + "'";
		System.out.println(a);
		ResultSet rs = st.executeQuery(a);
		// System.out.println(a);
		int i = 000001;
		while (rs.next()) {
			java.sql.Blob blob = rs.getBlob(1);
			InputStream ins = blob.getBinaryStream();
			// 输出到文件

			String fileDirectory = filePath + File.separator + serviceID;
			File file = new File(fileDirectory);
			file.mkdirs();
			String fileSubPath = filePath + file.separator + serviceID
					+ File.separator + (i++) + ".jpg";
			File file1 = new File(fileSubPath);
			OutputStream fout = new FileOutputStream(file1);
			// 下面将BLOB数据写入文件
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = ins.read(b)) != -1) {
				fout.write(b, 0, len);
			}
			// 依次关闭
			fout.close();
			ins.close();
		}
		con.commit();
		con.close();
	}

	/**
	 * 描述 按服务编号读取数据库图片信息到本地 参数 photoName 图片名称 filePath 从数据库中读取图片的保存路径
	 */
	public void readPhotoInfoByPhotoName(String photoName, String filePath)
			throws Exception {

		// 数据库连接
		DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		Connection con = DriverManager.getConnection(
				"jdbc:oracle:thin:@192.168.2.5:1521:ORGDB005", "服务定制网站",
				"APPLE");
		// st用于执行SQL语句
		Statement st = con.createStatement();
		
		String sqlStr = "select tupianneirong from  tupianxxb  where  tupianmingcheng="+ "'" + photoName + "'";
		System.out.println(sqlStr);
		// 将结果保存在rs中
		ResultSet rs = st
				.executeQuery(sqlStr);
		
		if (rs.next()) {
			java.sql.Blob blob = rs.getBlob(1);
			InputStream ins = blob.getBinaryStream();
			// 输出到文件
			File file = new File(filePath);
			OutputStream fout = new FileOutputStream(file);
			// 下面将BLOB数据写入文件
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = ins.read(b)) != -1) {
				fout.write(b, 0, len);
			}
			// 依次关闭
			fout.close();
			ins.close();
		}
		con.commit();
		con.close();
	}

	/**
	 * 描述 获得当前信息的编号 参数 serviceID 服务编号
	 * 
	 * @throws SQLException
	 */
	public int getID(String serviceID) throws SQLException {

		int maxID = -1;
		String sqlStr = "";
		Statement state = null;
		ResultSet rst = null;

		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection con = DriverManager.getConnection(
				"jdbc:oracle:thin:@192.168.2.5:1521:ORGDB005", "服务定制网站",
				"APPLE");
		state = con.createStatement();

		sqlStr = " select max(TO_NUMBER(tupianbianhao))"
				+ " from tupianxxb where FUWUBIANHAO=" + "'"+serviceID+"'";
		try {
			rst = state.executeQuery(sqlStr);
			if (rst.next()) {
				maxID = rst.getInt(1) + 1;
			} else {
				maxID = 1;
			}
			if (con != null) {
				try {
					state.close();
					state = null;
					con.close();
					con = null;
				} catch (SQLException sqlExe) {
					System.out.println("释放数据库连接失败!");
					sqlExe.printStackTrace();
				}
			}
		} catch (SQLException sqlExe) {
			System.out.println("获得编号信息失败!");
			sqlExe.printStackTrace();
		}
		return maxID;
	}

	// 测试
	public static void main(String[] args) throws Exception {
		SaveInfo2DB s = new SaveInfo2DB();
		String photoName = "110";
		// s.savePhotoInfo2DB("000001", "000003", "003", "E:\\图片\\003.jpg");
		//s.readPhotoInfoByServiceID("000001", "D:\\图片信息");
		//s.readPhotoInfoByPhotoName(photoName, "D:/photoName006.jpg");
		System.out.println(s.getID("000001"));
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -