★★用java sqlserver2000实现存放文件并回复文件 (0分)

  • 主题发起人 主题发起人 Haha...
  • 开始时间 开始时间
H

Haha...

Unregistered / Unconfirmed
GUEST, unregistred user!
朋友急要,所以没有好好整理代码,抱歉! 顺便贴出来,如果大家觉得没用的话请见谅!,
如果有错误请告诉我,大家一起改正!
上传文件可以用struts的upload的例子,改一下就行了
首先要注意的一点是不管你想存放到sqlserver中什么类型的文件,那个字段要设为是image型,
我刚开始用binary和varbinary出了很多异常.
下面的方法我试过exe,jpg,zip,gif
用com.microsoft.jdbc.sqlserver.SQLServerDriver,sun.jdbc.odbc.JdbcOdbcDriver都可以的.
写了一个数据库访问的bean如下:
package todb;
import java.sql.*;
import java.io.*;
public class DBBean implements Serializable{
private String DBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String DBLocation = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbs";
private String LogName = "hamduke";
private String Password= "2682226";
Connection conn = null;
ResultSet rs = null;
Statement stmt = null;
String strHint = "";
public void DBBean() { }
public void setDBDriver(String driver){ DBDriver = driver;}
public String getDBDriver(){ return DBDriver;
}
public void setDBLocation(String location){ DBLocation = location;}
public String getDBLocation(){ return DBLocation;
}
public void setLogName(String logname){ LogName = logname;}
public void setPassword(String password){ Password = password;}
public String getHint(){return strHint;}
public void initBean(){
try { Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("initBean(): " + e.getMessage());
}
}
public Connection getConn(){
try { conn = DriverManager.getConnection(DBLocation,LogName,Password);
}
catch(SQLException ex) {
System.err.println("getConn(): " + ex.getMessage());
}
return conn;
}//~getConn
public ResultSet executeQuery(String sql){
rs = null;
try{
conn = getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
strHint = "executeQuery: " + ex.getMessage();
System.err.println(strHint);
}
return rs;
}
//execute a SQL statement without returning recordset
public boolean executeUpdate(String sql)
{
try
{
conn = getConn();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
return true;
}
catch(SQLException ex)
{
strHint = "executeUpdate: " + ex.getMessage();
System.err.println(strHint);
return false;
}
}
//Close connections
public boolean closeConn()
{
try
{
if (rs!=null)
rs.close();
if (stmt!=null)
stmt.close();
if (conn!=null)
conn.close();
return true;
}
catch(SQLException ex)
{
strHint = "closeConn: " + ex.getMessage();
System.err.println(strHint);
return false;
}
}
}//~class
用jbuilder写了个界面,主要的代码写入和读出分别是两个jbutton的事件:
package todb;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.sql.*;
import java.sql.PreparedStatement;
import com.borland.dx.sql.dataset.*;
import com.borland.dbswing.*;
public class Frame1 extends JFrame {
private JPanel contentPane;
private JPanel jPanel1 = new JPanel();
private JButton jButton1 = new JButton();
public static File filename = null;
private JLabel jLabel1 = new JLabel();
private JFileChooser jFileChooser1 = new JFileChooser();
private XYLayout xYLayout2 = new XYLayout();
private JToggleButton jToggleButton1 = new JToggleButton();
private JToggleButton jToggleButton2 = new JToggleButton();
private Database database1 = new Database();
private QueryDataSet queryDataSet1 = new QueryDataSet();
private JdbTextArea jdbTextArea1 = new JdbTextArea();
private JdbNavToolBar jdbNavToolBar1 = new JdbNavToolBar();
private XYLayout xYLayout1 = new XYLayout();
private JdbTextField jdbTextField1 = new JdbTextField();
private JButton jButton2 = new JButton();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(496, 386));
this.setTitle("Frame Title");
jButton1.setText("Open");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jPanel1.setLayout(xYLayout2);
jLabel1.setText("jLabel1");
jPanel1.setBackground(Color.pink);
jToggleButton1.setText("TODB");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jToggleButton1_actionPerformed(e);
}
});
jToggleButton2.setText("Read");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jToggleButton2_actionPerformed(e);
}
});
database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:bbs", "hamduke", "2682226", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
database1.setDatabaseName("");
queryDataSet1.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1, "select * from Files", null, true, Load.ALL));
jdbTextArea1.setText("jdbTextArea1");
jdbTextArea1.setColumnName("FileData");
jdbTextArea1.setDataSet(queryDataSet1);
jdbNavToolBar1.setDataSet(queryDataSet1);
jdbTextField1.setText("jdbTextField1");
jdbTextField1.setColumnName("FileName");
jdbTextField1.setDataSet(queryDataSet1);
jPanel1.add(jButton1, new XYConstraints(0, 2, -1, -1));
jPanel1.add(jToggleButton1, new XYConstraints(66, 3, -1, -1));
jPanel1.add(jToggleButton2, new XYConstraints(130, 3, -1, -1));
jPanel1.add(jLabel1, new XYConstraints(211, 5, -1, -1));
jPanel1.add(jdbNavToolBar1, new XYConstraints(5, 35, -1, -1));
jPanel1.add(jButton2, new XYConstraints(258, 3, -1, -1));
contentPane.add(jdbTextArea1, new XYConstraints(15, 135, -1, 248));
contentPane.add(jFileChooser1, new XYConstraints(326, 100, 112, 46));
contentPane.add(jPanel1, new XYConstraints(0, 0, 496, -1));
contentPane.add(jdbTextField1, new XYConstraints(43, 90, 210, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
// Call openFile to attempt to load the text from file into TextArea
filename = jFileChooser1.getSelectedFile();
jLabel1.setText(jFileChooser1.getSelectedFile().getPath());
}
this.repaint();
}
//下面写入表Files的image字段FileData
void jToggleButton1_actionPerformed(ActionEvent e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
FileInputStream fis = new FileInputStream(filename);
byte[] bdata = new byte[(int) filename.length()];
fis.read(bdata);
String sql ="insert into Files (FileData) values(?)";
DBBean dbbean = new DBBean();
dbbean.initBean();
PreparedStatement pstmt=null;
pstmt=dbbean.getConn().prepareStatement(sql);
pstmt.setBytes(1,bdata);

pstmt.executeUpdate();
pstmt.close();
dbbean.closeConn();
fis.close();
System.out.println("OK");
}
catch (FileNotFoundException fnfe) {System.out.print(fnfe);}
catch (java.io.IOException ioe) {System.out.print(ioe);}
catch (java.sql.SQLException sqle ) {
System.out.print(sqle);}
}
//读出到文件D://81080.exe
void jToggleButton2_actionPerformed(ActionEvent e) {
try{
String sql="select FileData from Files where id=20";
DBBean dbbean = new DBBean();
dbbean.initBean();
ResultSet rs = dbbean.executeQuery(sql);
rs.next();
OutputStream bos = new FileOutputStream("D://81080.exe");
InputStream stream = rs.getBinaryStream("FileData");
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
String data = "The file has been written to ";
System.out.println(data);
}
catch (java.io.FileNotFoundException fnfe) {System.out.println(fnfe);}
catch (java.sql.SQLException sqle){System.out.println(sqle);}
catch (java.io.IOException ioe){System.out.println(ioe);}
}

}
//-------------------------------------Hamduke 10.31
 

Similar threads

W
回复
8
查看
1K
大唐电信
Z
回复
3
查看
628
434673459
4
回复
4
查看
579
434673459
4
后退
顶部