📄 oostablemodel.java~3~
字号:
package project1;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class OosTableModel extends AbstractTableModel
{
public OosTableModel() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private boolean connectedToDatabase=false;
public OosTableModel(String driver,String url,String query,String user,String password) throws SQLException,ClassNotFoundException
{
Class.forName(driver);
connection=DriverManager.getConnection(url,user,password);
statement=connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
connectedToDatabase=true;
setQuery(query);
}
public Class getColumnClass(int column) throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
try
{
String className=metaData.getColumnClassName(column+1);
return Class.forName(className);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return Object.class;
}
public int getColumnCount() throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
try
{
return metaData.getColumnCount();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
return 0;
}
public String getColumnName(int column) throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
try
{
return metaData.getColumnName(column+1);
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
return "";
}
}
public int getRowCount() throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
return numberOfRows;
}
public Object getValueAt(int row,int column) throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
try
{
resultSet.absolute(row+1);
return resultSet.getObject(column+1);
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
return "";
}
public void setQuery(String query) throws SQLException,IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException("Not Connected to Database");
resultSet=statement.executeQuery(query);
metaData=resultSet.getMetaData();
resultSet.last();
numberOfRows=resultSet.getRow();
fireTableStructureChanged();
}
public void disconnectFromDatabase()
{
try
{
statement.close();
connection.close();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
connectedToDatabase=false;
}
}
private void jbInit() throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -