-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultSetTableModel.java
More file actions
127 lines (108 loc) · 3.03 KB
/
Copy pathResultSetTableModel.java
File metadata and controls
127 lines (108 loc) · 3.03 KB
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
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
public class ResultSetTableModel extends AbstractTableModel
{
private final Connection connection;
private final Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private boolean connectedToDatabase=false;
public ResultSetTableModel (String url, String userName, String password, String query) throws SQLException
{
connection=DriverManager.getConnection(url, userName, password);
statement=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
connectedToDatabase=true;
setQuery(query);
}
@Override
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;
}
@Override
public int getRowCount() throws IllegalStateException
{
if(!connectedToDatabase)
throw new IllegalStateException ("Not Connected to Database");
return numberOfRows;
}
@Override
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();
}
// 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");
// determine column name
try
{
return metaData.getColumnName(column + 1);
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
return ""; // if problems, return empty string for column name
}
public void disconnectFromDatabase()
{
if (connectedToDatabase)
{
try
{
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
finally
{
connectedToDatabase=false;
}
}
}
}