⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultsettablemodel.java~6~

📁 许多开发者和用户都在寻找Java程序中访问数据库的便捷方法。由于Java是一个健壮
💻 JAVA~6~
字号:
package javadatabase;import java.sql.*;import java.util.*;import javax.swing.table.*;public class ResultSetTableModel {  private Connection connection;  private Statement statement;  private ResultSet resultSet;  private ResultSetMetaData metaData;  private int numberOfRows;  private boolean connectedToDatabase = false;  public ResultSetTableModel( String driver, String url,    String query ) throws SQLException, ClassNotFoundException {    Class.forName( driver );    connection = DriverManager.getConnection( url );    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 e) {      e.printStackTrace();    }    return Object.class;  } // end method getColumnClass  public int getColumnCount() throws IllegalStateException {    if ( !connectedToDatabase )      throw new IllegalStateException( "Not Connected to Database" );    try {	return metaData.getColumnCount();    }    catch( SQLException e) {      e.printStackTrace();    }    return 0;  } // end method getColumnCount  public String getColumnName( int column ) throws IllegalStateException {    if ( !connectedToDatabase )      throw new IllegalStateException( "Not Connected to Database" );    try {	return metaData.getColumnName( column + 1 );    }    catch( SQLException e) {      e.printStackTrace();    }    return "";  } // end method getColumnCount  public int getRowCount() throws IllegalStateException {  if ( !connectedToDatabase )    throw new IllegalStateException( "Not Connected to Database" );  return numberOfRows;  } // end method getColumnCount  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 e) {      e.printStackTrace();    }    return "";  } // end method getColumnCount  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 e ) {      e.printStackTrace();    }    finally {      connectedToDatabase = false;    }  }} // end class ResultSetTableModel

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -