这个程序为什么到这条语句不能执行了。
错误信息:
java.sql.SQLException: Column not found
at sun.jdbc.odbc.JdbcOdbcResultSet.findColumn(JdbcOdbcResultSet.java:1849)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410)
at DisplayQueryResults.getNextRow(DisplayQueryResults.java:125)
at DisplayQueryResults.displayResultSet(DisplayQueryResults.java:107)
at DisplayQueryResults.getTable(DisplayQueryResults.java:89)
at DisplayQueryResults. <init> (DisplayQueryResults.java:77)
at DisplayQueryResults.main(DisplayQueryResults.java:144)
程序代码:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
public class DisplayQueryResults extends JFrame
{
private Connection con;
private Statement stmt;
private ResultSet resultset;
private ResultSetMetaData rsmd;
private JTable table;
private JTextArea input;
privateJButton submit;
DisplayQueryResults( )
{
super();
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
}
catch (ClassNotFoundException ee)
{
System.err.println( "加载驱动失败! ");
ee.printStackTrace();
System.exit(1);
}
try{
con=DriverManager.getConnection( "jdbc:odbc:bank_data ", "sa ", "123456 ");
}
catch(SQLException ex)
{
System.out.println( "\n******SQLException catch********\n ");
while(ex!=null)
{
System.out.println( "SQLState: "+ex.getSQLState());
System.out.println( "Message: "+ex.getMessage());
System.out.println( "Vendor: "+ex.getErrorCode());
ex= ex.getNextException();
System.exit(1);
}
}
String test= "select * from user_table ";
input = new JTextArea(test,4,30);
submit=new JButton( "查询 ");
submit.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){getTable();}});
JPanel toppanel=new JPanel();
toppanel.setLayout(new BorderLayout());
toppanel.add(new JScrollPane(input),BorderLayout.CENTER);
toppanel.add(submit,BorderLayout.SOUTH);
table= new JTable();
Container c=getContentPane();
c.setLayout(new BorderLayout());
c.add(toppanel,BorderLayout.NORTH);
c.add(table,BorderLayout.CENTER);
getTable();
setSize(500,300);
show();
}
private void getTable() {
try{
String sql=input.getText();
stmt=con.createStatement();
resultset=stmt.executeQuery(sql);
displayResultSet(resultset);
}
catch(SQLException e){e.printStackTrace();}
}
private void displayResultSet(ResultSet rs) throws SQLException
{
boolean next=rs.next();
if(!next){JOptionPane.showMessageDialog(this, "结果集中没纪录 ");setTitle( "无记录显示 ");return;}
Vector columnHeads=new Vector();
Vector rows=new Vector();
try{
rsmd=rs.getMetaData();
System.out.println(rsmd.getColumnCount());
for(int i=1;i <=rsmd.getColumnCount();i++)
{columnHeads.add(rsmd.getColumnName(i));System.out.println(rsmd.getColumnName(i));}
do{System.out.println( "ghg ");
rows.addElement(getNextRow(rs,rsmd));
}while(rs.next());
table= new JTable(rows,columnHeads);
JScrollPane scroller=new JScrollPane();
Container c=getContentPane();
c.remove(1);
c.add(scroller,BorderLayout.CENTER);
c.validate();
}
catch(SQLException ee){ee.printStackTrace();}
}
private Vector getNextRow(ResultSet rs ,ResultSetMetaData rsd) throws SQLException
{System.out.println(rsd.getColumnCount());
Vector currentRow=new Vector();
for(int i=1;i <=rsd.getColumnCount();++i)
{ System.out.println(rs.getString(i+1));
System.out.println(i);
currentRow.addElement(rs.getString(i));//到这条语句似没有执行
System.out.println(i);
System.out.println( "ghg ");
}
return currentRow;
}
public void shutDown()
{
try {con.close();}
catch(SQLException ee)
{
System.err.println( "不能断、开数据库连接 ");
ee.printStackTrace();
}
}
public static void main(String args[]){
final DisplayQueryResults app = new DisplayQueryResults();
app.addWindowListener(
new WindowAdapter(){public void windowClosing(WindowEvent e)
{app.shutDown();System.exit(0);}});
}
}
[解决办法]
for(int i=1;i <=rsmd.getColumnCount();i++)
{columnHeads.add(rsmd.getColumnName(i));System.out.println(rsmd.getColumnName(i));}
for(int i=1;i <=rsd.getColumnCount();++i)
{ System.out.println(rs.getString(i+1));
System.out.println(i);
从0开始,
[解决办法]
没有找到字段
因为for(int i=1;i <=rsd.getColumnCount();++i)