当前位置: 技术问答>java相关
怪问题送分
来源: 互联网 发布时间:2015-04-28
本文导语: 我写了一个对数据库中图片操作的程序,但是有时可以运行,有时出错,我知道一定是指针问题,而且还会自己退出jbuilder。实在是找不出了,刚学java,希望大虾们帮忙一定送分。 一下是我三个类的代码: package imag...
我写了一个对数据库中图片操作的程序,但是有时可以运行,有时出错,我知道一定是指针问题,而且还会自己退出jbuilder。实在是找不出了,刚学java,希望大虾们帮忙一定送分。
一下是我三个类的代码:
package imagetest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
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();
jLabel1.setText("图像测试");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton1.setText("插入");
jButton2.setText("保存为");
contentPane.add(jLabel1, new XYConstraints(153, 21, 62, 30));
contentPane.add(jButton1, new XYConstraints(51, 140, -1, -1));
contentPane.add(jButton2, new XYConstraints(240, 143, 88, -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);
}
}
}
------------------------------
package imagetest;
import javax.swing.UIManager;
import java.awt.*;
import imagetest.*;
public class Application1
{
boolean packFrame = false;
Frame1 frame;
//Construct the application
public Application1()
{
frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
Application1 App = new Application1();
new InsertImage(App);
}
}
-------------------------------------
package imagetest;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.sql.*;
import java.util.*;
import imagetest.Application1;
public class InsertImage implements ActionListener
{
Connection conn;
Application1 App;
Properties props;
public InsertImage( Application1 A )
{
try
{
App = A;
App.frame.jButton1.addActionListener( this );
App.frame.jButton2.addActionListener( this );
props = new Properties();
props.put("user","sa");
props.put("password","");
props.put("server","smsdb@CENWENCHU:1433");
Driver MyDriver = (Driver)Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", props);
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
try
{
if (conn.isClosed())
conn = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", props);
}
catch ( Exception ex )
{
ex.printStackTrace();
}
if ( source == App.frame.jButton1 )
{
String StrSql = "insert into ServletDB values (?,?,?,?)";
try
{
File f = new File("c:\1.png");
FileInputStream fin = new FileInputStream(f);
PreparedStatement ps = conn.prepareStatement(StrSql);
ps.setString(1,"李丽华");
ps.setInt(2,12);
ps.setString(3,"RD");
int len = (int)f.length();
ps.setBinaryStream(4,fin,len);
ps.executeUpdate();
ps.close();
fin.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
else
{
try
{
FileOutputStream fou = new FileOutputStream("c:\2.png");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("Select * from ServletDB where UserPassWord = 12 ");
while (rs.next())
{
InputStream imagedata = rs.getBinaryStream("image");
byte[] idata = new byte[8000];
int rec = imagedata.read(idata);
fou.write(idata);
idata = null;
}
st.close();
rs.close();
fou.close();
}
catch (Exception ex )
{
ex.printStackTrace();
}
}
try
{
conn.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
一下是我三个类的代码:
package imagetest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame
{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
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();
jLabel1.setText("图像测试");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton1.setText("插入");
jButton2.setText("保存为");
contentPane.add(jLabel1, new XYConstraints(153, 21, 62, 30));
contentPane.add(jButton1, new XYConstraints(51, 140, -1, -1));
contentPane.add(jButton2, new XYConstraints(240, 143, 88, -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);
}
}
}
------------------------------
package imagetest;
import javax.swing.UIManager;
import java.awt.*;
import imagetest.*;
public class Application1
{
boolean packFrame = false;
Frame1 frame;
//Construct the application
public Application1()
{
frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
Application1 App = new Application1();
new InsertImage(App);
}
}
-------------------------------------
package imagetest;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.sql.*;
import java.util.*;
import imagetest.Application1;
public class InsertImage implements ActionListener
{
Connection conn;
Application1 App;
Properties props;
public InsertImage( Application1 A )
{
try
{
App = A;
App.frame.jButton1.addActionListener( this );
App.frame.jButton2.addActionListener( this );
props = new Properties();
props.put("user","sa");
props.put("password","");
props.put("server","smsdb@CENWENCHU:1433");
Driver MyDriver = (Driver)Class.forName("weblogic.jdbc.mssqlserver4.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", props);
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
try
{
if (conn.isClosed())
conn = DriverManager.getConnection("jdbc:weblogic:mssqlserver4", props);
}
catch ( Exception ex )
{
ex.printStackTrace();
}
if ( source == App.frame.jButton1 )
{
String StrSql = "insert into ServletDB values (?,?,?,?)";
try
{
File f = new File("c:\1.png");
FileInputStream fin = new FileInputStream(f);
PreparedStatement ps = conn.prepareStatement(StrSql);
ps.setString(1,"李丽华");
ps.setInt(2,12);
ps.setString(3,"RD");
int len = (int)f.length();
ps.setBinaryStream(4,fin,len);
ps.executeUpdate();
ps.close();
fin.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
else
{
try
{
FileOutputStream fou = new FileOutputStream("c:\2.png");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("Select * from ServletDB where UserPassWord = 12 ");
while (rs.next())
{
InputStream imagedata = rs.getBinaryStream("image");
byte[] idata = new byte[8000];
int rec = imagedata.read(idata);
fou.write(idata);
idata = null;
}
st.close();
rs.close();
fou.close();
}
catch (Exception ex )
{
ex.printStackTrace();
}
}
try
{
conn.close();
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
|
是你的程序初始化问题
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。