⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displayqueryresults.java~2~

📁 许多开发者和用户都在寻找Java程序中访问数据库的便捷方法。由于Java是一个健壮
💻 JAVA~2~
字号:
 import java.awt.*;import java.awt.event.*;import java.sql.*;import java.util.*;import javax.swing.*;import javax.swing.table.*;public class DisplayQueryResults extends JFrame {  // JDBC driver name and database URL  static final String JDBC_DRIVER =    "com.microsoft.jdbc.sqlserver.SQLServerDriver";  static final String DATABASE_URL =    "jdbc:microsoft:sqlserver://BILLGATES:1433;" +    "user=fengwei;password=fengwei;databasename=books";  // default query retrieves all data from authors table  static final String DEFAULT_QUERY = "SELECT * FROM authors";  private ResultSetTableModel tableModel;  private JTextArea queryArea;  public DisplayQueryResults() {    super("Displaying Query Results");    try {      tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL,					   DEFAULT_QUERY);      queryArea = new JTextArea(DEFAULT_QUERY, 3, 100);      queryArea.setWrapStyleWord(true);      queryArea.setLineWrap(ture);      JScrollPane scrollPane = new JScrollPane(queryArea,					       ScrollPaneConstants.					       VERTICAL_SCROLLBAR_AS_NEEDED,					       ScrollPaneConstants.					       HORIZONTAL_SCROLLBAR_NEVER);      JButton submitButton = new JButton("Submit Query");      Box box = Box.createHorizontalBox();      box.add(scrollPane);      box.add(submitButton);      JTable resultTable = new JTable(tableModel);      Container c = getContentPane();      c.add(box, BorderLayout.NORTH);      c.add(new JScrollPane(resultTable), BorderLayout.CENTER);      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);	      // ensure database connection is closed	      tableModel.disconnectFromDatabase();	      System.exit(1);	    } // end inner catch	  } // end outter catch	} // end actionPerformed      } // end anonymous class ActionListener    ); // end call to addActionListener      // set window size and display window      setSize(500, 250);      setVisible(true);    } // end try    // detect problems loading database driver    catch( ClassNotFoundException classNotFound ) {      JOptionPane.showMessageDialog( null, classNotFound.getMessage(),	"Driver Not Found", JOptionPane.ERROR_MESSAGE );      System.exit( 1 );    }    // catch SQLException thrown by ResultSetTableModel    // if problems occur while setting up database    // connection and querying database    catch( SQLException sqlException ) {      JOptionPane.showMessageDialog( null, sqlException.getMessage(),	"Database Error", JOptionPane.ERROR_MESSAGE );      tableModel.disconnectFromDatabase();      System.exit( 1 );    }    setDefaultCloseOperation( DISPOSE_ON_CLOSE );    addWindowListener(	new WindowAdapter() {	  public void windowClosed( WindowEvent event ) {	    tableModel.disconnectFromDatabase();	    System.exit( 0 );	  }	}    );  } // end DisplayQueryResults constructor  public static void main( String args[] ) {    new DisplayQueryResults();  }} // end class DisplayQueryResults

⌨️ 快捷键说明

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