📄 resultsettablemodel.java
字号:
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.*;
import java.math.BigDecimal;
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 BigDecimal adminID;
public ResultSetTableModel(String driver, String url,
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;
}
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 exception)
{
exception.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 IllegalStateException, SQLException
{
if(!connectedToDatabase) throw new IllegalStateException("Not Connected to Database");
resultSet = statement.executeQuery(query);
metaData = resultSet.getMetaData();
resultSet.last();
numberOfRows = resultSet.getRow();
fireTableStructureChanged();
}
public void setExecute(String exe, String query) throws IllegalStateException, SQLException
{
if(!connectedToDatabase) throw new IllegalStateException("Not Connected to Database");
statement.execute(exe);
setQuery(query);
}
public boolean checkPassword(String user, String password)
{
if(!connectedToDatabase) throw new IllegalStateException("Not Connected to Database");
String str = "select adminID from admins where adminName = '" + user
+ "' and adminPassword = '" + password + "'";
try
{
setQuery(str);
}
catch(Exception e)
{
e.printStackTrace();
}
if(this.getRowCount() == 1)
{
adminID = (BigDecimal)getValueAt(0, 0);
return true;
}
return false;
}
public void disconnectFromDatabase()
{
try
{
statement.close();
connection.close();
}
catch(SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
connectedToDatabase = false;
}
}
public void setAutoCommit(boolean auto)
{
if(!connectedToDatabase) throw new IllegalStateException("Not Connected to Database");
try
{
connection.setAutoCommit(auto);
}
catch(SQLException sql)
{
System.err.println(sql.getMessage());
}
}
public void commit()
{
try
{
connection.commit();
}
catch(SQLException sql)
{
System.err.println(sql.getMessage());
}
}
public void rollback()
{
try
{
connection.rollback();
}
catch(SQLException sql)
{
System.err.println(sql.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -