请问下,怎样可以从数据库中获取出图片,然后读出到JPANEL里面,通过不断点击list里面的选项,来更换不同的图片
各位高手,请问下,怎样可以从数据库中获取出图片,然后读出到JPANEL里面,通过不断点击list里面的选项,来更换不同的图,我想问下,思路是不是想先是连接数据库,然后获取数据库图片,以二进制流读到JPANEL,但是这样做的话,能不停切换图片吗 --------------------编程问答-------------------- 可以,不过因为每次切换的时候都要去读数据库,可能速度稍有点卡。改进方式是:如果图片不大的话,可以缓存在JVM中。 --------------------编程问答-------------------- 高手,还有个问题就是以二进制流的方法读入JPanel里面的时候,那个图片的位置该怎样填
例如这句
ImageIcon backg_img=new ImageIcon(Test.class.getResource("/abc/backg.png"));
实在括号里面填写读取数据库图片那一行的语句吗,因为数据库中的图片不是只有一张,是在括号里面填写
ResultSet rs.getString(2);吗,这样可不可以行得通,还是说还有其他办法,请指教 --------------------编程问答-------------------- 你的图片是以blob形式存在数据库字段里面?
还是说图片其实是在磁盘中,数据库只是存了该图片在磁盘中的路径? --------------------编程问答-------------------- 我是在数据库中直接使用“使用向导创建表”里面直接按“照片”这个示例字段,然后建好表之后,直接把图片拖进那一项,然后现在我想做的事是在JPanel里读出这张图片,如果这个方法不现实的话,请告诉我其他更好的办法 --------------------编程问答-------------------- 没,只是不太清楚你数据库究竟是咋整的。
猜测是blob形式,那么操作的方式是:
ResultSet rs; // 操作JDBC获取ResultSet过程略
InputStream is = rs.getBinaryStream("字段名");
Image img = ImageIO.read(is);
ImageIcon icon = new ImageIcon(img);
JLabel lbl = new JLabel(icon);
如果JLabel早已创建好,也可以:
lbl.setIcon(icon);
lbl.updateUI(); --------------------编程问答-------------------- 原来如此,谢谢,高手,我拿去试一试先 --------------------编程问答-------------------- 有一个问题想问下,高手,你那JLabel怎样外面写一次,我在外面再写一次的话,会运行不了,麻烦你了
代码片段如下:
list.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==list){
try {
con=DriverManager.getConnection("jdbc:odbc:shujuku","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
while(rs.next()){
String yangzi=rs.getString(2);
String jieshao=rs.getString(4);
String danjia=rs.getString(7);
InputStream is = rs.getBinaryStream("照片");
Image img = ImageIO.read(is);
ImageIcon icon = new ImageIcon(img);
JLabel lbl = new JLabel(icon);
lbl.setIcon(icon);
lbl.updateUI();
}
} catch (SQLException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}});
try {
con=DriverManager.getConnection("jdbc:odbc:shujuku","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
while(rs.next()){
String name=rs.getString(3);
list.add("1"+name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel lb1 = new JLabel("");
lb1.setBounds(189, 10, 113, 121);
frame.getContentPane().add(lb1);
} --------------------编程问答-------------------- LZ把所以代码贴出来,这样帮你的人可能会多些 --------------------编程问答-------------------- 恩,好提议,谢谢
代码:
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.applet.*;
import java.awt.Event;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.sql.*;
import connect.Connect;
import java.awt.*;
public class cuenhuo {
JFrame frame;
public static Statement sql;
public static ResultSet rs;
public static Connection con;
Connect c=new Connect();
/**
* Launch the application.
*/
public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
shouyinji window = new shouyinji();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* @wbp.parser.entryPoint
*/
public cuenhuo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setForeground(Color.WHITE);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u8D44\u6599");
label.setBounds(204, 148, 98, 15);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u8D44\u6599");
label_1.setBounds(204, 195, 98, 15);
frame.getContentPane().add(label_1);
JButton button = new JButton("\u5237\u65B0");
button.setFont(new Font("黑体", Font.PLAIN, 18));
button.setForeground(Color.RED);
button.setBounds(224, 233, 93, 23);
frame.getContentPane().add(button);
final List list = new List();
list.setBackground(Color.CYAN);
list.setBounds(0, 10, 164, 246);
frame.getContentPane().add(list);
list.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==list){
try {
con=DriverManager.getConnection("jdbc:odbc:shujuku","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
while(rs.next()){
String jieshao=rs.getString(4);
String danjia=rs.getString(7);
InputStream is = rs.getBinaryStream("照片");
Image img = ImageIO.read(is);
ImageIcon icon = new ImageIcon(img);
JLabel lbl = new JLabel(icon);
lbl.setIcon(icon);
lbl.updateUI();
}
} catch (SQLException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}});
try {
con=DriverManager.getConnection("jdbc:odbc:shujuku","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 库存表");
while(rs.next()){
String name=rs.getString(3);
list.add("1"+name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JLabel lb1 = new JLabel("");
lb1.setBounds(189, 10, 113, 121);
frame.getContentPane().add(lb1);
}
}
--------------------编程问答-------------------- 好像你的JLabel lb1 创建了两次,你设个全局变量试试, --------------------编程问答--------------------
--------------------编程问答-------------------- 我发现原来我的数据库中照片这一项是OLE对象 --------------------编程问答-------------------- 我发现原来我的数据库中照片这一项是OLE对象 --------------------编程问答--------------------
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class cuenhuo {
JFrame frame;
public static Statement sql;
public static ResultSet rs;
public static Connection con;
Connect c = new Connect();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
shouyinji window = new shouyinji();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* @wbp.parser.entryPoint
*/
public cuenhuo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setForeground(Color.WHITE);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u8D44\u6599");
label.setBounds(204, 148, 98, 15);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u6CA1\u6709\u4EFB\u4F55\u8D44\u6599");
label_1.setBounds(204, 195, 98, 15);
frame.getContentPane().add(label_1);
JButton button = new JButton("\u5237\u65B0");
button.setFont(new Font("黑体", Font.PLAIN, 18));
button.setForeground(Color.RED);
button.setBounds(224, 233, 93, 23);
frame.getContentPane().add(button);
final List list = new List();
list.setBackground(Color.CYAN);
list.setBounds(0, 10, 164, 246);
frame.getContentPane().add(list);
try {
con = DriverManager.getConnection("jdbc:odbc:shujuku", "", "");
sql = con.createStatement();
rs = sql.executeQuery("select * From 库存表");
while (rs.next()) {
String name = rs.getString(3);
list.add("1" + name);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final JLabel lb1 = new JLabel("");
lb1.setBounds(189, 10, 113, 121);
frame.getContentPane().add(lb1);
list.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == list) {
try {
con = DriverManager.getConnection("jdbc:odbc:shujuku",
"", "");
sql = con.createStatement();
rs = sql.executeQuery("select * From 库存表");
while (rs.next()) {
String jieshao = rs.getString(4);
String danjia = rs.getString(7);
InputStream is = rs.getBinaryStream("照片");
Image img = ImageIO.read(is);
ImageIcon icon = new ImageIcon(img);
lb1.setIcon(icon);
lb1.updateUI();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
}
那估计是不行滴。。。 --------------------编程问答-------------------- 那要改成什么类型才可以 --------------------编程问答-------------------- 这可真是头疼了,Access这方面似乎比较的存在问题。。。
你能不能换成SQL Server用Image类型;或者换Oracle用blob类型啥的。。。
另外你是怎么把图片的信息存进去的,也是个问题;保存方式和读取方式需要保持一致的。 --------------------编程问答-------------------- 请问如何更新一条已经存在于数据库的语句,如果用UPDATE语句的话,因为我有些条件是变量,所以不知道如何实现,请教高手 --------------------编程问答-------------------- 请问如何更新一条已经存在于数据库的数据,如果用UPDATE语句的话,因为我有些条件是变量,所以不知道如何实现,请教高手 --------------------编程问答-------------------- 代码如下:
package zhiwei;
import java.awt.EventQueue;
public class 销售管理 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
static Statement sql;
static Connection con;
static ResultSet rs;
final JLabel lblNull_3 = new JLabel();
static JComboBox comboBox = new JComboBox();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(ClassNotFoundException e){
}
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 销售管理");
while(rs.next()){
String name=rs.getString(1);
comboBox.addItem(name);
}
}catch(SQLException e1){
}
try {
销售管理 window = new 销售管理();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public 销售管理() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(null);
JLabel label = new JLabel("\u9500\u552E\u7BA1\u7406");
label.setForeground(Color.RED);
label.setBackground(Color.BLACK);
label.setFont(new Font("华文琥珀", Font.PLAIN, 26));
label.setBounds(153, 10, 110, 28);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u7269\u54C1\u540D\u79F0\uFF1A");
label_1.setFont(new Font("宋体", Font.PLAIN, 18));
label_1.setBounds(10, 54, 90, 21);
frame.getContentPane().add(label_1);
JLabel label_3 = new JLabel("\u7269\u54C1\u5355\u4EF7\uFF1A");
label_3.setFont(new Font("宋体", Font.PLAIN, 18));
label_3.setBounds(10, 115, 90, 21);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("\u8D2D\u5165\u6570\u91CF\uFF1A");
label_4.setFont(new Font("宋体", Font.PLAIN, 18));
label_4.setBounds(10, 146, 90, 21);
frame.getContentPane().add(label_4);
JLabel label_5 = new JLabel("\u5171\u8BA1\uFF1A");
label_5.setFont(new Font("宋体", Font.PLAIN, 18));
label_5.setBounds(110, 177, 54, 21);
frame.getContentPane().add(label_5);
List list = new List();
list.setBounds(270, 72, 149, 126);
frame.getContentPane().add(list);
final JLabel lblNull = new JLabel("null");
lblNull.setBounds(110, 120, 126, 15);
frame.getContentPane().add(lblNull);
final JLabel lblNull_4 = new JLabel("null");
lblNull_4.setBounds(71, 208, 54, 15);
frame.getContentPane().add(lblNull_4);
comboBox.setBounds(110, 56, 126, 21);
frame.getContentPane().add(comboBox);
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 销售管理");
int get=comboBox.getSelectedIndex();
int duiying=0;
while(rs.next()){
if(duiying==get){
String danjia=rs.getString(2);
String shuliang=rs.getString(3);
lblNull_4.setText(shuliang);
lblNull.setText(danjia);
}duiying++;
}con.close();
}catch(SQLException e1){
}
}
});
final JLabel lblNull_1 = new JLabel("null");
lblNull_1.setBounds(170, 182, 54, 15);
frame.getContentPane().add(lblNull_1);
JLabel label_6 = new JLabel("null");
label_6.setBounds(120, 246, 76, 15);
frame.getContentPane().add(label_6);
JButton button = new JButton("\u786E\u5B9A");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int sum=0;
int chengfa=0;
int dej = (int) Double.parseDouble(textField.getText());
int dyj = (int) Double.parseDouble(lblNull_4.getText());
lblNull_4.setText(""+(chengfa=dyj - dej));
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("insert into 销售管理 values('"+comboBox.getSelectedItem()+"','"+lblNull.getText()+"','"+lblNull_4.getText()+"')");
//String columnNames={"产品名称","产品单价","产品数量"};
//sql.executeUpdate(sql1, columnNames);
lblNull_4.setText(""+(chengfa=dyj - dej));
con.close();
}catch(SQLException e1){
}
if(lblNull.getText()!=""){
int diyigezhi = (int) Double.parseDouble(textField.getText());
int diergezhi = (int) Double.parseDouble(lblNull.getText());
lblNull_1.setText(""+(sum=diyigezhi * diergezhi));
}
}
});
button.setFont(new Font("华文琥珀", Font.PLAIN, 18));
button.setForeground(Color.RED);
button.setBounds(10, 177, 93, 23);
frame.getContentPane().add(button);
JButton button_1 = new JButton("\u5171\u8BA1");
button_1.setForeground(Color.RED);
button_1.setFont(new Font("华文琥珀", Font.PLAIN, 18));
button_1.setBounds(10, 241, 110, 23);
frame.getContentPane().add(button_1);
textField = new JTextField();
textField.setBounds(104, 148, 66, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel label_2 = new JLabel("\u6536\u53D6\uFF1A");
label_2.setFont(new Font("宋体", Font.PLAIN, 18));
label_2.setBounds(264, 207, 54, 21);
frame.getContentPane().add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(328, 209, 66, 21);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton button_2 = new JButton("\u786E\u8BA4");
button_2.setBounds(274, 241, 93, 23);
frame.getContentPane().add(button_2);
JLabel label_7 = new JLabel("\u627E\u56DE\uFF1A");
label_7.setFont(new Font("宋体", Font.PLAIN, 18));
label_7.setBounds(264, 281, 54, 21);
frame.getContentPane().add(label_7);
JLabel lblNull_2 = new JLabel("null");
lblNull_2.setBounds(328, 286, 54, 15);
frame.getContentPane().add(lblNull_2);
lblNull_3.setBounds(10, 287, 110, 15);
frame.getContentPane().add(lblNull_3);
frame.setBounds(100, 100, 450, 361);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 在线表");
while(rs.next()){
String num=rs.getString(3);
lblNull_3.setText(num);
}
}catch(SQLException e1){
}
JButton button_3 = new JButton("\u8FDB\u5165\u7269\u54C1\u7BA1\u7406");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(lblNull_3.getText().equals("0")){
JOptionPane.showMessageDialog(null,"进入失败,权限不足");
}else if(lblNull_3.getText().equals("1")){
JOptionPane.showMessageDialog(null,"进入成功,欢饮进入");
new 物品管理().main(null);
frame.dispose();
}
}
});
button_3.setForeground(Color.RED);
button_3.setBounds(10, 304, 110, 23);
frame.getContentPane().add(button_3);
JButton button_4 = new JButton("\u9000\u51FA\u5E76\u56DE\u5230\u767B\u9646\u754C\u9762");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("delete * from 在线表 ");
}catch(SQLException e1){
}
new 总系统().main(null);
frame.dispose();
}
});
button_4.setBounds(170, 304, 148, 23);
frame.getContentPane().add(button_4);
JButton button_5 = new JButton("\u5237\u65B0");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
con=DriverManager.getConnection("jdbc:odbc:sjk","","");
sql=con.createStatement();
rs=sql.executeQuery("select * From 在线表");
while(rs.next()){
String num=rs.getString(3);
lblNull_3.setText(num);
}
}catch(SQLException e1){
}
}
});
button_5.setBounds(170, 282, 93, 23);
frame.getContentPane().add(button_5);
JButton button_6 = new JButton("\u8FD4\u56DE\u83DC\u5355");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new 菜单().main(null);
}
});
button_6.setBounds(328, 304, 93, 23);
frame.getContentPane().add(button_6);
JLabel label_8 = new JLabel("\u5269\u4F59\u6570\u91CF\uFF1A");
label_8.setBounds(10, 210, 66, 15);
frame.getContentPane().add(label_8);
}
}
--------------------编程问答-------------------- 楼主的代码让人看着好纠结
补充:Java , Eclipse