resultsettablemodel.java~3~

来自「许多开发者和用户都在寻找Java程序中访问数据库的便捷方法。由于Java是一个健」· JAVA~3~ 代码 · 共 95 行

JAVA~3~
95
字号
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 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 int 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}

⌨️ 快捷键说明

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