隨筆-200  評論-148  文章-15  trackbacks-0
          多謝大家,我已經弄好了,就是用的樓上那為仁兄的方法!太感謝了 !

          表是:
          id int
          imageS image

          [code]
          public void save_actionPerformed(ActionEvent e) {??//保存到數據庫中
          ? ? try{
          ? ?? ?File tmpFile = new File(path);
          ? ?? ?FileInputStream in = new FileInputStream(tmpFile);
          ? ?? ?BufferedInputStream inFile = new BufferedInputStream(in);
          ? ?? ?//獲取圖片大小
          ? ?? ?int a = Integer.parseInt(String.valueOf(tmpFile.length()));
          ? ?? ?System.out.println(a / 1000 + "KB");
          ? ?? ?byte len[] = new byte[a];
          ? ?? ?//讀字節
          ? ?? ?while (inFile.read(len) != -1) {
          ? ?? ?}
          ? ?? ?int pos=1;
          ? ?? ?//設置id號碼
          ? ?? ?String sql = "select count(*) as total from product";
          ? ?? ?pst = con.prepareStatement(sql);
          ? ?? ?rs = pst.executeQuery();
          ? ?? ?if (rs.next()) {
          ? ?? ?? ?pos=rs.getInt(1);
          ? ?? ?? ?pos++;
          ? ?? ?}
          ? ?? ?//插入記錄
          ? ?? ?sql = "insert into product values(?,?)";
          ? ?? ?pst = con.prepareStatement(sql);
          ? ?? ?pst.setInt(1, pos);
          ? ?? ?pst.setBytes(2, len);
          ? ?? ?pst.executeUpdate();
          ? ?? ?JOptionPane.showMessageDialog(this, "插入記錄完畢!", "消息",
          ? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? JOptionPane.INFORMATION_MESSAGE);
          ? ? }
          ? ? catch(IOException ex){
          ? ?? ?System.out.println("I/O錯誤!"+ex.getMessage());
          ? ?? ?ex.printStackTrace();
          ? ? }
          ? ? catch(Exception ex){
          ? ?? ?System.out.println("插入記錄錯誤!"+ex.getMessage());
          ? ?? ?ex.printStackTrace();
          ? ? }
          ??}
          [/code]

          我使用了一個下拉彩旦
          [code]
          ??public void item_itemStateChanged(ItemEvent e) {??//提取圖片
          ? ? if(item.getSelectedItem().toString().equals("------ID------")){
          ? ? }
          ? ? else{
          ? ?? ?int tmpId = Integer.parseInt(item.getSelectedItem().toString());
          ? ?? ?try {
          ? ?? ???String sql = "select * from product where id=?";
          ? ?? ???pst = con.prepareStatement(sql);
          ? ?? ???pst.setInt(1, tmpId);
          ? ?? ???rs = pst.executeQuery();
          ? ?? ???if (rs.next()) {
          ? ?? ?? ? int id = rs.getInt("id");
          ? ?? ?? ? //提取圖片字節
          ? ?? ?? ? byte src[] = rs.getBytes("imageS");
          ? ?? ?? ? //還原
          ? ?? ?? ? ImageIcon i = new ImageIcon(src);
          ? ?? ?? ? ima.setIcon(i);
          ? ?? ???}
          ? ?? ?}
          ? ?? ?catch (SQLException ex) {
          ? ?? ???System.out.println("從數據庫讀取圖片錯誤!");
          ? ?? ???System.out.println(ex.getMessage());
          ? ?? ?}
          ? ? }

          ??}
          [/code]
          posted on 2006-10-28 10:13 無聲 閱讀(281) 評論(0)  編輯  收藏 所屬分類: 職場生活
          主站蜘蛛池模板: 宜川县| 黄陵县| 柳林县| 衡阳市| 大足县| 明水县| 义马市| 高陵县| 海丰县| 东兰县| 平塘县| 潜山县| 江山市| 延吉市| 长汀县| 石家庄市| 商丘市| 临朐县| 松原市| 行唐县| 连南| 淳化县| 邓州市| 泰来县| 祥云县| 江西省| 烟台市| 林西县| 景洪市| 通州市| 宜州市| 衢州市| 马山县| 彩票| 盐城市| 通山县| 乐业县| 舟曲县| 阜宁县| 鄂尔多斯市| 景泰县|