📄 savephone.java
字号:
public class SavePhone{
public static void main(){
try{
//装载驱动程序
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn = DriverManager.getConnection
("jdbc:microsoft:sqlserver://hostname:port;","sa","zhangjl");
/*存储名为"dancegirl.gif"的图像文件,
使用java.sql包PreparedStatement类的SetBinaryStream()方法
*/
File files = new File("dancegirl.gif");
FileInputStream fis=new FileInputStream(files);
//预编译SQL语句
PreparedStatement ps = conn.prepareStatement(
"INSERT INTO binary_data (name,data) "VALUES (?, ?)");
ps.setString(1, files.getName());
ps.setBinaryStream(2, fis,(int)files.length());
ps.executeUpdate();
fis.close();
ps.close();
/*调出已存入数据库的名为"dancegirl.gif"的文件,
读出字节数并作为ImageIcon类对象在Label控件中显示出来
*/
ImageIcon ii;
JLabel jLabel1 = new JLabel();
int byteSum = 0;
int bytesRead = 0;
byte[] buffer = new byte[8 * 1924];
FileOutputStream fis = new FileOutputStream
((String)(jComboBox1.getSelectedItem()));
PreparedStatement ps =
conn.prepareStatement("select data from binary_data where name=?");
ps.setString(1,(String)(jComboBox1.getSelectedItem()));
ResultSet rs = ps.executeQuery();
if(rs != null){
while(rs.next()){
InputStream is = rs.getBinaryStream(1);
while((bytesRead = is.read(buffer)) != -1) {
byteSum += bytesRead;
fis.write(buffer, 0, bytesRead);
}
ii = new ImageIcon(buffer);
jLabel1.setIcon(ii);
is.close();
}
rs.close();
}
ps.close();
fis.close();
}catch(Exception e){
System.out.println("conn failed");
}finally{
rs.close();
ps.close();
fis.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -