public class DisplayQueryResults extends JFrame{
static final String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=books";
static final String USERNAME = "sa";
static final String PASSWORD = "123456";
static final String DEFAULT_QUERY = "SELECT * FROM Authors";
private ResultSetTableModel tableModel;
private JTextArea queryArea;

public DisplayQueryResults(){
super("Displaying Query Results");
try{
tableModel = new ResultSetTableModel(dbURL,USERNAME,PASSWORD,DEFAULT_QUERY);
queryArea = new JTextArea(DEFAULT_QUERY,3,100);
queryArea.setWrapStyleWord(true);
queryArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JButton submitButton = new JButton("Submit Query");
Box boxNorth = Box.createHorizontalBox();
boxNorth.add(scrollPane);
boxNorth.add(submitButton);
JTable resultTable = new JTable(tableModel);
JLabel filterLabel = new JLabel("Filter:");
final JTextField filterText = new JTextField();
JButton filterButton = new JButton("Apply Filter");
Box boxSouth = Box.createHorizontalBox();

boxSouth.add(filterLabel);
boxSouth.add(filterText);
boxSouth.add(filterButton);

add(boxNorth,BorderLayout.NORTH);
add(new JScrollPane(resultTable),BorderLayout.CENTER);
add(boxSouth,BorderLayout.SOUTH);

submitButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
tableModel.setQuery(queryArea.getText());
}
catch(SQLException sqlException){
JOptionPane.showMessageDialog(null, sqlException.getMessage(),"Database error",JOptionPane.ERROR_MESSAGE);
try{
tableModel.setQuery(DEFAULT_QUERY);
queryArea.setText(DEFAULT_QUERY);
}
catch (SQLException sqlException2){
JOptionPane.showMessageDialog(null, sqlException2.getMessage(),"Database error",JOptionPane.ERROR_MESSAGE);
System.exit(1);

}
}
}
}




);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tableModel);
resultTable.setRowSorter(sorter);
setSize(500,250);
setVisible(true);

filterButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = filterText.getText();
if (text.length()==0)
sorter.setRowFilter(null);
else{
try{
sorter.setRowFilter(RowFilter.regexFilter(text));
}
catch(PatternSyntaxException pse){
JOptionPane.showMessageDialog(null,"Bad regex pattern","Bad regex pattern",JOptionPane.ERROR_MESSAGE);
}
}
}
}
);



}
catch(SQLException sqlException){
JOptionPane.showMessageDialog(null,sqlException.getMessage(),"Database error",JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(
new WindowAdapter(){
public void windowClosed(WindowEvent event){
tableModel.disconnectFromBatabase();
System.exit(0);
}
}
);



   }
public static void main(String args[]){
new DisplayQueryResults();
}
}
大神们看看这个代码有错误码?怎么不现实界面,说是无法连接到数据库

解决方案 »

  1.   

    Exception in thread "main" java.lang.IllegalStateException: Not Connected to Database
    at ch01.ResultSetTableModel.setQuery(ResultSetTableModel.java:83)
    at ch01.ResultSetTableModel.<init>(ResultSetTableModel.java:24)
    at ch01.DisplayQueryResults.<init>(DisplayQueryResults.java:43)
    at ch01.DisplayQueryResults.main(DisplayQueryResults.java:137)
      

  2.   

    都给你说了是ResultSetTableModel的错误了, 而你的ResultSetTableModel代码又没有贴出来,从你的错误来看,很可能是你把ResultSet当做返回值给返回了,ResultSet是要和数据库连接关联的,你把它返回了就和数据库连接脱了关系。你应该是在返回ResultSet那个方法中就要将ResultSet中的数据保存在一个容器类中