|
| 1 | +package com.salk.lib.db.gbase; |
| 2 | + |
| 3 | +/** |
| 4 | + * @author salkli |
| 5 | + * @since 2023/3/30 |
| 6 | + **/ |
| 7 | +import com.salk.lib.db.mysql.bit.BitUtil; |
| 8 | + |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.sql.*; |
| 14 | +import java.util.Properties; |
| 15 | + |
| 16 | +public class InsertData { |
| 17 | + public static void main(String[] args) { |
| 18 | + InsertData insertData=new InsertData(); |
| 19 | + //insertData.getBlob(); |
| 20 | + insertData.insertBlob(); |
| 21 | + } |
| 22 | + public void getBlob(){//读取Blob数据 |
| 23 | + Connection con = null; |
| 24 | + PreparedStatement ps = null; |
| 25 | + ResultSet rs = null; |
| 26 | + try { |
| 27 | + con = JDBCTools.getConnection(); |
| 28 | + String sql = "SELECT varb1,varb2,f_binary_l FROM t_fields where varb1 is not null"; |
| 29 | + ps = con.prepareStatement(sql); |
| 30 | + rs = ps.executeQuery(); |
| 31 | + if(rs.next()){ |
| 32 | + byte[] picture = rs.getBytes(1);//得到Blob对象 |
| 33 | + System.out.println(picture); |
| 34 | + |
| 35 | + byte[] picture1 = rs.getBytes(2);//得到Blob对象 |
| 36 | + |
| 37 | + byte[] yihuo=new byte[picture.length]; |
| 38 | + for (int i = 0; i < Math.min(picture.length, picture1.length); i++) { |
| 39 | + yihuo[i]=(byte)(picture[i] ^ picture1[i]); |
| 40 | + |
| 41 | + } |
| 42 | + boolean result=true; |
| 43 | + for (int i = 0; i < Math.min(picture.length, yihuo.length); i++) { |
| 44 | + if((yihuo[i]|picture[i])!=picture[i]){ |
| 45 | + result=false; |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + System.out.println(result); |
| 50 | + //开始读入文件 |
| 51 | + System.out.println(picture1); |
| 52 | + } |
| 53 | + } catch (Exception e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public void insertBlob(){//插入Blob |
| 60 | + Connection con = null; |
| 61 | + PreparedStatement ps = null; |
| 62 | + try { |
| 63 | + con = JDBCTools.getConnection(); |
| 64 | + String sql = "INSERT INTO t_bit(f_binary_str,f_blob,f_binary_l,f_binary_l2) VALUES(?,?,?,?)"; |
| 65 | + ps = con.prepareStatement(sql); |
| 66 | + ByteBitMap bitMap = new ByteBitMap(44); |
| 67 | + bitMap.add(2); |
| 68 | + bitMap.add(3); |
| 69 | + bitMap.add(43); |
| 70 | + //bitMap.add(64); |
| 71 | + //bitMap.add(456); |
| 72 | + //bitMap.add(32332); |
| 73 | + byte[] bits = bitMap.bits; |
| 74 | + ps.setBytes(1,bits); |
| 75 | + |
| 76 | + ByteBitMap bitMap2 = new ByteBitMap(44); |
| 77 | + bitMap2.add(2); |
| 78 | + bitMap2.add(3); |
| 79 | + bitMap2.add(5); |
| 80 | + bitMap2.add(43); |
| 81 | + //bitMap2.add(64); |
| 82 | + //bitMap2.add(456); |
| 83 | + //bitMap2.add(32332); |
| 84 | + byte[] bits2 = bitMap2.bits; |
| 85 | + |
| 86 | + ps.setBytes(2, bits2); |
| 87 | + long l = BitUtil.bytesToLong(bits); |
| 88 | + System.out.println(l); |
| 89 | + ps.setLong(3, l); |
| 90 | + long l2 = BitUtil.bytesToLong(bits2); |
| 91 | + ps.setLong(4, l2); |
| 92 | + System.out.println(l2); |
| 93 | + ps.executeUpdate(); |
| 94 | + } catch (Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + }finally{ |
| 97 | + JDBCTools.release(con, ps); |
| 98 | + } |
| 99 | + } |
| 100 | + public static class JDBCTools {//JDBC工具类 用来建立连接和释放连接 |
| 101 | + public static Connection getConnection() throws Exception{//连接数据库 |
| 102 | + String driverClass = null; |
| 103 | + String url = null; |
| 104 | + String user = null; |
| 105 | + String password = null; |
| 106 | + |
| 107 | + Properties properties = new Properties(); |
| 108 | + |
| 109 | + InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties"); |
| 110 | + properties.load(in); |
| 111 | + driverClass = properties.getProperty("driver"); |
| 112 | + url = properties.getProperty("jdbcurl"); |
| 113 | + user = properties.getProperty("user"); |
| 114 | + password = properties.getProperty("password"); |
| 115 | + Class.forName(driverClass); |
| 116 | + return DriverManager.getConnection(url, user, password); |
| 117 | + } |
| 118 | + public static void release(Connection con , Statement state){//关闭数据库连接 |
| 119 | + if(state != null){ |
| 120 | + try { |
| 121 | + state.close(); |
| 122 | + } catch (SQLException e) { |
| 123 | + e.printStackTrace(); |
| 124 | + } |
| 125 | + } |
| 126 | + if(con != null){ |
| 127 | + try { |
| 128 | + con.close(); |
| 129 | + } catch (SQLException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + public static void release(ResultSet rs , Connection con , Statement state){//关闭数据库连接 |
| 136 | + if(rs != null) |
| 137 | + { |
| 138 | + try { |
| 139 | + rs.close(); |
| 140 | + } catch (SQLException e) { |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + } |
| 144 | + if(state != null){ |
| 145 | + try { |
| 146 | + state.close(); |
| 147 | + } catch (SQLException e) { |
| 148 | + e.printStackTrace(); |
| 149 | + } |
| 150 | + } |
| 151 | + if(con != null){ |
| 152 | + try { |
| 153 | + con.close(); |
| 154 | + } catch (SQLException e) { |
| 155 | + e.printStackTrace(); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments