110 lines
3.9 KiB
Java
110 lines
3.9 KiB
Java
package com.point.strategy.oaDocking.service;
|
|
|
|
import com.point.strategy.common.Base64Utils;
|
|
import com.point.strategy.common.SystemProperty;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.sql.*;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
public class YcjSystemIntegration {
|
|
private static final Logger LOGGER = Logger.getLogger(YcjSystemIntegration.class.getName());
|
|
|
|
private static final String driver_dm = SystemProperty.getKeyValue("ycj.driverClassName", "application.properties");
|
|
private static final String url = SystemProperty.getKeyValue("ycj.url", "application.properties");
|
|
private static final String user = SystemProperty.getKeyValue("ycj.username", "application.properties");
|
|
private static final String password = SystemProperty.getKeyValue("ycj.password", "application.properties");
|
|
|
|
// 获取数据库连接
|
|
private static Connection getConnection() throws Exception {
|
|
Class.forName(driver_dm);
|
|
return DriverManager.getConnection(url, user, password);
|
|
}
|
|
|
|
// 查询
|
|
public static ResultSet executeQuery(String sql) throws Exception {
|
|
Connection conn = null;
|
|
Statement st = null;
|
|
try {
|
|
conn = getConnection();
|
|
st = conn.createStatement();
|
|
return st.executeQuery(sql);
|
|
} catch (Exception e) {
|
|
LOGGER.log(Level.SEVERE, "执行查询时出错: " + sql, e);
|
|
closeResources(conn, st, null);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
// 修改
|
|
public static int executeUpdate(String sql) throws Exception {
|
|
Connection conn = null;
|
|
Statement st = null;
|
|
try {
|
|
conn = getConnection();
|
|
st = conn.createStatement();
|
|
return st.executeUpdate(sql);
|
|
} catch (Exception e) {
|
|
LOGGER.log(Level.SEVERE, "执行更新时出错: " + sql, e);
|
|
throw e;
|
|
} finally {
|
|
closeResources(conn, st, null);
|
|
}
|
|
}
|
|
|
|
// 关闭资源
|
|
public static void closeResources(Connection conn, Statement st, ResultSet rs) {
|
|
try {
|
|
if (rs != null) rs.close();
|
|
if (st != null) st.close();
|
|
if (conn != null) conn.close();
|
|
} catch (Exception e) {
|
|
LOGGER.log(Level.WARNING, "关闭资源时出错", e);
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// Connection conn = null;
|
|
// Statement st = null;
|
|
// ResultSet resultSet = null;
|
|
// try {
|
|
//
|
|
// String imageBytes = Base64Utils.encode2("D:\\2\\222.pdf");
|
|
// conn = getConnection();
|
|
// String sql = "INSERT INTO fujian (fawen_index,fujian_id,sound_image) VALUES (?,?,?)";
|
|
// PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
// pstmt.setInt(1, 1);
|
|
// pstmt.setInt(2, 5);
|
|
// pstmt.setString(3, imageBytes);
|
|
// pstmt.executeUpdate();
|
|
//
|
|
// System.out.println("图像已成功转换为longblob并存储到数据库。");
|
|
// } catch (Exception e) {
|
|
// e.printStackTrace();
|
|
// }
|
|
|
|
|
|
Connection conn = null;
|
|
Statement st = null;
|
|
ResultSet resultSet = null;
|
|
try (FileInputStream fis = new FileInputStream(new File("D:\\2\\222.pdf"))) {
|
|
|
|
byte[] imageBytes = new byte[(int) new File("D:\\2\\222.pdf").length()];
|
|
fis.read(imageBytes);
|
|
conn = getConnection();
|
|
String sql = "INSERT INTO fujian (fawen_index,fujian_id,sound_image) VALUES (?,?,?)";
|
|
PreparedStatement pstmt = conn.prepareStatement(sql);
|
|
pstmt.setInt(1, 1);
|
|
pstmt.setInt(2, 3);
|
|
pstmt.setBytes(3, imageBytes);
|
|
pstmt.executeUpdate();
|
|
|
|
System.out.println("图像已成功转换为longblob并存储到数据库。");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} |