连接到数据库的程序的源代码.doc
文本预览下载声明
这个程序是连接到数据库的一个程序,主要功能是在上方的文本输入框中输入查询语句,实现从数据库中查询的功能,还可以在下方的过滤文本框中输入要过滤的文本,实现过滤查询结果的功能。
现在的问题是编译能够成功,但是运行时会报错。错误信息及源代码在下面给出,请问原因为何?多谢指教。
//这是ResultSetTableModel类,用作JTable的构造函数的参数。
package ResultSetTableModel;
import java.sql.*;
import javax.swing.table.*;
public class ResultSetTableModel extends AbstractTableModel
{
private Connection connection;
private Statement statement;
private ResultSet resultset;
private ResultSetMetaData metadata;
private int numberofrows;
private boolean connectedtodatabase=false;
public ResultSetTableModel(String url,String username,String password,String query)
{
try
{
connection=DriverManager.getConnection(url,username,password);
statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
connectedtodatabase=true;
setQuery(query);
}
catch(SQLException sqlexception)
{
System.out.println(error on connecting to database);
}
}
public void setQuery(String query) throws SQLException,IllegalStateException
{
if(!connectedtodatabase)
{
throw new IllegalStateException(Not connected to database);
}
resultset=statement.executeQuery(query);
metadata=resultset.getMetaData();
}
public void disconnectFromDatabase()
{
if(connectedtodatabase)
{
try
{
resultset.close();
statement.close();
connection.close();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
connectedtodatabase=false;
}
}
}
public int getColumnCount() throws IllegalStateException
{
int currentrow=1;
if(!connectedtodatabase)
{
throw new IllegalStateException(Not connected to database);
}
try
{
resultset.last();
currentrow=resultset.getRow();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
return currentrow;
}
public Class getColumnClass(int column) throws IllegalStateException
{
if(!connected
显示全部