隨筆-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)  編輯  收藏 所屬分類: 職場生活
          主站蜘蛛池模板: 息烽县| 罗甸县| 台东县| 南宁市| 武宁县| 尚义县| 三门县| 石泉县| 河曲县| 广德县| 峨眉山市| 洪泽县| 柳州市| 台东市| 昌江| 达拉特旗| 汾阳市| 乐陵市| 江安县| 德阳市| 株洲市| 沅陵县| 鹤壁市| 金华市| 三河市| 贵德县| 海口市| 剑川县| 闵行区| 沾益县| 营口市| 宁陕县| 临颍县| 太谷县| 潮安县| 娄烦县| 平阴县| 县级市| 涡阳县| 漯河市| 红原县|