首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

通过用户界面处理excel数据库有关问题

2012-01-14 
通过用户界面处理excel数据库问题想通过java 的用户界面 来交互式的处理excel数据表中的内容! 代码如下imp

通过用户界面处理excel数据库问题
想通过java 的用户界面 来交互式的处理excel数据表中的内容! 代码如下

import java.awt.*;
import java.util.*;

import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class SimpleFrameTest {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
SizedFrame frame=new SizedFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

}

class SizedFrame extends JFrame
{
public SizedFrame()
{
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int screenHeight=screenSize.height;
int screenWeight=screenSize.width;
this.setSize(screenWeight/2, screenHeight/2);
this.setLocationByPlatform(true);
setTitle("sizedFrame");
//create buttons;
final JButton SearchButton=new JButton("查询");
JLabel production_id=new JLabel("产品号",SwingConstants.RIGHT);
JLabel price_id=new JLabel("价格");
final JTextField production=new JTextField(10);
final JTextField price=new JTextField(10);
JButtonPanel=new JPanel();
JButtonPanel.add(production_id);
JButtonPanel.add(production);
  JButtonPanel.add(price_id);
  JButtonPanel.add(price);
add(JButtonPanel,BorderLayout.NORTH);  
// 这一段函数是初始化 连接链表

try{
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ=C:\\Documents and Settings\\Administrator\\桌面\\example.xls;READONLY=FALSE"; // 不设置数据源
Properties p=new Properties();
p.put("charSet", "gbk");
Class.forName(driverName);
Connection dbConn=null;
dbConn = DriverManager.getConnection(dbURL, p);
final Statement smt = dbConn.createStatement(); }catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
} finally
{
try
{
dbConn.close();
} catch (SQLException e)
{
e.printStackTrace();
}
}
 
SearchButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
String select_id=production.getText();
ResultSet set = smt.executeQuery("select * from [sheet1$] where Id=select_id"); price.setText(set.getString(3));
}catch (SQLException e)
{
e.printStackTrace();

}
});  
 
}
private JPanel JButtonPanel;

}
想按下查询Button调用actionPerformed函数来得到数据表中的价格 ,然后显示在价格文本框中!

现在问题就是 在actionPerformed函数中总是出现类似 变量 smt无法处理的错误 我都不知道该将红色部分的这些语句怎么放了??高手指教!!万分感谢!

[解决办法]
final Statement smt定义为类全局变量试试。
[解决办法]
你写的代码有很多地方值得商榷,至少代码需要优化,给你一个能跑的例子
你自己修改DBQ文件的位置,example文件需要有id price至少这两个字段

Java code
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;public class SimpleFrameTest {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() {            public void run() {                SizedFrame frame = new SizedFrame();                frame.addWindowListener(frame);                frame.setVisible(true);            }        });    }}class SizedFrame extends JFrame implements WindowListener{    @Override    public void windowOpened(WindowEvent e) {        // TODO Auto-generated method stub            }    @Override    public void windowClosing(WindowEvent e) {        // TODO Auto-generated method stub        this.closeCon();        System.exit(0);    }    @Override    public void windowClosed(WindowEvent e) {        // TODO Auto-generated method stub            }    @Override    public void windowIconified(WindowEvent e) {        // TODO Auto-generated method stub            }    @Override    public void windowDeiconified(WindowEvent e) {        // TODO Auto-generated method stub            }    @Override    public void windowActivated(WindowEvent e) {        // TODO Auto-generated method stub            }    @Override    public void windowDeactivated(WindowEvent e) {        // TODO Auto-generated method stub            }    private Connection dbConn = null;    private Statement smt =null;                 private void closeCon(){                try {            smt.close();            dbConn.close();        } catch (SQLException e) {            e.printStackTrace();        }    }        public SizedFrame() {        Toolkit kit = Toolkit.getDefaultToolkit();        Dimension screenSize = kit.getScreenSize();        int screenHeight = screenSize.height;        int screenWeight = screenSize.width;        this.setSize(screenWeight / 2, screenHeight / 2);        this.setLocationByPlatform(true);        setTitle("sizedFrame");        // create buttons;        final JButton searchButton = new JButton("查询");        JLabel production_id = new JLabel("产品号", SwingConstants.RIGHT);        JLabel price_id = new JLabel("价格");        final JTextField production = new JTextField(10);        final JTextField price = new JTextField(10);        JButtonPanel = new JPanel();        JButtonPanel.add(production_id);        JButtonPanel.add(production);        JButtonPanel.add(price_id);        JButtonPanel.add(price);        JButtonPanel.add(searchButton);        add(JButtonPanel, BorderLayout.NORTH);        // 这一段函数是初始化 连接链表        try {            String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";            String dbURL = "jdbc:odbc:driver={Microsoft Excel Driver (*.xls)};DBQ=resources/excel/example.xls;READONLY=FALSE"; // 不设置数据源            Properties p = new Properties();            p.put("charSet", "gbk");            Class.forName(driverName);//            Connection dbConn = null;            dbConn = DriverManager.getConnection(dbURL, p);            smt = dbConn.createStatement();        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }         searchButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent event) {                try {                    String select_id_str = production.getText();                    int select_id=Integer.parseInt(select_id_str);                    ResultSet set = smt                            .executeQuery("select * from [sheet1$] where id="+select_id);                    String priceStr="";                    while(set.next()){                        priceStr=set.getString("price");                    }                    price.setText(priceStr);                } catch (SQLException e) {                    e.printStackTrace();                }            }        });    }    private JPanel JButtonPanel;} 

热点排行