This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResultSetTableModel.java
174 lines (140 loc) · 6.24 KB
/
ResultSetTableModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class ResultSetTableModel extends AbstractTableModel {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
// keep track of database connection status
private boolean connectedToDatabase = false;
public ResultSetTableModel( String driver, String url,String query ) throws SQLException, ClassNotFoundException
{
// load database driver class
Class.forName( driver );
// connect to database
connection = DriverManager.getConnection( url );
// create Statement to query database
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY );
// update database connection status
connectedToDatabase = true;
// set query and execute it
setQuery( query );
}
public Class getColumnClass( int column ) throws IllegalStateException
{
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
// determine Java class of column
try {
String className = metaData.getColumnClassName( column + 1 );
// return Class object that represents className
return Class.forName( className );
}
// catch SQLExceptions and ClassNotFoundExceptions
catch ( Exception exception ) {
exception.printStackTrace();
}
// if problems occur above, assume type Object
return Object.class;
}
public int getColumnCount() throws IllegalStateException
{
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
// determine number of columns
try {
return metaData.getColumnCount();
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems occur above, return 0 for number of columns
return 0;
}
// get name of a particular column in ResultSet
public String getColumnName( int column ) throws IllegalStateException
{
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
try {
return metaData.getColumnName( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string for column name
return "";
}
// return number of rows in ResultSet
public int getRowCount() throws IllegalStateException
{
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
return numberOfRows;
}
public Object getValueAt( int row, int column )
throws IllegalStateException
{
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
// obtain a value at specified ResultSet row and column
try {
resultSet.absolute( row + 1 );
return resultSet.getObject( column + 1 );
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// if problems, return empty string object
return "";
}
public void setQuery( String query )
throws SQLException, IllegalStateException
{ // ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to Database" );
// specify query and execute it
resultSet = statement.executeQuery( query );
// obtain meta data for ResultSet
metaData = resultSet.getMetaData();
// determine number of rows in ResultSet
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
// notify JTable that model has changed
fireTableStructureChanged();
}
public void disconnectFromDatabase()
{
// close Statement and Connection
try {
statement.close();
connection.close();
}
// catch SQLExceptions and print error message
catch ( SQLException sqlException ) {
sqlException.printStackTrace();
}
// update database connection status
finally {
connectedToDatabase = false;
}
}
}