| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 716 人关注过本帖
标题:[求助]SQL查询结果回显到Table上的问题
只看楼主 加入收藏
kent_ck
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-7-29
收藏
 问题点数:0 回复次数:1 
[求助]SQL查询结果回显到Table上的问题

我使用Eclipse编写了一个学生成绩查询子页面,该页面可以通过编译,但是一点击查询按钮"search"调用getTable()方法时就抛出4个错误(用红色字体表示),问题主要出在代码后半部分的方法调用上.
下面是学生成绩查询子页面的全部代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.io.*;
public class Admin
{
public JFrame f;
public JMenuBar aMenuBar;
public JToolBar aToolBar;
public JTable aTable;
public JButton add,delete,search,turnback;
public JMenu manage,set;
public JMenuItem updata, cancel,setDB;
public JScrollPane scrollPane;
public Connection con;
public Statement stmt;
public ResultSet resultset;
public ResultSetMetaData rsMetaData;

public Admin()
{
f=new JFrame("学生成绩查询界面");
aMenuBar=new JMenuBar();
aToolBar=new JToolBar();

manage=new JMenu("管理");
set=new JMenu("设置");
manage.setMnemonic('M');
set.setMnemonic('S');
updata=new JMenuItem("更新数据");
cancel=new JMenuItem("返回");
setDB=new JMenuItem("数据库参数设置");
aMenuBar.add(manage);
aMenuBar.add(set);
manage.add(updata);
manage.add(cancel);
set.add(setDB);

add=new JButton("添加");
delete=new JButton("删除");
search=new JButton("查询");
turnback=new JButton("取消");
aToolBar.setLayout(new FlowLayout(FlowLayout.CENTER));
aToolBar.add(add);
aToolBar.add(delete);
aToolBar.add(search);
aToolBar.add(turnback);
aToolBar.setVisible(false);
//注册事件监听器
turnback.addActionListener(new gotoAdminLogin());
updata.addActionListener(new gotoAdminLogin());
setDB.addActionListener(new gotoAdminLogin());
cancel.addActionListener(new gotoAdminLogin());
search.addActionListener(new gotoAdminLogin());
add.addActionListener(new gotoAdminLogin());
delete.addActionListener(new gotoAdminLogin());

f.setJMenuBar(aMenuBar);
f.getContentPane().add(aToolBar,BorderLayout.NORTH);
f.setSize(400,300);
f.setLocation(300,230);
f.setVisible(true);

//测试数据库连接情况
try
{
//读入配置文件
Properties prop=new Properties();
prop.load(new FileInputStream("db.cfg"));
//获取各项配置参数
String ip=prop.getProperty("DB_IP");
String port=prop.getProperty("DB_PORT");
String username=prop.getProperty("DB_USER_NAME");
String password=prop.getProperty("DB_USER_PWD");
String dbname=prop.getProperty("DB_NAME");
//根据读入的配置参数构建数据库url
String url="jdbc:microsoft:sqlserver://"+ip+":"+port;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("数据库加载驱动成功");
con=DriverManager.getConnection(url,username,password);
con.setCatalog(dbname);
JOptionPane.showMessageDialog(null, "数据库连接成功!","信息",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据库连接失败!","信息",JOptionPane.INFORMATION_MESSAGE);
}
}

class gotoAdminLogin implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object sre=e.getSource();
if(sre==turnback)
{
aToolBar.setVisible(false);
}
else if(sre==cancel)
{
new Admin_Login();
f.setVisible(false);
}
else if(sre==setDB)
{
new setSQLOption();
}
else if(sre==updata)
{
aToolBar.setVisible(true);
}
else if(sre==search)
{
getTable();//报错点1
}
else if(sre==add){}
else if(sre==delete)
{
new DeleteDialog();
}
}
}
public void getTable()
{
try
{
//执行SQL语句
String readSql="SELECT * FROM StudentInfo";
stmt=con.createStatement();
resultset=stmt.executeQuery(readSql);
//在表格中显示查询结果
displayResultSet(resultset);//报错点2
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void displayResultSet(ResultSet rs)
throws SQLException
{
Vector columnHeads=new Vector();
Vector rows=new Vector();
try {
//获取字段的名称
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); ++i)
columnHeads.addElement(rsmd.getColumnName(i) );
//获取记录集
do
{
rows.addElement(getNextRow(rs,rsmd));//报错点3
}
while(rs.next());
//在表格中显示查询结果
//创建表格
aTable=new JTable(rows,columnHeads);
scrollPane=new JScrollPane(aTable);
f.getContentPane().add(scrollPane,BorderLayout.CENTER);
scrollPane.setVisible(true);
aTable.setSize(400,150);
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
}
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd)
throws SQLException
{
Vector currentRow = new Vector();
for (int i = 1;i <=rsmd.getColumnCount(); ++i )
currentRow.addElement(rs.getString(i));//报错点4
//返回一条记录
return currentRow;
}
public static void main(String args[])
{
new Admin();
}
}

搜索更多相关主题的帖子: 查询 结果 Table SQL 
2007-07-29 00:49
kent_ck
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-7-29
收藏
得分:0 
回复:(kent_ck)[求助]SQL查询结果回显到Table上的问...
以下是Eclipse给出的报错信息:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operation for the current cursor position.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.validateCursorPosition(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.getString(Unknown Source)
at Admin.getNextRow(Admin.java:178)
at Admin.displayResultSet(Admin.java:157)
at Admin.getTable(Admin.java:137)
at Admin$gotoAdminLogin.actionPerformed(Admin.java:119)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
2007-07-29 00:52
快速回复:[求助]SQL查询结果回显到Table上的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025746 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved