SQLServer的,希望对你有帮助:
public void initializeDB()
{
try
{
mStatement=conn.createStatement();
ResultSet rsInsert =mStatement.executeQuery("select * from imageTable");
ResultSetMetaData rsmdInsert=rsInsert.getMetaData();
for (int i = 1; i <= rsmdInsert.getColumnCount();i++)
{
mTestImageDb.jCBColumnName.addItem(rsmdInsert.getColumnName(i));
}
while (rsInsert.next())
{
miCurrentCount++;
mTestImageDb.jCBRowNumber.addItem(String.valueOf(miCurrentCount));
} rsmdInsert = null;
rsInsert.close();
rsInsert = null;
mStatement.close();
mStatement= null;
System.err.println(miCurrentCount);
}
catch (Exception exInit)
{
System.err.println("initializeDB error:"+exInit);
} }public boolean insertImage(File f)
{
String StrSql = "insert into imageTable values (?,?,?,?,?,?,?)";
String sFile = f.getName(); try
{
// File f = new File("e:\\T14.gif");
fin = new FileInputStream(f);
/*//////////////////////////////////////通过字节数组来存入数据库
byte[] ib = new byte[(int)f.length()];
fin.read(ib);
ByteArrayInputStream fou1 = new ByteArrayInputStream(ib);
*/
ps = conn.prepareStatement(StrSql); ps.setInt(1,miCurrentCount+1);
ps.setString(2,sFile);
ps.setString(3,sFile.substring(sFile.length()-3));
ps.setString(4,f.getPath());
ps.setInt(5,(int)f.length());
///时间
Calendar cc = new GregorianCalendar();
ps.setTimestamp(6,new Timestamp(cc.getTime().getTime()),cc);
/////////////////////////////////////////////////
int len = (int)f.length();
ps.setBinaryStream(7,fin,len);
ps.setBlob();
ps.executeUpdate();
ps.clearParameters();
}
catch ( Exception ex )
{
ex.printStackTrace();
return false;
} miCurrentCount++;
mTestImageDb.jCBRowNumber.addItem(String.valueOf(miCurrentCount)); return true;
} public boolean getImage()
{
try
{
fou = new FileOutputStream("e:\\temp\\2.jpg"); st = conn.createStatement();
rs = st.executeQuery("Select * from imageTable "); // rs.molast();
//System.out.println("当前数据库中的行数:" + rs.getInt("id"));
//rs.first();
while (rs.next())
{
int dLength = rs.getInt("ImageLength");
InputStream imagedata = rs.getBinaryStream("image"); idata = new byte[dLength]; int rec = imagedata.read(idata);
/*char[] c = new char[dLength];
for(int i2 = 0 ; i2 < dLength ; i2 ++)
{
c[i2] = (char)idata[i2];
}
c2b(c);*/
ImageIcon mImageIcon = new ImageIcon(idata);
this.mTestImageDb.jLImage.setIcon(mImageIcon);
fou.write(idata);
//idata = null;
} rs.close();
st.close();
fou.close();
}
catch (Exception ex )
{
ex.printStackTrace();
return false;
} return true;
}
public void initializeDB()
{
try
{
mStatement=conn.createStatement();
ResultSet rsInsert =mStatement.executeQuery("select * from imageTable");
ResultSetMetaData rsmdInsert=rsInsert.getMetaData();
for (int i = 1; i <= rsmdInsert.getColumnCount();i++)
{
mTestImageDb.jCBColumnName.addItem(rsmdInsert.getColumnName(i));
}
while (rsInsert.next())
{
miCurrentCount++;
mTestImageDb.jCBRowNumber.addItem(String.valueOf(miCurrentCount));
} rsmdInsert = null;
rsInsert.close();
rsInsert = null;
mStatement.close();
mStatement= null;
System.err.println(miCurrentCount);
}
catch (Exception exInit)
{
System.err.println("initializeDB error:"+exInit);
} }public boolean insertImage(File f)
{
String StrSql = "insert into imageTable values (?,?,?,?,?,?,?)";
String sFile = f.getName(); try
{
// File f = new File("e:\\T14.gif");
fin = new FileInputStream(f);
/*//////////////////////////////////////通过字节数组来存入数据库
byte[] ib = new byte[(int)f.length()];
fin.read(ib);
ByteArrayInputStream fou1 = new ByteArrayInputStream(ib);
*/
ps = conn.prepareStatement(StrSql); ps.setInt(1,miCurrentCount+1);
ps.setString(2,sFile);
ps.setString(3,sFile.substring(sFile.length()-3));
ps.setString(4,f.getPath());
ps.setInt(5,(int)f.length());
///时间
Calendar cc = new GregorianCalendar();
ps.setTimestamp(6,new Timestamp(cc.getTime().getTime()),cc);
/////////////////////////////////////////////////
int len = (int)f.length();
ps.setBinaryStream(7,fin,len);
ps.setBlob();
ps.executeUpdate();
ps.clearParameters();
}
catch ( Exception ex )
{
ex.printStackTrace();
return false;
} miCurrentCount++;
mTestImageDb.jCBRowNumber.addItem(String.valueOf(miCurrentCount)); return true;
} public boolean getImage()
{
try
{
fou = new FileOutputStream("e:\\temp\\2.jpg"); st = conn.createStatement();
rs = st.executeQuery("Select * from imageTable "); // rs.molast();
//System.out.println("当前数据库中的行数:" + rs.getInt("id"));
//rs.first();
while (rs.next())
{
int dLength = rs.getInt("ImageLength");
InputStream imagedata = rs.getBinaryStream("image"); idata = new byte[dLength]; int rec = imagedata.read(idata);
/*char[] c = new char[dLength];
for(int i2 = 0 ; i2 < dLength ; i2 ++)
{
c[i2] = (char)idata[i2];
}
c2b(c);*/
ImageIcon mImageIcon = new ImageIcon(idata);
this.mTestImageDb.jLImage.setIcon(mImageIcon);
fou.write(idata);
//idata = null;
} rs.close();
st.close();
fou.close();
}
catch (Exception ex )
{
ex.printStackTrace();
return false;
} return true;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货